cleanup: typo and code
This commit is contained in:
parent
8035c6f7af
commit
d639ea1b71
22
router.go
22
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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user