export pathvar for user-defined routers (#911)

* refactor

* export pathvar for user-defined routers
master
Kevin Wan 3 years ago committed by GitHub
parent fbf2eebc42
commit fc04ad7854
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,7 +7,7 @@ import (
"strings" "strings"
"github.com/tal-tech/go-zero/core/mapping" "github.com/tal-tech/go-zero/core/mapping"
"github.com/tal-tech/go-zero/rest/internal/context" "github.com/tal-tech/go-zero/rest/pathvar"
) )
const ( const (
@ -119,7 +119,7 @@ func ParseJsonBody(r *http.Request, v interface{}) error {
// ParsePath parses the symbols reside in url path. // ParsePath parses the symbols reside in url path.
// Like http://localhost/bag/:name // Like http://localhost/bag/:name
func ParsePath(r *http.Request, v interface{}) error { func ParsePath(r *http.Request, v interface{}) error {
vars := context.Vars(r) vars := pathvar.Vars(r)
m := make(map[string]interface{}, len(vars)) m := make(map[string]interface{}, len(vars))
for k, v := range vars { for k, v := range vars {
m[k] = v m[k] = v

@ -1,4 +1,4 @@
package context package pathvar
import ( import (
"context" "context"
@ -17,13 +17,13 @@ func Vars(r *http.Request) map[string]string {
return nil return nil
} }
// WithPathVars writes params into given r and returns a new http.Request. // WithVars writes params into given r and returns a new http.Request.
func WithPathVars(r *http.Request, params map[string]string) *http.Request { func WithVars(r *http.Request, params map[string]string) *http.Request {
return r.WithContext(context.WithValue(r.Context(), pathVars, params)) return r.WithContext(context.WithValue(r.Context(), pathVars, params))
} }
type contextKey string type contextKey string
func (c contextKey) String() string { func (c contextKey) String() string {
return "rest/internal/context key: " + string(c) return "rest/pathvar/context key: " + string(c)
} }

@ -1,4 +1,4 @@
package context package pathvar
import ( import (
"context" "context"

@ -8,7 +8,7 @@ import (
"github.com/tal-tech/go-zero/core/search" "github.com/tal-tech/go-zero/core/search"
"github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal/context" "github.com/tal-tech/go-zero/rest/pathvar"
) )
const ( const (
@ -61,7 +61,7 @@ func (pr *patRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if tree, ok := pr.trees[r.Method]; ok { if tree, ok := pr.trees[r.Method]; ok {
if result, ok := tree.Search(reqPath); ok { if result, ok := tree.Search(reqPath); ok {
if len(result.Params) > 0 { if len(result.Params) > 0 {
r = context.WithPathVars(r, result.Params) r = pathvar.WithVars(r, result.Params)
} }
result.Item.(http.Handler).ServeHTTP(w, r) result.Item.(http.Handler).ServeHTTP(w, r)
return return

@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/rest/httpx" "github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal/context" "github.com/tal-tech/go-zero/rest/pathvar"
) )
const ( const (
@ -107,12 +107,12 @@ func TestPatRouter(t *testing.T) {
router := NewRouter() router := NewRouter()
err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err := router.Handle(test.method, "/a/:b", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routed = true routed = true
assert.Equal(t, 1, len(context.Vars(r))) assert.Equal(t, 1, len(pathvar.Vars(r)))
})) }))
assert.Nil(t, err) assert.Nil(t, err)
err = router.Handle(test.method, "/a/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err = router.Handle(test.method, "/a/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
routed = true routed = true
assert.Nil(t, context.Vars(r)) assert.Nil(t, pathvar.Vars(r))
})) }))
assert.Nil(t, err) assert.Nil(t, err)
err = router.Handle(test.method, "/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { err = router.Handle(test.method, "/b/c", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

@ -29,12 +29,12 @@ type (
// Be aware that later RunOption might overwrite previous one that write the same option. // Be aware that later RunOption might overwrite previous one that write the same option.
// The process will exit if error occurs. // The process will exit if error occurs.
func MustNewServer(c RestConf, opts ...RunOption) *Server { func MustNewServer(c RestConf, opts ...RunOption) *Server {
engine, err := NewServer(c, opts...) server, err := NewServer(c, opts...)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
return engine return server
} }
// NewServer returns a server with given config of c and options defined in opts. // NewServer returns a server with given config of c and options defined in opts.
@ -61,36 +61,36 @@ func NewServer(c RestConf, opts ...RunOption) (*Server, error) {
} }
// AddRoutes add given routes into the Server. // AddRoutes add given routes into the Server.
func (e *Server) AddRoutes(rs []Route, opts ...RouteOption) { func (s *Server) AddRoutes(rs []Route, opts ...RouteOption) {
r := featuredRoutes{ r := featuredRoutes{
routes: rs, routes: rs,
} }
for _, opt := range opts { for _, opt := range opts {
opt(&r) opt(&r)
} }
e.ngin.AddRoutes(r) s.ngin.AddRoutes(r)
} }
// AddRoute adds given route into the Server. // AddRoute adds given route into the Server.
func (e *Server) AddRoute(r Route, opts ...RouteOption) { func (s *Server) AddRoute(r Route, opts ...RouteOption) {
e.AddRoutes([]Route{r}, opts...) s.AddRoutes([]Route{r}, opts...)
} }
// Start starts the Server. // Start starts the Server.
// Graceful shutdown is enabled by default. // Graceful shutdown is enabled by default.
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period. // Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
func (e *Server) Start() { func (s *Server) Start() {
handleError(e.opts.start(e.ngin)) handleError(s.opts.start(s.ngin))
} }
// Stop stops the Server. // Stop stops the Server.
func (e *Server) Stop() { func (s *Server) Stop() {
logx.Close() logx.Close()
} }
// Use adds the given middleware in the Server. // Use adds the given middleware in the Server.
func (e *Server) Use(middleware Middleware) { func (s *Server) Use(middleware Middleware) {
e.ngin.use(middleware) s.ngin.use(middleware)
} }
// ToMiddleware converts the given handler to a Middleware. // ToMiddleware converts the given handler to a Middleware.

Loading…
Cancel
Save