123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- package myth
- import (
- "net/http"
- )
- type Handle func(http.ResponseWriter, *http.Request)
- type Router struct {
- trees map[string]*node
-
-
-
-
-
- RedirectTrailingSlash bool
-
-
-
-
-
-
-
-
-
- RedirectFixedPath bool
-
-
-
-
-
-
- HandleMethodNotAllowed bool
-
-
- HandleOPTIONS bool
-
-
- NotFound http.Handler
-
-
-
-
-
- MethodNotAllowed http.Handler
-
-
-
-
-
- PanicHandler func(http.ResponseWriter, *http.Request, interface{})
- }
- var _ http.Handler = NewRouter()
- func NewRouter() *Router {
- return &Router{
- RedirectTrailingSlash: true,
- RedirectFixedPath: true,
- HandleMethodNotAllowed: true,
- HandleOPTIONS: true,
- }
- }
- func (r *Router) GET(path string, handle Handle) {
- r.Handle(http.MethodGet, path, handle)
- }
- func (r *Router) HEAD(path string, handle Handle) {
- r.Handle(http.MethodHead, path, handle)
- }
- func (r *Router) OPTIONS(path string, handle Handle) {
- r.Handle(http.MethodOptions, path, handle)
- }
- func (r *Router) POST(path string, handle Handle) {
- r.Handle(http.MethodPost, path, handle)
- }
- func (r *Router) PUT(path string, handle Handle) {
- r.Handle(http.MethodPut, path, handle)
- }
- func (r *Router) PATCH(path string, handle Handle) {
- r.Handle(http.MethodPatch, path, handle)
- }
- func (r *Router) DELETE(path string, handle Handle) {
- r.Handle(http.MethodDelete, path, handle)
- }
- func (r *Router) Handle(method, path string, handle Handle) {
- if path[0] != '/' {
- panic("path must begin with '/' in path '" + path + "'")
- }
- if r.trees == nil {
- r.trees = make(map[string]*node)
- }
- root := r.trees[method]
- if root == nil {
- root = new(node)
- r.trees[method] = root
- }
- root.addRoute(path, handle)
- }
- func (r *Router) Handler(method, path string, handler http.Handler) {
- r.Handle(method, path,
- func(w http.ResponseWriter, req *http.Request) {
- handler.ServeHTTP(w, req)
- },
- )
- }
- func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) {
- r.Handler(method, path, handler)
- }
- func (r *Router) ServeFiles(path string, root http.FileSystem) {
- if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
- panic("path must end with /*filepath in path '" + path + "'")
- }
- fileServer := http.FileServer(root)
- r.GET(path, func(w http.ResponseWriter, req *http.Request) {
- if vars := Vars(req); vars != nil {
- path, _ := vars["filepath"]
- req.URL.Path = path
- }
- fileServer.ServeHTTP(w, req)
- })
- }
- func (r *Router) recv(w http.ResponseWriter, req *http.Request) {
- if rcv := recover(); rcv != nil {
- r.PanicHandler(w, req, rcv)
- }
- }
- func (r *Router) Lookup(method, path string) (Handle, map[string]string, bool) {
- if root := r.trees[method]; root != nil {
- return root.getValue(path)
- }
- return nil, nil, false
- }
- func (r *Router) allowed(path, reqMethod string) (allow string) {
- if path == "*" {
- for method := range r.trees {
- if method == http.MethodOptions {
- continue
- }
-
- if len(allow) == 0 {
- allow = method
- } else {
- allow += ", " + method
- }
- }
- } else {
- for method := range r.trees {
-
- if method == reqMethod || method == http.MethodOptions {
- continue
- }
- handle, _, _ := r.trees[method].getValue(path)
- if handle != nil {
-
- if len(allow) == 0 {
- allow = method
- } else {
- allow += ", " + method
- }
- }
- }
- }
- if len(allow) > 0 {
- allow += ", OPTIONS"
- }
- return
- }
- func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
- if r.PanicHandler != nil {
- defer r.recv(w, req)
- }
- path := req.URL.Path
- if root := r.trees[req.Method]; root != nil {
- if handle, ps, tsr := root.getValue(path); handle != nil {
- req = setVars(req, ps)
- req = setCurrentPath(req, path)
- handle(w, req)
- return
- } else if req.Method != http.MethodConnect && path != "/" {
- code := 301
- if req.Method != http.MethodGet {
-
-
- code = 307
- }
- if tsr && r.RedirectTrailingSlash {
- if len(path) > 1 && path[len(path)-1] == '/' {
- req.URL.Path = path[:len(path)-1]
- } else {
- req.URL.Path = path + "/"
- }
- http.Redirect(w, req, req.URL.String(), code)
- return
- }
-
- if r.RedirectFixedPath {
- fixedPath, found := root.findCaseInsensitivePath(
- CleanPath(path),
- r.RedirectTrailingSlash,
- )
- if found {
- req.URL.Path = string(fixedPath)
- http.Redirect(w, req, req.URL.String(), code)
- return
- }
- }
- }
- }
- if req.Method == http.MethodOptions && r.HandleOPTIONS {
-
- if allow := r.allowed(path, req.Method); len(allow) > 0 {
- w.Header().Set("Allow", allow)
- return
- }
- } else {
-
- if r.HandleMethodNotAllowed {
- if allow := r.allowed(path, req.Method); len(allow) > 0 {
- w.Header().Set("Allow", allow)
- if r.MethodNotAllowed != nil {
- r.MethodNotAllowed.ServeHTTP(w, req)
- } else {
- http.Error(w,
- http.StatusText(http.StatusMethodNotAllowed),
- http.StatusMethodNotAllowed,
- )
- }
- return
- }
- }
- }
-
- if r.NotFound != nil {
- r.NotFound.ServeHTTP(w, req)
- } else {
- http.NotFound(w, req)
- }
- }
|