cleanup: typo and code
This commit is contained in:
parent
8035c6f7af
commit
d639ea1b71
18
router.go
18
router.go
@ -6,16 +6,12 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
const RouteCtxKey = "ServeCTX"
|
// Router is a wrapper around the go's standard http.ServeMux.
|
||||||
|
// It's a lean wrapper with methods to make routing easier
|
||||||
type (
|
type Router struct {
|
||||||
// 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
|
mux *http.ServeMux
|
||||||
middlewares []func(http.Handler) http.Handler
|
middlewares []func(http.Handler) http.Handler
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
func NewRouter() *Router {
|
func NewRouter() *Router {
|
||||||
return &Router{
|
return &Router{
|
||||||
@ -26,7 +22,7 @@ func NewRouter() *Router {
|
|||||||
// Use will register middleware(s) with router stack
|
// Use will register middleware(s) with router stack
|
||||||
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
func (r *Router) Use(h ...func(http.Handler) http.Handler) {
|
||||||
if r == nil {
|
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...)
|
r.middlewares = append(r.middlewares, h...)
|
||||||
}
|
}
|
||||||
@ -81,7 +77,7 @@ func (r *Router) Trace(pattern string, h http.HandlerFunc) {
|
|||||||
// panics.
|
// panics.
|
||||||
func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
func (r *Router) handlerFunc(method, pattern string, h http.HandlerFunc) {
|
||||||
if r == nil {
|
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)
|
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) {
|
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
if r == nil {
|
if r == nil {
|
||||||
panic("method ServeHTTP called on nil")
|
panic("mux: method ServeHTTP called on nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
r.mux.ServeHTTP(w, req)
|
r.mux.ServeHTTP(w, req)
|
||||||
|
@ -2,6 +2,7 @@ package mux
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -10,7 +11,7 @@ import (
|
|||||||
|
|
||||||
type ServeCB func(srv *http.Server) error
|
type ServeCB func(srv *http.Server) error
|
||||||
|
|
||||||
// Serve with gracefull shutdown
|
// Serve with graceful shutdown
|
||||||
func (r *Router) Serve(cb ServeCB) {
|
func (r *Router) Serve(cb ServeCB) {
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
Handler: r,
|
Handler: r,
|
||||||
@ -32,7 +33,7 @@ func (r *Router) Serve(cb ServeCB) {
|
|||||||
close(idleConnsClosed)
|
close(idleConnsClosed)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := cb(srv); err != http.ErrServerClosed {
|
if err := cb(srv); !errors.Is(err, http.ErrServerClosed) {
|
||||||
// Error starting or closing listener:
|
// Error starting or closing listener:
|
||||||
slog.Error("start server error", "error", err)
|
slog.Error("start server error", "error", err)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user