From d639ea1b71d0e8c5489a94e4949afba89bc327d1 Mon Sep 17 00:00:00 2001 From: Ankit Patial Date: Sat, 12 Oct 2024 20:57:57 +0530 Subject: [PATCH] cleanup: typo and code --- router.go | 22 +++++++++------------- router_serve.go | 5 +++-- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/router.go b/router.go index c909f3d..b40ba1c 100644 --- a/router.go +++ b/router.go @@ -6,16 +6,12 @@ import ( "strings" ) -const RouteCtxKey = "ServeCTX" - -type ( - // Router is a wrapper around the go's standard http.ServeMux. - // It's a lean wrapper with methods to make routing easier - Router struct { - mux *http.ServeMux - middlewares []func(http.Handler) http.Handler - } -) +// Router is a wrapper around the go's standard http.ServeMux. +// It's a lean wrapper with methods to make routing easier +type Router struct { + mux *http.ServeMux + middlewares []func(http.Handler) http.Handler +} func NewRouter() *Router { return &Router{ @@ -26,7 +22,7 @@ func NewRouter() *Router { // Use will register middleware(s) with router stack func (r *Router) Use(h ...func(http.Handler) http.Handler) { if r == nil { - panic("serve: func Use was called on nil") + panic("mux: func Use was called on nil") } r.middlewares = append(r.middlewares, h...) } @@ -81,7 +77,7 @@ func (r *Router) Trace(pattern string, h http.HandlerFunc) { // panics. func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) { if r == nil { - panic("serve: func Handle() was called on nil") + panic("mux: func Handle() was called on nil") } path := fmt.Sprintf("%s %s", method, pattern) @@ -142,7 +138,7 @@ func (r *Router) Resource(pattern string, fn func(resource *Resource)) { func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { if r == nil { - panic("method ServeHTTP called on nil") + panic("mux: method ServeHTTP called on nil") } r.mux.ServeHTTP(w, req) diff --git a/router_serve.go b/router_serve.go index 42fabe3..1c3338a 100644 --- a/router_serve.go +++ b/router_serve.go @@ -2,6 +2,7 @@ package mux import ( "context" + "errors" "log/slog" "net/http" "os" @@ -10,7 +11,7 @@ import ( type ServeCB func(srv *http.Server) error -// Serve with gracefull shutdown +// Serve with graceful shutdown func (r *Router) Serve(cb ServeCB) { srv := &http.Server{ Handler: r, @@ -32,7 +33,7 @@ func (r *Router) Serve(cb ServeCB) { close(idleConnsClosed) }() - if err := cb(srv); err != http.ErrServerClosed { + if err := cb(srv); !errors.Is(err, http.ErrServerClosed) { // Error starting or closing listener: slog.Error("start server error", "error", err) }