[Feature] add path prefix for http server (#2439)

* update http.NewServer
Add feature:option of mux router prefix path.

* remove nil check in
transport->http->PathPrefix option.

Co-authored-by: luozhihong <luozhihong@risenlighten.com>
pull/2458/head
ruohone 2 years ago committed by GitHub
parent 7a99e8bbdc
commit d82d607c21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      transport/http/server.go

@ -128,6 +128,13 @@ func Listener(lis net.Listener) ServerOption {
}
}
// PathPrefix with mux's PathPrefix, router will replaced by a subrouter that start with prefix.
func PathPrefix(prefix string) ServerOption {
return func(s *Server) {
s.router = s.router.PathPrefix(prefix).Subrouter()
}
}
// Server is an HTTP server wrapper.
type Server struct {
*http.Server
@ -162,11 +169,12 @@ func NewServer(opts ...ServerOption) *Server {
enc: DefaultResponseEncoder,
ene: DefaultErrorEncoder,
strictSlash: true,
router: mux.NewRouter(),
}
for _, o := range opts {
o(srv)
}
srv.router = mux.NewRouter().StrictSlash(srv.strictSlash)
srv.router.StrictSlash(srv.strictSlash)
srv.router.NotFoundHandler = http.DefaultServeMux
srv.router.MethodNotAllowedHandler = http.DefaultServeMux
srv.router.Use(srv.filter())

Loading…
Cancel
Save