[GH-ISSUE #108] refactor http Handling #90

Closed
opened 2026-05-07 00:19:47 +02:00 by BreizhHardware · 4 comments

Originally created by @genofire on GitHub (Jan 19, 2022).
Original GitHub issue: https://github.com/binwiederhier/ntfy/issues/108

Please use switch instatt of a list of else if:
github.com/binwiederhier/ntfy@c83b5c6e73/server/server.go (L314)

Example with expression in case:

    // doesn't have an expression after switch
    switch {  
        case v == "Rob":         // check the case here
        fmt.Println("Hi, Rob!")
        default:
        fmt.Println("Hi, user!")
    }

Otherwise there are routing libraries out there, and ways: https://benhoyt.com/writings/go-routing/

I love gin-gonic not here meanthan

Originally created by @genofire on GitHub (Jan 19, 2022). Original GitHub issue: https://github.com/binwiederhier/ntfy/issues/108 Please use switch instatt of a list of `else if`: https://github.com/binwiederhier/ntfy/blob/c83b5c6e73c6b386b83a63a77704d8e40fb45df3/server/server.go#L314 Example with expression in `case`: ```golang // doesn't have an expression after switch switch { case v == "Rob": // check the case here fmt.Println("Hi, Rob!") default: fmt.Println("Hi, user!") } ``` --- Otherwise there are routing libraries out there, and ways: https://benhoyt.com/writings/go-routing/ I love [gin-gonic](https://gin-gonic.com/) not here meanthan
Author
Owner

@binwiederhier commented on GitHub (Jan 19, 2022):

It's funny I read the post you linked before, and I chose not to do any of them. I have read and kept up with routers a lot and I think they just don't add enough value to bring them in.

As for the switch case; isn't that just a personal preference?

<!-- gh-comment-id:1016781918 --> @binwiederhier commented on GitHub (Jan 19, 2022): It's funny I read the post you linked before, and I chose not to do any of them. I have read and kept up with routers a lot and I think they just don't add enough value to bring them in. As for the switch case; isn't that just a personal preference?
Author
Owner

@genofire commented on GitHub (Jan 19, 2022):

personal preference if you call it that way ....

source code should be readable and therefore switch was designed ...

normally switch is optimized by compiler for runtime, but i believe that is not possible in a expression in case way.

<!-- gh-comment-id:1016962735 --> @genofire commented on GitHub (Jan 19, 2022): personal preference if you call it that way .... source code should be readable and therefore `switch` was designed ... normally switch is optimized by compiler for runtime, but i believe that is not possible in a expression in case way.
Author
Owner

@genofire commented on GitHub (Jan 19, 2022):

You are already using an Http-Router:
github.com/binwiederhier/ntfy@c83b5c6e73/server/server.go (L232-L238)

maybe you should rename :
github.com/binwiederhier/ntfy@c83b5c6e73/server/server.go (L296)
to func (s *Server) ServeHTTP(w ResponseWriter, r *Request) and direct use it.

And put it s here instatt of mux:
github.com/binwiederhier/ntfy@c83b5c6e73/server/server.go (L238)

<!-- gh-comment-id:1016972528 --> @genofire commented on GitHub (Jan 19, 2022): You are already using an Http-Router: https://github.com/binwiederhier/ntfy/blob/c83b5c6e73c6b386b83a63a77704d8e40fb45df3/server/server.go#L232-L238 maybe you should rename : https://github.com/binwiederhier/ntfy/blob/c83b5c6e73c6b386b83a63a77704d8e40fb45df3/server/server.go#L296 to `func (s *Server) ServeHTTP(w ResponseWriter, r *Request)` and direct use it. And put it `s` here instatt of `mux`: https://github.com/binwiederhier/ntfy/blob/c83b5c6e73c6b386b83a63a77704d8e40fb45df3/server/server.go#L238
Author
Owner

@binwiederhier commented on GitHub (Feb 5, 2022):

to func (s *Server) ServeHTTP(w ResponseWriter, r *Request) and direct use it.

That would expose the ServeHTTP method to the outside, which I did not want.

Overall I am quite happy with the HTTP routing handling. I like the if/then/else the way it is. I may adjust it a little if it gets too unwieldy in the future but for now I think this is fine.

Thanks for the suggestion though. Keep them coming!

<!-- gh-comment-id:1030634477 --> @binwiederhier commented on GitHub (Feb 5, 2022): > to func (s *Server) ServeHTTP(w ResponseWriter, r *Request) and direct use it. That would expose the ServeHTTP method to the outside, which I did not want. Overall I am quite happy with the HTTP routing handling. I like the if/then/else the way it is. I may adjust it a little if it gets too unwieldy in the future but for now I think this is fine. Thanks for the suggestion though. Keep them coming!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ntfy#90
No description provided.