You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-zero/rest/router/patrouter.go

125 lines
2.6 KiB
Go

4 years ago
package router
4 years ago
import (
"errors"
4 years ago
"net/http"
"path"
"strings"
"github.com/tal-tech/go-zero/core/search"
"github.com/tal-tech/go-zero/rest/httpx"
"github.com/tal-tech/go-zero/rest/internal/context"
4 years ago
)
const (
allowHeader = "Allow"
allowMethodSeparator = ", "
)
var (
ErrInvalidMethod = errors.New("not a valid http method")
ErrInvalidPath = errors.New("path must begin with '/'")
)
type patRouter struct {
trees map[string]*search.Tree
notFound http.Handler
notAllowed http.Handler
4 years ago
}
func NewRouter() httpx.Router {
return &patRouter{
4 years ago
trees: make(map[string]*search.Tree),
}
}
func (pr *patRouter) Handle(method, reqPath string, handler http.Handler) error {
4 years ago
if !validMethod(method) {
return ErrInvalidMethod
}
if len(reqPath) == 0 || reqPath[0] != '/' {
return ErrInvalidPath
}
cleanPath := path.Clean(reqPath)
tree, ok := pr.trees[method]
if ok {
4 years ago
return tree.Add(cleanPath, handler)
}
tree = search.NewTree()
pr.trees[method] = tree
return tree.Add(cleanPath, handler)
4 years ago
}
func (pr *patRouter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
4 years ago
reqPath := path.Clean(r.URL.Path)
if tree, ok := pr.trees[r.Method]; ok {
if result, ok := tree.Search(reqPath); ok {
if len(result.Params) > 0 {
4 years ago
r = context.WithPathVars(r, result.Params)
4 years ago
}
result.Item.(http.Handler).ServeHTTP(w, r)
return
}
}
allows, ok := pr.methodsAllowed(r.Method, reqPath)
if !ok {
pr.handleNotFound(w, r)
return
}
if pr.notAllowed != nil {
pr.notAllowed.ServeHTTP(w, r)
} else {
w.Header().Set(allowHeader, allows)
4 years ago
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func (pr *patRouter) SetNotFoundHandler(handler http.Handler) {
4 years ago
pr.notFound = handler
}
func (pr *patRouter) SetNotAllowedHandler(handler http.Handler) {
pr.notAllowed = handler
}
func (pr *patRouter) handleNotFound(w http.ResponseWriter, r *http.Request) {
4 years ago
if pr.notFound != nil {
pr.notFound.ServeHTTP(w, r)
} else {
http.NotFound(w, r)
}
}
func (pr *patRouter) methodsAllowed(method, path string) (string, bool) {
4 years ago
var allows []string
for treeMethod, tree := range pr.trees {
if treeMethod == method {
continue
}
_, ok := tree.Search(path)
if ok {
allows = append(allows, treeMethod)
}
}
if len(allows) > 0 {
return strings.Join(allows, allowMethodSeparator), true
}
return "", false
4 years ago
}
func validMethod(method string) bool {
return method == http.MethodDelete || method == http.MethodGet ||
method == http.MethodHead || method == http.MethodOptions ||
method == http.MethodPatch || method == http.MethodPost ||
method == http.MethodPut
}