Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked fromgorilla/mux

A powerful HTTP router and URL matcher for building Go web servers with 🦍

License

NotificationsYou must be signed in to change notification settings

amustaque97/mux

 
 

Repository files navigation

GoDocCircleCISourcegraph

Gorilla Logo


⚠️The Gorilla Toolkit is looking for a new maintainer


Packagegorilla/mux implements a request router and dispatcher for matching incoming requests totheir respective handler.

The name mux stands for "HTTP request multiplexer". Like the standardhttp.ServeMux,mux.Router matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:

  • It implements thehttp.Handler interface so it is compatible with the standardhttp.ServeMux.
  • Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
  • URL hosts, paths and query values can have variables with an optional regular expression.
  • Registered URLs can be built, or "reversed", which helps maintaining references to resources.
  • Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.


Install

With acorrectly configured Go toolchain:

go get -u github.com/gorilla/mux

Examples

Let's start registering a couple of URL paths and handlers:

funcmain() {r:=mux.NewRouter()r.HandleFunc("/",HomeHandler)r.HandleFunc("/products",ProductsHandler)r.HandleFunc("/articles",ArticlesHandler)http.Handle("/",r)}

Here we register three routes mapping URL paths to handlers. This is equivalent to howhttp.HandleFunc() works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (http.ResponseWriter,*http.Request) as parameters.

Paths can have variables. They are defined using the format{name} or{name:pattern}. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:

r:=mux.NewRouter()r.HandleFunc("/products/{key}",ProductHandler)r.HandleFunc("/articles/{category}/",ArticlesCategoryHandler)r.HandleFunc("/articles/{category}/{id:[0-9]+}",ArticleHandler)

The names are used to create a map of route variables which can be retrieved callingmux.Vars():

funcArticlesCategoryHandler(w http.ResponseWriter,r*http.Request) {vars:=mux.Vars(r)w.WriteHeader(http.StatusOK)fmt.Fprintf(w,"Category: %v\n",vars["category"])}

And this is all you need to know about the basic usage. More advanced options are explained below.

Matching Routes

Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:

r:=mux.NewRouter()// Only matches if domain is "www.example.com".r.Host("www.example.com")// Matches a dynamic subdomain.r.Host("{subdomain:[a-z]+}.example.com")

There are several other matchers that can be added. To match path prefixes:

r.PathPrefix("/products/")

...or HTTP methods:

r.Methods("GET","POST")

...or URL schemes:

r.Schemes("https")

...or header values:

r.Headers("X-Requested-With","XMLHttpRequest")

...or query values:

r.Queries("key","value")

...or to use a custom matcher function:

r.MatcherFunc(func(r*http.Request,rm*RouteMatch)bool {returnr.ProtoMajor==0})

...and finally, it is possible to combine several matchers in a single route:

r.HandleFunc("/products",ProductsHandler).Host("www.example.com").Methods("GET").Schemes("http")

Routes are tested in the order they were added to the router. If two routes match, the first one wins:

r:=mux.NewRouter()r.HandleFunc("/specific",specificHandler)r.PathPrefix("/").Handler(catchAllHandler)

Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".

For example, let's say we have several URLs that should only match when the host iswww.example.com. Create a route for that host and get a "subrouter" from it:

r:=mux.NewRouter()s:=r.Host("www.example.com").Subrouter()

Then register routes in the subrouter:

s.HandleFunc("/products/",ProductsHandler)s.HandleFunc("/products/{key}",ProductHandler)s.HandleFunc("/articles/{category}/{id:[0-9]+}",ArticleHandler)

The three URL paths we registered above will only be tested if the domain iswww.example.com, because the subrouter is tested first. This is not only convenient, but also optimizes request matching. You can create subrouters combining any attribute matchers accepted by a route.

Subrouters can be used to create domain or path "namespaces": you define subrouters in a central place and then parts of the app can register its paths relatively to a given subrouter.

There's one more thing about subroutes. When a subrouter has a path prefix, the inner routes use it as base for their paths:

r:=mux.NewRouter()s:=r.PathPrefix("/products").Subrouter()// "/products/"s.HandleFunc("/",ProductsHandler)// "/products/{key}/"s.HandleFunc("/{key}/",ProductHandler)// "/products/{key}/details"s.HandleFunc("/{key}/details",ProductDetailsHandler)

Static Files

Note that the path provided toPathPrefix() represents a "wildcard": callingPathPrefix("/static/").Handler(...) means that the handler will be passed anyrequest that matches "/static/*". This makes it easy to serve static files with mux:

funcmain() {vardirstringflag.StringVar(&dir,"dir",".","the directory to serve files from. Defaults to the current dir")flag.Parse()r:=mux.NewRouter()// This will serve files under http://localhost:8000/static/<filename>r.PathPrefix("/static/").Handler(http.StripPrefix("/static/",http.FileServer(http.Dir(dir))))srv:=&http.Server{Handler:r,Addr:"127.0.0.1:8000",// Good practice: enforce timeouts for servers you create!WriteTimeout:15*time.Second,ReadTimeout:15*time.Second,    }log.Fatal(srv.ListenAndServe())}

Serving Single Page Applications

Most of the time it makes sense to serve your SPA on a separate web server from your API,but sometimes it's desirable to serve them both from one place. It's possible to write a simplehandler for serving your SPA (for use with React Router'sBrowserRouter for example), and leveragemux's powerful routing for your API endpoints.

package mainimport ("encoding/json""log""net/http""os""path/filepath""time""github.com/gorilla/mux")// spaHandler implements the http.Handler interface, so we can use it// to respond to HTTP requests. The path to the static directory and// path to the index file within that static directory are used to// serve the SPA in the given static directory.typespaHandlerstruct {staticPathstringindexPathstring}// ServeHTTP inspects the URL path to locate a file within the static dir// on the SPA handler. If a file is found, it will be served. If not, the// file located at the index path on the SPA handler will be served. This// is suitable behavior for serving an SPA (single page application).func (hspaHandler)ServeHTTP(w http.ResponseWriter,r*http.Request) {// get the absolute path to prevent directory traversalpath,err:=filepath.Abs(r.URL.Path)iferr!=nil {// if we failed to get the absolute path respond with a 400 bad request// and stophttp.Error(w,err.Error(),http.StatusBadRequest)return}// prepend the path with the path to the static directorypath=filepath.Join(h.staticPath,path)// check whether a file exists at the given path_,err=os.Stat(path)ifos.IsNotExist(err) {// file does not exist, serve index.htmlhttp.ServeFile(w,r,filepath.Join(h.staticPath,h.indexPath))return}elseiferr!=nil {// if we got an error (that wasn't that the file doesn't exist) stating the// file, return a 500 internal server error and stophttp.Error(w,err.Error(),http.StatusInternalServerError)return}// otherwise, use http.FileServer to serve the static dirhttp.FileServer(http.Dir(h.staticPath)).ServeHTTP(w,r)}funcmain() {router:=mux.NewRouter()router.HandleFunc("/api/health",func(w http.ResponseWriter,r*http.Request) {// an example API handlerjson.NewEncoder(w).Encode(map[string]bool{"ok":true})})spa:=spaHandler{staticPath:"build",indexPath:"index.html"}router.PathPrefix("/").Handler(spa)srv:=&http.Server{Handler:router,Addr:"127.0.0.1:8000",// Good practice: enforce timeouts for servers you create!WriteTimeout:15*time.Second,ReadTimeout:15*time.Second,}log.Fatal(srv.ListenAndServe())}

Registered URLs

Now let's see how to build registered URLs.

Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name callingName() on a route. For example:

r:=mux.NewRouter()r.HandleFunc("/articles/{category}/{id:[0-9]+}",ArticleHandler).Name("article")

To build a URL, get the route and call theURL() method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:

url,err:=r.Get("article").URL("category","technology","id","42")

...and the result will be aurl.URL with the following path:

"/articles/technology/42"

This also works for host and query value variables:

r:=mux.NewRouter()r.Host("{subdomain}.example.com").Path("/articles/{category}/{id:[0-9]+}").Queries("filter","{filter}").HandlerFunc(ArticleHandler).Name("article")// url.String() will be "http://news.example.com/articles/technology/42?filter=gorilla"url,err:=r.Get("article").URL("subdomain","news","category","technology","id","42","filter","gorilla")

All variables defined in the route are required, and their values must conform to the corresponding patterns. These requirements guarantee that a generated URL will always match a registered route -- the only exception is for explicitly defined "build-only" routes which never match.

Regex support also exists for matching Headers within a route. For example, we could do:

r.HeadersRegexp("Content-Type","application/(text|json)")

...and the route will match both requests with a Content-Type ofapplication/json as well asapplication/text

There's also a way to build only the URL host or path for a route: use the methodsURLHost() orURLPath() instead. For the previous route, we would do:

// "http://news.example.com/"host,err:=r.Get("article").URLHost("subdomain","news")// "/articles/technology/42"path,err:=r.Get("article").URLPath("category","technology","id","42")

And if you use subrouters, host and path defined separately can be built as well:

r:=mux.NewRouter()s:=r.Host("{subdomain}.example.com").Subrouter()s.Path("/articles/{category}/{id:[0-9]+}").HandlerFunc(ArticleHandler).Name("article")// "http://news.example.com/articles/technology/42"url,err:=r.Get("article").URL("subdomain","news","category","technology","id","42")

Walking Routes

TheWalk function onmux.Router can be used to visit all of the routes that are registered on a router. For example,the following prints all of the registered routes:

package mainimport ("fmt""net/http""strings""github.com/gorilla/mux")funchandler(w http.ResponseWriter,r*http.Request) {return}funcmain() {r:=mux.NewRouter()r.HandleFunc("/",handler)r.HandleFunc("/products",handler).Methods("POST")r.HandleFunc("/articles",handler).Methods("GET")r.HandleFunc("/articles/{id}",handler).Methods("GET","PUT")r.HandleFunc("/authors",handler).Queries("surname","{surname}")err:=r.Walk(func(route*mux.Route,router*mux.Router,ancestors []*mux.Route)error {pathTemplate,err:=route.GetPathTemplate()iferr==nil {fmt.Println("ROUTE:",pathTemplate)}pathRegexp,err:=route.GetPathRegexp()iferr==nil {fmt.Println("Path regexp:",pathRegexp)}queriesTemplates,err:=route.GetQueriesTemplates()iferr==nil {fmt.Println("Queries templates:",strings.Join(queriesTemplates,","))}queriesRegexps,err:=route.GetQueriesRegexp()iferr==nil {fmt.Println("Queries regexps:",strings.Join(queriesRegexps,","))}methods,err:=route.GetMethods()iferr==nil {fmt.Println("Methods:",strings.Join(methods,","))}fmt.Println()returnnil})iferr!=nil {fmt.Println(err)}http.Handle("/",r)}

Graceful Shutdown

Go 1.8 introduced the ability togracefully shutdown a*http.Server. Here's how to do that alongsidemux:

package mainimport ("context""flag""log""net/http""os""os/signal""time""github.com/gorilla/mux")funcmain() {varwait time.Durationflag.DurationVar(&wait,"graceful-timeout",time.Second*15,"the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")flag.Parse()r:=mux.NewRouter()// Add your routes as neededsrv:=&http.Server{Addr:"0.0.0.0:8080",// Good practice to set timeouts to avoid Slowloris attacks.WriteTimeout:time.Second*15,ReadTimeout:time.Second*15,IdleTimeout:time.Second*60,Handler:r,// Pass our instance of gorilla/mux in.    }// Run our server in a goroutine so that it doesn't block.gofunc() {iferr:=srv.ListenAndServe();err!=nil {log.Println(err)        }    }()c:=make(chan os.Signal,1)// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.signal.Notify(c,os.Interrupt)// Block until we receive our signal.<-c// Create a deadline to wait for.ctx,cancel:=context.WithTimeout(context.Background(),wait)defercancel()// Doesn't block if no connections, but will otherwise wait// until the timeout deadline.srv.Shutdown(ctx)// Optionally, you could run srv.Shutdown in a goroutine and block on// <-ctx.Done() if your application should wait for other services// to finalize based on context cancellation.log.Println("shutting down")os.Exit(0)}

Middleware

Mux supports the addition of middlewares to aRouter, which are executed in the order they are added if a match is found, including its subrouters.Middlewares are (typically) small pieces of code which take one request, do something with it, and pass it down to another middleware or the final handler. Some common use cases for middleware are request logging, header manipulation, orResponseWriter hijacking.

Mux middlewares are defined using the de facto standard type:

typeMiddlewareFuncfunc(http.Handler) http.Handler

Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc. This takes advantage of closures being able access variables from the context where they are created, while retaining the signature enforced by the receivers.

A very basic middleware which logs the URI of the request being handled could be written as:

funcloggingMiddleware(next http.Handler) http.Handler {returnhttp.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {// Do stuff herelog.Println(r.RequestURI)// Call the next handler, which can be another middleware in the chain, or the final handler.next.ServeHTTP(w,r)    })}

Middlewares can be added to a router usingRouter.Use():

r:=mux.NewRouter()r.HandleFunc("/",handler)r.Use(loggingMiddleware)

A more complex authentication middleware, which maps session token to users, could be written as:

// Define our structtypeauthenticationMiddlewarestruct {tokenUsersmap[string]string}// Initialize it somewherefunc (amw*authenticationMiddleware)Populate() {amw.tokenUsers["00000000"]="user0"amw.tokenUsers["aaaaaaaa"]="userA"amw.tokenUsers["05f717e5"]="randomUser"amw.tokenUsers["deadbeef"]="user0"}// Middleware function, which will be called for each requestfunc (amw*authenticationMiddleware)Middleware(next http.Handler) http.Handler {returnhttp.HandlerFunc(func(w http.ResponseWriter,r*http.Request) {token:=r.Header.Get("X-Session-Token")ifuser,found:=amw.tokenUsers[token];found {// We found the token in our maplog.Printf("Authenticated user %s\n",user)// Pass down the request to the next middleware (or final handler)next.ServeHTTP(w,r)        }else {// Write an error and stop the handler chainhttp.Error(w,"Forbidden",http.StatusForbidden)        }    })}
r:=mux.NewRouter()r.HandleFunc("/",handler)amw:=authenticationMiddleware{}amw.Populate()r.Use(amw.Middleware)

Note: The handler chain will be stopped if your middleware doesn't callnext.ServeHTTP() with the corresponding parameters. This can be used to abort a request if the middleware writer wants to. Middlewaresshould write toResponseWriter if theyare going to terminate the request, and theyshould not write toResponseWriter if theyare not going to terminate it.

Handling CORS Requests

CORSMethodMiddleware intends to make it easier to strictly set theAccess-Control-Allow-Methods response header.

  • You will still need to use your own CORS handler to set the other CORS headers such asAccess-Control-Allow-Origin
  • The middleware will set theAccess-Control-Allow-Methods header to all the method matchers (e.g.r.Methods(http.MethodGet, http.MethodPut, http.MethodOptions) ->Access-Control-Allow-Methods: GET,PUT,OPTIONS) on a route
  • If you do not specify any methods, then:

Important: there must be anOPTIONS method matcher for the middleware to set the headers.

Here is an example of usingCORSMethodMiddleware along with a customOPTIONS handler to set all the required CORS headers:

package mainimport ("net/http""github.com/gorilla/mux")funcmain() {r:=mux.NewRouter()// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headersr.HandleFunc("/foo",fooHandler).Methods(http.MethodGet,http.MethodPut,http.MethodPatch,http.MethodOptions)r.Use(mux.CORSMethodMiddleware(r))http.ListenAndServe(":8080",r)}funcfooHandler(w http.ResponseWriter,r*http.Request) {w.Header().Set("Access-Control-Allow-Origin","*")ifr.Method==http.MethodOptions {return    }w.Write([]byte("foo"))}

And an request to/foo using something like:

curl localhost:8080/foo -v

Would look like:

*   Trying ::1...* TCP_NODELAYset* Connected to localhost (::1) port 8080 (#0)> GET /foo HTTP/1.1> Host: localhost:8080> User-Agent: curl/7.59.0> Accept:*/*>< HTTP/1.1 200 OK< Access-Control-Allow-Methods: GET,PUT,PATCH,OPTIONS< Access-Control-Allow-Origin:*< Date: Fri, 28 Jun 2019 20:13:30 GMT< Content-Length: 3< Content-Type: text/plain; charset=utf-8<* Connection#0 to host localhost left intactfoo

Testing Handlers

Testing handlers in a Go web application is straightforward, andmux doesn't complicate this any further. Given two files:endpoints.go andendpoints_test.go, here's how we'd test an application usingmux.

First, our simple HTTP handler:

// endpoints.gopackage mainfuncHealthCheckHandler(w http.ResponseWriter,r*http.Request) {// A very simple health check.w.Header().Set("Content-Type","application/json")w.WriteHeader(http.StatusOK)// In the future we could report back on the status of our DB, or our cache// (e.g. Redis) by performing a simple PING, and include them in the response.io.WriteString(w,`{"alive": true}`)}funcmain() {r:=mux.NewRouter()r.HandleFunc("/health",HealthCheckHandler)log.Fatal(http.ListenAndServe("localhost:8080",r))}

Our test code:

// endpoints_test.gopackage mainimport ("net/http""net/http/httptest""testing")funcTestHealthCheckHandler(t*testing.T) {// Create a request to pass to our handler. We don't have any query parameters for now, so we'll// pass 'nil' as the third parameter.req,err:=http.NewRequest("GET","/health",nil)iferr!=nil {t.Fatal(err)    }// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.rr:=httptest.NewRecorder()handler:=http.HandlerFunc(HealthCheckHandler)// Our handlers satisfy http.Handler, so we can call their ServeHTTP method// directly and pass in our Request and ResponseRecorder.handler.ServeHTTP(rr,req)// Check the status code is what we expect.ifstatus:=rr.Code;status!=http.StatusOK {t.Errorf("handler returned wrong status code: got %v want %v",status,http.StatusOK)    }// Check the response body is what we expect.expected:=`{"alive": true}`ifrr.Body.String()!=expected {t.Errorf("handler returned unexpected body: got %v want %v",rr.Body.String(),expected)    }}

In the case that our routes havevariables, we can pass those in the request. We could writetable-driven tests to test multiplepossible route variables as needed.

// endpoints.gofuncmain() {r:=mux.NewRouter()// A route with a route variable:r.HandleFunc("/metrics/{type}",MetricsHandler)log.Fatal(http.ListenAndServe("localhost:8080",r))}

Our test file, with a table-driven test ofrouteVariables:

// endpoints_test.gofuncTestMetricsHandler(t*testing.T) {tt:= []struct{routeVariablestringshouldPassbool    }{        {"goroutines",true},        {"heap",true},        {"counters",true},        {"queries",true},        {"adhadaeqm3k",false},    }for_,tc:=rangett {path:=fmt.Sprintf("/metrics/%s",tc.routeVariable)req,err:=http.NewRequest("GET",path,nil)iferr!=nil {t.Fatal(err)        }rr:=httptest.NewRecorder()// Need to create a router that we can pass the request through so that the vars will be added to the contextrouter:=mux.NewRouter()router.HandleFunc("/metrics/{type}",MetricsHandler)router.ServeHTTP(rr,req)// In this case, our MetricsHandler returns a non-200 response// for a route variable it doesn't know about.ifrr.Code==http.StatusOK&&!tc.shouldPass {t.Errorf("handler should have failed on routeVariable %s: got %v want %v",tc.routeVariable,rr.Code,http.StatusOK)        }    }}

Full Example

Here's a complete, runnable example of a smallmux based server:

package mainimport ("net/http""log""github.com/gorilla/mux")funcYourHandler(w http.ResponseWriter,r*http.Request) {w.Write([]byte("Gorilla!\n"))}funcmain() {r:=mux.NewRouter()// Routes consist of a path and a handler function.r.HandleFunc("/",YourHandler)// Bind to a port and pass our router inlog.Fatal(http.ListenAndServe(":8000",r))}

License

BSD licensed. See the LICENSE file for details.

About

A powerful HTTP router and URL matcher for building Go web servers with 🦍

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go100.0%

[8]ページ先頭

©2009-2025 Movatter.jp