diff --git a/bind.go b/bind.go index d3f9487..3f2d6c5 100644 --- a/bind.go +++ b/bind.go @@ -249,12 +249,12 @@ func JsonRender() RendererFunc { } } - type response struct { + type JsonResponse struct { Code int `json:"code"` Message string `json:"message,omitempty"` Data interface{} `json:"data"` } - ctx.JSON(http.StatusOK, response{Code: code, Message: message, Data: result}) + _ = ctx.JSON(http.StatusOK, JsonResponse{Code: code, Message: message, Data: result}) } } diff --git a/context.go b/context.go index 967805c..c58d3ec 100644 --- a/context.go +++ b/context.go @@ -337,6 +337,14 @@ type RouteContext struct { methodsAllowed []methodTyp } +// AllowedMethods report allowed http methods. +func (c *RouteContext) AllowedMethods() (methods []string) { + for _, m := range c.methodsAllowed { + methods = append(methods, methodTypString(m)) + } + return +} + // Reset context to initial state func (c *RouteContext) Reset() { c.Routes = nil diff --git a/middleware.go b/middleware.go index 28386b2..db4da6b 100644 --- a/middleware.go +++ b/middleware.go @@ -30,13 +30,13 @@ type Middlewares []MiddlewareFunc // Handler builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. func (mws Middlewares) Handler(h http.Handler) http.Handler { - return &chainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws} + return &ChainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws} } // HandlerFunc builds and returns a http.Handler from the chain of middlewares, // with `h http.Handler` as the final handler. func (mws Middlewares) HandlerFunc(h http.HandlerFunc) http.Handler { - return &chainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws} + return &ChainHandler{Endpoint: h, chain: mws.chain(h), Middlewares: mws} } // Build a http.Handler composed of an inline middlewares. @@ -51,12 +51,16 @@ func (mws Middlewares) chain(handler http.Handler) http.Handler { return handler } -type chainHandler struct { +type ChainHandler struct { Endpoint http.Handler chain http.Handler Middlewares Middlewares } -func (c *chainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { +func (c *ChainHandler) Unwrap() any { + return c.Endpoint +} + +func (c *ChainHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { c.chain.ServeHTTP(w, r) } diff --git a/options.go b/options.go index 7b9758f..207cea0 100644 --- a/options.go +++ b/options.go @@ -91,14 +91,12 @@ func (options Options) TlsConfig() *tls.Config { } return &tls.Config{ - GetCertificate: options.GetCertificate, + GetCertificate: func(info *tls.ClientHelloInfo) (*tls.Certificate, error) { + cert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile) + if err != nil { + return nil, err + } + return &cert, nil + }, } } - -func (options Options) GetCertificate(info *tls.ClientHelloInfo) (*tls.Certificate, error) { - cert, err := tls.LoadX509KeyPair(options.CertFile, options.KeyFile) - if err != nil { - return nil, err - } - return &cert, nil -} diff --git a/router.go b/router.go index 299904c..812bbc2 100644 --- a/router.go +++ b/router.go @@ -54,7 +54,7 @@ import ( // http.ListenAndServe(":8080", router) // } type Router interface { - // Handler dispatches the handler registered in the matched route. + Routes http.Handler // Use appends a MiddlewareFunc to the chain. diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..d5ea8c2 --- /dev/null +++ b/server_test.go @@ -0,0 +1,15 @@ +package web + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNewServer(t *testing.T) { + svr := NewServer(Options{}) + assert.NotNil(t, svr) + assert.Equal(t, ":8080", svr.Addr()) + assert.Equal(t, false, svr.options.IsTls()) + assert.Nil(t, svr.options.TlsConfig()) +} diff --git a/tree.go b/tree.go index 7f4bd02..a721475 100644 --- a/tree.go +++ b/tree.go @@ -856,7 +856,7 @@ func walk(r Routes, walkFn WalkFunc, parentRoute string, parentMw ...func(http.H fullRoute := parentRoute + route.Pattern fullRoute = strings.Replace(fullRoute, "/*/", "/", -1) - if chain, ok := handler.(*chainHandler); ok { + if chain, ok := handler.(*ChainHandler); ok { if err := walkFn(method, fullRoute, chain.Endpoint, append(mws, chain.Middlewares...)...); err != nil { return err }