fix:http rehister url lack / (#1464)

* fix: set strictSlash true
pull/1465/head
seasrain 3 years ago committed by GitHub
parent 6e6526efd9
commit 798adbff5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      transport/http/server.go

@ -105,41 +105,52 @@ func TLSConfig(c *tls.Config) ServerOption {
} }
} }
// StrictSlash is with mux's StrictSlash
// If true, when the path pattern is "/path/", accessing "/path" will
// redirect to the former and vice versa.
func StrictSlash(strictSlash bool) ServerOption {
return func(o *Server) {
o.strictSlash = strictSlash
}
}
// Server is an HTTP server wrapper. // Server is an HTTP server wrapper.
type Server struct { type Server struct {
*http.Server *http.Server
lis net.Listener lis net.Listener
tlsConf *tls.Config tlsConf *tls.Config
once sync.Once once sync.Once
endpoint *url.URL endpoint *url.URL
err error err error
network string network string
address string address string
timeout time.Duration timeout time.Duration
filters []FilterFunc filters []FilterFunc
ms []middleware.Middleware ms []middleware.Middleware
dec DecodeRequestFunc dec DecodeRequestFunc
enc EncodeResponseFunc enc EncodeResponseFunc
ene EncodeErrorFunc ene EncodeErrorFunc
router *mux.Router strictSlash bool
log *log.Helper router *mux.Router
log *log.Helper
} }
// NewServer creates an HTTP server by options. // NewServer creates an HTTP server by options.
func NewServer(opts ...ServerOption) *Server { func NewServer(opts ...ServerOption) *Server {
srv := &Server{ srv := &Server{
network: "tcp", network: "tcp",
address: ":0", address: ":0",
timeout: 1 * time.Second, timeout: 1 * time.Second,
dec: DefaultRequestDecoder, dec: DefaultRequestDecoder,
enc: DefaultResponseEncoder, enc: DefaultResponseEncoder,
ene: DefaultErrorEncoder, ene: DefaultErrorEncoder,
log: log.NewHelper(log.DefaultLogger), strictSlash: true,
log: log.NewHelper(log.DefaultLogger),
} }
for _, o := range opts { for _, o := range opts {
o(srv) o(srv)
} }
srv.router = mux.NewRouter() srv.router = mux.NewRouter().StrictSlash(srv.strictSlash)
srv.router.Use(srv.filter()) srv.router.Use(srv.filter())
srv.Server = &http.Server{ srv.Server = &http.Server{
Handler: FilterChain(srv.filters...)(srv.router), Handler: FilterChain(srv.filters...)(srv.router),

Loading…
Cancel
Save