Fast and flexible HTTP router

All-in-one tool to optimize performance and monitor errors & logs

treemux - fast and flexible HTTP router

build workflow PkgGoDev Chat

High-speed, flexible, tree-based HTTP router for Go. It is as fast as httprouter, but with relaxed routing rules.

Benchmark results
#GithubAPI Routes: 203
   GorillaMux: 1319632 Bytes
   HttpRouter: 37088 Bytes
   VmihailencoTreemux: 55056 Bytes

#GPlusAPI Routes: 13
   GorillaMux: 66016 Bytes
   HttpRouter: 2760 Bytes
   VmihailencoTreemux: 5392 Bytes

#ParseAPI Routes: 26
   GorillaMux: 105448 Bytes
   HttpRouter: 5024 Bytes
   VmihailencoTreemux: 5472 Bytes

#Static Routes: 157
   GorillaMux: 582520 Bytes
   HttpRouter: 21680 Bytes
   VmihailencoTreemux: 46768 Bytes

goos: linux
goarch: amd64
pkg: github.com/julienschmidt/go-http-routing-benchmark
cpu: AMD Ryzen 5 2600 Six-Core Processor
BenchmarkGorillaMux_Param                	  482310	      2466 ns/op	    1312 B/op	      10 allocs/op
BenchmarkHttpRouter_Param                	11034873	        98.17 ns/op	      32 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_Param        	 7469511	       159.0 ns/op	      32 B/op	       1 allocs/op
BenchmarkGorillaMux_Param5               	  315780	      3630 ns/op	    1376 B/op	      10 allocs/op
BenchmarkHttpRouter_Param5               	 4141971	       284.0 ns/op	     160 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_Param5       	 2814357	       433.4 ns/op	     160 B/op	       1 allocs/op
BenchmarkGorillaMux_Param20              	  153157	      7711 ns/op	    3483 B/op	      12 allocs/op
BenchmarkHttpRouter_Param20              	 1311662	       902.1 ns/op	     640 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_Param20      	  939973	      1449 ns/op	     640 B/op	       1 allocs/op
BenchmarkGorillaMux_ParamWrite           	  444409	      2533 ns/op	    1312 B/op	      10 allocs/op
BenchmarkHttpRouter_ParamWrite           	 8234792	       145.1 ns/op	      32 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_ParamWrite   	 6130166	       191.2 ns/op	      32 B/op	       1 allocs/op
BenchmarkGorillaMux_GithubStatic         	  221184	      5321 ns/op	    1008 B/op	       9 allocs/op
BenchmarkHttpRouter_GithubStatic         	27660388	        43.66 ns/op	       0 B/op	       0 allocs/op
BenchmarkVmihailencoTreemux_GithubStatic 	21185312	        56.69 ns/op	       0 B/op	       0 allocs/op
BenchmarkGorillaMux_GithubParam          	  148010	      8032 ns/op	    1328 B/op	      10 allocs/op
BenchmarkHttpRouter_GithubParam          	 4974057	       237.6 ns/op	      96 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_GithubParam  	 3405594	       351.2 ns/op	      64 B/op	       1 allocs/op
BenchmarkGorillaMux_GithubAll            	     319	   3641622 ns/op	  258152 B/op	    1994 allocs/op
BenchmarkHttpRouter_GithubAll            	   26018	     47075 ns/op	   13792 B/op	     167 allocs/op
BenchmarkVmihailencoTreemux_GithubAll    	   19554	     63180 ns/op	   10848 B/op	     167 allocs/op
BenchmarkGorillaMux_GPlusStatic          	  802489	      1811 ns/op	    1008 B/op	       9 allocs/op
BenchmarkHttpRouter_GPlusStatic          	38865102	        31.89 ns/op	       0 B/op	       0 allocs/op
BenchmarkVmihailencoTreemux_GPlusStatic  	41022703	        29.31 ns/op	       0 B/op	       0 allocs/op
BenchmarkGorillaMux_GPlusParam           	  356788	      3449 ns/op	    1312 B/op	      10 allocs/op
BenchmarkHttpRouter_GPlusParam           	 6561582	       164.7 ns/op	      64 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_GPlusParam   	 6196903	       193.5 ns/op	      32 B/op	       1 allocs/op
BenchmarkGorillaMux_GPlus2Params         	  201336	      6237 ns/op	    1328 B/op	      10 allocs/op
BenchmarkHttpRouter_GPlus2Params         	 5670499	       197.0 ns/op	      64 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_GPlus2Params 	 3671635	       337.5 ns/op	      64 B/op	       1 allocs/op
BenchmarkGorillaMux_GPlusAll             	   24546	     50849 ns/op	   16528 B/op	     128 allocs/op
BenchmarkHttpRouter_GPlusAll             	  616034	      2250 ns/op	     640 B/op	      11 allocs/op
BenchmarkVmihailencoTreemux_GPlusAll     	  441236	      3026 ns/op	     512 B/op	      11 allocs/op
BenchmarkGorillaMux_ParseStatic          	  632019	      2185 ns/op	    1008 B/op	       9 allocs/op
BenchmarkHttpRouter_ParseStatic          	44729970	        27.70 ns/op	       0 B/op	       0 allocs/op
BenchmarkVmihailencoTreemux_ParseStatic  	22529782	        54.80 ns/op	       0 B/op	       0 allocs/op
BenchmarkGorillaMux_ParseParam           	  498606	      2423 ns/op	    1312 B/op	      10 allocs/op
BenchmarkHttpRouter_ParseParam           	 7737769	       145.6 ns/op	      64 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_ParseParam   	 7123089	       168.0 ns/op	      32 B/op	       1 allocs/op
BenchmarkGorillaMux_Parse2Params         	  419157	      2924 ns/op	    1328 B/op	      10 allocs/op
BenchmarkHttpRouter_Parse2Params         	 7583847	       165.6 ns/op	      64 B/op	       1 allocs/op
BenchmarkVmihailencoTreemux_Parse2Params 	 4374222	       272.1 ns/op	      64 B/op	       1 allocs/op
BenchmarkGorillaMux_ParseAll             	   12295	     99150 ns/op	   31120 B/op	     250 allocs/op
BenchmarkHttpRouter_ParseAll             	  388022	      3227 ns/op	     640 B/op	      16 allocs/op
BenchmarkVmihailencoTreemux_ParseAll     	  235298	      4937 ns/op	     608 B/op	      16 allocs/op
BenchmarkGorillaMux_StaticAll            	    1122	    967515 ns/op	  158261 B/op	    1413 allocs/op
BenchmarkHttpRouter_StaticAll            	   88946	     12589 ns/op	       0 B/op	       0 allocs/op
BenchmarkVmihailencoTreemux_StaticAll    	   96432	     12494 ns/op	       0 B/op	       0 allocs/op

Installing with Go Modules

When using Go Modules, import this repository with import "github.com/vmihailenco/treemux" to ensure that you get the right version.

Handler

The handler is a simple function with the prototype func(w http.ResponseWriter, req treemux.Request) error. A treemux.Request contains route name and parameters parsed from wildcards and catch-alls in the URL. This type is aliased as treemux.HandlerFunc.

import "github.com/vmihailenco/treemux"

router := treemux.New()

group := router.NewGroup("/api/v1")

group.GET("/:id", func(w http.ResponseWriter, req treemux.Request) error {
  id := req.Param("id")
  return treemux.JSON(w, treemux.H{
      "url": fmt.Sprintf("GET /api/v1/%s", id),
      "route": req.Route(),
  })
})

log.Println(http.ListenAndServe(":8080", router))

Why not http.HandlerFunc?

treemux.HandlerFunc is a thin wrapper over http.HandlerFunc:

  • treemux.Request replaces *http.Request. You can get the original request via req.Request.
  • Handlers return errors just like other Go functions.

Those 2 tiny changes bring us:

  • Shorter and simpler error handling. In your handlers you just return the error and deal with it in a middleware in centralized fashion.
  • Easier debugging. Since middlewares have access to errors you can log errors along with other debugging information. OpenTelemetry integration uses that to record the error.
  • Route name and params. *http.Request was not designed to carry the route name and params. You can store that information in the request context.Context, but that clones the request and therefore requires an allocation
  • Effeciency. treemux.Request is designed so req.WithContext(ctx) does not allocate.

Treemux comes with middlewares that handle gzip compression, CORS, OpenTelemetry integration, and request logging. So with minimal changes you can make treemux work nicely with existing libraries.

Converting http.HandlerFunc to treemux.HandlerFunc

treemux provides helpers to convert existing http.HandlerFunc and http.Handler into treemux.HandlerFunc:

// http.HandlerFunc -> treemux.HandlerFunc
router.GET("/foo", treemux.HTTPHandlerFunc(existingHandlerFunc))

// http.Handler -> treemux.HandlerFunc
router.GET("/bar", treemux.HTTPHandler(existingHandler))

Middlewares

Middleware is a function that wraps a handler with another function:

func corsMiddleware(next treemux.HandlerFunc) treemux.HandlerFunc {
    return func(w http.ResponseWriter, req treemux.Request) error {
        if origin := req.Header.Get("Origin"); origin != "" {
            h := w.Header()
            h.Set("Access-Control-Allow-Origin", origin)
            h.Set("Access-Control-Allow-Credentials", "true")
        }
        return next(w, req)
    }
}

router := treemux.New(treemux.WithMiddleware(corsMiddleware))

Middlewares are also used for error handling.

Routing Rules

The syntax here is modeled after httprouter. Each variable in a path may match on one segment only, except for an optional catch-all variable at the end of the URL.

Some examples of valid URL patterns are:

  • /post/all
  • /post/:postid
  • /post/:postid/page/:page
  • /post/:postid/:page
  • /images/*path
  • /favicon.ico
  • /:year/:month/
  • /:year/:month/:post
  • /:page

Note that all of the above URL patterns may exist concurrently in the router.

Path elements starting with : indicate a wildcard in the path. A wildcard will only match on a single path segment. That is, the pattern /post/:postid will match on /post/1 or /post/1/, but not /post/1/2.

A path element starting with * is a catch-all, whose value will be a string containing all text in the URL matched by the wildcards. For example, with a pattern of /images/*path and a requested URL images/abc/def, path would contain abc/def. A catch-all path will not match an empty string, so in this example a separate route would need to be installed if you also want to match /images/.

Using : and * in routing patterns

The characters : and * can be used at the beginning of a path segment by escaping them with a backslash. A double backslash at the beginning of a segment is interpreted as a single backslash. These escapes are only checked at the very beginning of a path segment; they are not necessary or processed elsewhere in a token.

router.GET("/foo/\\*starToken", handler) // matches /foo/*starToken
router.GET("/foo/star*inTheMiddle", handler) // matches /foo/star*inTheMiddle
router.GET("/foo/starBackslash\\*", handler) // matches /foo/starBackslash\*
router.GET("/foo/\\\\*backslashWithStar") // matches /foo/\*backslashWithStar

Routing Groups

Lets you create a new group of routes with a given path prefix. Makes it easier to create clusters of paths like:

  • /api/v1/foo
  • /api/v1/bar

To use this you do:

router = treemux.New()

api := router.NewGroup("/api/v1")
api.GET("/foo", fooHandler) // becomes /api/v1/foo
api.GET("/bar", barHandler) // becomes /api/v1/bar

Or using WithGroup:

router.WithGroup("/api/v1", func(g *treemux.Group) {
    g.GET("/foo", fooHandler) // becomes /api/v1/foo
    g.GET("/bar", barHandler) // becomes /api/v1/bar
})

More complex example:

router := treemux.New()

g := router.NewGroup("/api/v1", treemux.WithMiddleware(ipRateLimitMiddleware))

g.NewGroup("/users/:user_id",
    treemux.WithMiddleware(authMiddleware),
    treemux.WithGroup(func(g *treemux.Group) {
        g.GET("", userHandler)

        g = g.WithMiddleware(adminMiddleware)

        g.PUT("", updateUserHandler)
        g.DELETE("", deleteUserHandler)
    }))

g.NewGroup("/projects/:project_id/articles/:article_id",
    treemux.WithMiddleware(authMiddleware),
    treemux.WithMiddleware(projectMiddleware),
    treemux.WithGroup(func(g *treemux.Group) {
        g.GET("", articleHandler)

        g.Use(quotaMiddleware)

        g.POST("", createArticleHandler)
        g.PUT("", updateArticleHandler)
        g.DELETE("", deleteArticleHandler)
    }))

Routing Priority

The priority rules in the router are simple.

  1. Static path segments take the highest priority. If a segment and its subtree are able to match the URL, that match is returned.
  2. Wildcards take second priority. For a particular wildcard to match, that wildcard and its subtree must match the URL.
  3. Finally, a catch-all rule will match when the earlier path segments have matched, and none of the static or wildcard conditions have matched. Catch-all rules must be at the end of a pattern.

So with the following patterns adapted from simpleblog, we'll see certain matches:

router = treemux.New()
router.GET("/:page", pageHandler)
router.GET("/:year/:month/:post", postHandler)
router.GET("/:year/:month", archiveHandler)
router.GET("/images/*path", staticHandler)
router.GET("/favicon.ico", staticHandler)

Example scenarios

  • /abc will match /:page
  • /2014/05 will match /:year/:month
  • /2014/05/really-great-blog-post will match /:year/:month/:post
  • /images/CoolImage.gif will match /images/*path
  • /images/2014/05/MayImage.jpg will also match /images/*path, with all the text after /images stored in the variable path.
  • /favicon.ico will match /favicon.ico

Special Method Behavior

If TreeMux.HeadCanUseGet is set to true, the router will call the GET handler for a pattern when a HEAD request is processed, if no HEAD handler has been added for that pattern. This behavior is enabled by default.

Go's http.ServeContent and related functions already handle the HEAD method correctly by sending only the header, so in most cases your handlers will not need any special cases for it.

Trailing Slashes

The router has special handling for paths with trailing slashes. If a pattern is added to the router with a trailing slash, any matches on that pattern without a trailing slash will be redirected to the version with the slash. If a pattern does not have a trailing slash, matches on that pattern with a trailing slash will be redirected to the version without.

The trailing slash flag is only stored once for a pattern. That is, if a pattern is added for a method with a trailing slash, all other methods for that pattern will also be considered to have a trailing slash, regardless of whether or not it is specified for those methods too. However this behavior can be turned off by setting TreeMux.RedirectTrailingSlash to false. By default it is set to true.

One exception to this rule is catch-all patterns. By default, trailing slash redirection is disabled on catch-all patterns, since the structure of the entire URL and the desired patterns can not be predicted. If trailing slash removal is desired on catch-all patterns, set TreeMux.RemoveCatchAllTrailingSlash to true.

router = treemux.New()
router.GET("/about", pageHandler)
router.GET("/posts/", postIndexHandler)
router.POST("/posts", postFormHandler)

GET /about will match normally.
GET /about/ will redirect to /about.
GET /posts will redirect to /posts/.
GET /posts/ will match normally.
POST /posts will redirect to /posts/, because the GET method used a trailing slash.

Custom Redirects

RedirectBehavior sets the behavior when the router redirects the request to the canonical version of the requested URL using RedirectTrailingSlash or RedirectClean. The default behavior is to return a 301 status, redirecting the browser to the version of the URL that matches the given pattern.

These are the values accepted for RedirectBehavior. You may also add these values to the RedirectMethodBehavior map to define custom per-method redirect behavior.

  • Redirect301 - HTTP 301 Moved Permanently; this is the default.
  • Redirect307 - HTTP/1.1 Temporary Redirect
  • Redirect308 - RFC7538 Permanent Redirect
  • UseHandler - Don't redirect to the canonical path. Just call the handler instead.

Rationale/Usage

On a POST request, most browsers that receive a 301 will submit a GET request to the redirected URL, meaning that any data will likely be lost. If you want to handle and avoid this behavior, you may use Redirect307, which causes most browsers to resubmit the request using the original method and request body.

Since 307 is supposed to be a temporary redirect, the new 308 status code has been proposed, which is treated the same, except it indicates correctly that the redirection is permanent. The big caveat here is that the RFC is relatively recent, and older or non-compliant browsers will not handle it. Therefore its use is not recommended unless you really know what you're doing.

Finally, the UseHandler value will simply call the handler function for the pattern, without redirecting to the canonical version of the URL.

RequestURI vs. URL.Path

Escaped Slashes

Go automatically processes escaped characters in a URL, converting + to a space and %XX to the corresponding character. This can present issues when the URL contains a %2f, which is unescaped to '/'. This isn't an issue for most applications, but it will prevent the router from correctly matching paths and wildcards.

For example, the pattern /post/:post would not match on /post/abc%2fdef, which is unescaped to /post/abc/def. The desired behavior is that it matches, and the post wildcard is set to abc/def.

Therefore, this router defaults to using the raw URL, stored in the Request.RequestURI variable. Matching wildcards and catch-alls are then unescaped, to give the desired behavior.

TL;DR: If a requested URL contains a %2f, this router will still do the right thing. Some Go HTTP routers may not due to Go issue 3659.

http Package Utility Functions

Although using RequestURI avoids the issue described above, certain utility functions such as http.StripPrefix modify URL.Path, and expect that the underlying router is using that field to make its decision. If you are using some of these functions, set the router's PathSource member to URLPath. This will give up the proper handling of escaped slashes described above, while allowing the router to work properly with these utility functions.

Error Handlers

NotFoundHandler

TreeMux.NotFoundHandler can be set to provide custom 404-error handling. The default implementation is Go's http.NotFound function.

MethodNotAllowedHandler

If a pattern matches, but the pattern does not have an associated handler for the requested method, the router calls the MethodNotAllowedHandler. The default version of this handler just writes the status code http.StatusMethodNotAllowed.

Unexpected Differences from Other Routers

This router is intentionally light on features in the name of simplicity and performance. When coming from another router that does heavier processing behind the scenes, you may encounter some unexpected behavior. This list is by no means exhaustive, but covers some nonobvious cases that users have encountered.

httprouter and catch-all parameters

When using httprouter, a route with a catch-all parameter (e.g. /images/*path) will match on URLs like /images/ where the catch-all parameter is empty. This router does not match on empty catch-all parameters, but the behavior can be duplicated by adding a route without the catch-all (e.g. /images/).

httptreemux

This is a fork of httptreemux. The original code was written by Daniel Imfeld.

Changes from httptreemux

  • Thin wrapper treemux.Request around http.Request to expose route via Request.Route and route params via req.Params.

  • Setting a context.Context does not require an allocation.

  • More efficient params encoding using a slice instead of a map.

  • Reworked configuration.

  • Group is immutable to avoid accidental leaking of middlewares into the group.

Owner
Comments
  • SERIOUS, issues with CORS and OPTIONS calls

    SERIOUS, issues with CORS and OPTIONS calls

    Coming from go-chi I was using this code:

    func main() {
    	r := chi.NewRouter()
    
    	r.Use(newCorsMiddleware())
    
    	r.Get("/", myHandler)
    	r.Post("/", myHandler)
    
    	http.ListenAndServe(":3000", r)
    }
    
    func newCorsMiddleware(corsOriginList []string) func(h http.Handler) http.Handler {
    	return func(next http.Handler) http.Handler {
    		return cors.New(cors.Options{
    			AllowedOrigins:   corsOriginList,
    			AllowCredentials: true,
    		}).Handler(next)
    	}
    }
    

    When I use it from browser (with a fetch method in javascript) and an OPTIONS call starts it works, I get 204 No Content and all the right Access-Control-Allow-..... correctly.

    With the same code but bunrouter instead of go-chi things are different:

    func main() {
    	r := bunrouter.New(bunrouter.WithMiddleware(newCorsMiddleware(corsOriginList)))
    
    	router.NewGroup("/",
    		bunrouter.WithGroup(func(group *bunrouter.Group) {
    			group.GET("", myHandler)
    			group.POST("", myHandler)
    			//group.OPTIONS("", myHandler)
    		}),
    	)
    
    	http.ListenAndServe(":3000", r)
    }
    
    func newCorsMiddleware(corsOriginList []string) func(h http.Handler) http.Handler {
    	return func(next http.Handler) http.Handler {
    		return cors.New(cors.Options{
    			AllowedOrigins:   corsOriginList,
    			AllowCredentials: true,
    		}).Handler(next)
    	}
    }
    

    During the OPTIONS call I get the 405 Method Not Allowed but all the right Access-Control-Allow-..... correctly.

    In go-chi I don't need to use router.Options() because this is a problem that https://github.com/rs/cors handles, not me.

    I think something is broken with bunrouter in this case, infact if I add a group.OPTIONS("", myHandler) the call works, but obviously this is wrong because I'm not directly handling OPTIONS calls.

  • `unable to upgrade struct { httpsnoop.Unwrapper...` and `http: superfluous response.WriteHeader call`

    `unable to upgrade struct { httpsnoop.Unwrapper...` and `http: superfluous response.WriteHeader call`

    Sorry to bother you again.

    I updated to bunrouter and reqlog 1.0.14 and the websocket works.

    But now if I reject origin it warns with:

    http: superfluous response.WriteHeader call from github.com/uptrace/bunrouter/extra/reqlog.NewResponseWriter.func1.1 (middleware.go:120)
    
    unable to upgrade struct { httpsnoop.Unwrapper; http.ResponseWriter; http.Flusher; http.CloseNotifier; http.Hijacker; io.ReaderFrom } to websocket websocket: request origin not allowed by Upgrader.CheckOrigin
    

    the code is:

    func newHandler() *handler.Server {
    	gqlgenServer := handler.New(gqlgen.NewExecutableSchema(gqlgen.Config{Resolvers: &resolvers}))
    
    	gqlgenServer.AddTransport(transport.Websocket{
    		KeepAlivePingInterval: 10 * time.Second,
    		Upgrader: websocket.Upgrader{
    			CheckOrigin: func(r *http.Request) bool {
    				return false // if true here it has no errors with 1.0.14
    			},
    		}},
    	)
    
    	gqlgenServer.AddTransport(transport.POST{})
    
    	return gqlgenServer
    }
    
    gqlgenHandler := newHandler()
    
    router.Use(authMiddleware).WithGroup("/graphql", func(group *bunrouter.Group) {
      group.POST("", bunrouter.HTTPHandler(gqlgenHandler))
    })
    

    Why?

  • How to set params and emulate request to controller (handlerfunc)

    How to set params and emulate request to controller (handlerfunc)

    💡 The feature or bug you are proposing

    Normally I'll do :

    	resp := httptest.NewRecorder()
    	req := bunrouter.Request{}
    	req.Request = &http.Request{}
    

    and I will create params like this:

    	params := httprouter.Params{}
    	params = append(params, httprouter.Param{Key: "id", Value: "157"})
    

    But I can't find a way to add those to the request. (this is my question) because proviously (github.com/julienschmidt/httprouter) I used to do :

    controller.GetOneRecord(resp, req, params)
    

    but now the params are wrapped inside of the request.

  • Panic on route handler

    Panic on route handler

    Hi, I'm trying to handle two endpoints: /api/internal and /api/internal/*params.

    IMO these are 2 different path, so the router should not panic.

    Btw, I think that you should return an error instead of panic: in my case I'm using the router as dynamic matcher, so the panic can be an unattended behaviour.

  • serving static files gives 404

    serving static files gives 404

    💡 The feature or bug you are proposing

    I try to serve static files, but even my simplest static dir fails.

    📄 The description of the bug or the logic behind your proposal

    	rootDir := libFile.RootDir()
    	dir := rootDir + "/website/css/"
    	fileServer := http.FileServer(http.Dir(dir))
    	router.GET("/css/*path", bunrouter.HTTPHandler(fileServer))
    
    

    dir is : "/Users/marcelloh/data/go-private/SailboatCompanion/website/css/" (which is the correct location)

    🚀 The expected result

    not a 404 when I call /css/mycss.css

    When I debug, I can see that the node /css (which is node 0) is overwritten by the node /country

  • http: superfluous response.WriteHeader call

    http: superfluous response.WriteHeader call

    I'm using bunrouter now and sometimes it appears this warning in logging:

    http: superfluous response.WriteHeader call from github.com/uptrace/bunrouter/extra/reqlog.(*statusCodeRecorder).WriteHeader (middleware.go:121)
    

    Why?

    https://github.com/uptrace/bunrouter/blob/b621373e5ed07d76ec548becf02df1add9253c0e/extra/reqlog/middleware.go#L121

  • unable to upgrade reqlog.flusher to websocket websocket: response does not implement http.Hijacker using websocket

    unable to upgrade reqlog.flusher to websocket websocket: response does not implement http.Hijacker using websocket

    I'm trying for the first time to use websockets (github.com/gorilla/websocket) with bunrouter.

    I'm using

    bunRouterOptions := []bunrouter.Option{
      // some...
    }
    
    bunRouterOptions = append(bunRouterOptions, bunrouter.Use(reqlog.NewMiddleware(reqlog.WithVerbose(true))))
    
    router.Use(gqlgenDataloadersMiddleware).WithGroup("/graphql", func(group *bunrouter.Group) {
      group.GET("", bunrouter.HTTPHandler(gqlgenHandler))
      group.POST("", bunrouter.HTTPHandler(gqlgenHandler))
    })
    

    but I'm having this error:

    unable to upgrade reqlog.flusher to websocket websocket: response does not implement http.Hijacker:
    http: superfluous response.WriteHeader call from github.com/uptrace/bunrouter/extra/reqlog.(*statusCodeRecorder).WriteHeader (middleware.go:134)
    

    If I comment the append reqlog.NewMiddleware line it works.

  • Bug: Route doesn't match if inside the url are present encoded characters

    Bug: Route doesn't match if inside the url are present encoded characters

    Current Bug

    I tried to register an api but the bunRouter route doesn't match if inside the path are present encoded characters

    registered route ->/api/backend/projects/:projectId/branches/:branchName/files/:filePath called url -> /api/backend/projects/project-id/branches/branch-id/files/config-extension%252Fcms-backend%252FcmsProperties.json

    I use the method bunrouter.New().Compat().handle() to register the route.

    Version used

    v1.0.10

  • Where is the docs project?

    Where is the docs project?

    Where is the project for bunrouter docs? I would subscribe to any changes on it, please.

    Why don't you put it in the docs folder of the project? Also for https://github.com/uptrace/bun.

  • Issue with SSE server

    Issue with SSE server "flusher, err := w.(http.Flusher)" error

    I'm trying to use the amazing https://github.com/r3labs/sse with bunrouter.

    func main() {
    	sseHandler := newSSEHandler()
    
    	router := bunrouter.New(
    		bunrouter.WithMiddleware(reqlog.NewMiddleware(reqlog.WithEnabled(true))),
    	)
    
    	router.NewGroup("sse",
    		bunrouter.WithGroup(func(group *bunrouter.Group) {
    			group.GET("", sseHandler)
    		}),
    	)
    
    	// Start server here...
    }
    
    func newSSEHandler() bunrouter.HandlerFunc {
    	sseServer := sse.New()
    
    	sseServer.CreateStream("test")
    
    	return func(w http.ResponseWriter, r bunrouter.Request) error {
    		sseServer.HTTPHandler(w, r.Request)
    
    		return nil
    	}
    }
    

    But when it starts it gives:

    Streaming unsupported!
    

    which comes from here: https://github.com/r3labs/sse/blob/11986ea9f6a2aa21c3a8237090fb95d8a72ca577/http.go#L19:

    func (s *Server) HTTPHandler(w http.ResponseWriter, r *http.Request) {
    	flusher, err := w.(http.Flusher)
    	if !err {
    		http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
    		return
    	}
    
    	// ...
    

    Is this a problem with bunrouter?

    How can I fix it?

  • Use middleware dynamically

    Use middleware dynamically

    Using https://github.com/go-chi/chi I'm writing code like this:

    router := chi.NewRouter()
    
    if isLoggingEnabled {
      router.Use(middleware.Logger)
    }
    
    return router
    

    Is there a way to do this with bunrouter?

  • Added configurable reqlog middleware

    Added configurable reqlog middleware

    📄 What has changed

    Enhancement for https://github.com/uptrace/bunrouter/issues/79 Configured reqlog middleware to log request IP Address, request Path, headers and params

    ✅ Before submitting, please be sure to check this list

    • [x] your branch will not cause merge conflict with main branch
    • [ ] unit tests are included
    • [ ] the CHANGELOG has been updated
    • [x] you are not committing extraneous files or sensitive data
  • reqlog middleware

    reqlog middleware

    💡 The feature or bug you are proposing

    [feature] reqlog middleware to be configurable

    📄 The description of the bug or the logic behind your proposal

    Are you able to make the logger configurable so it can include other parameters?

    ip, path, referrer, agent, header etc etc

  • Content-Type returned by JSON is not application/json when used in special middlewares

    Content-Type returned by JSON is not application/json when used in special middlewares

    💡 The feature or bug you are proposing

    Using bunrouter.JSON in handlers passed to either WithNotFoundHandler or WithMethodNotAllowedHandler results in application/text Content-Type header.

    📄 The description of the bug or the logic behind your proposal

    Constructing my router like this:

    router := bunrouter.New(
    	bunrouter.WithMiddleware(reqLogMiddleware),
    	bunrouter.WithMiddleware(errorHandlerMiddleware),
    	bunrouter.WithNotFoundHandler(notFoundHandler),
    	bunrouter.WithMethodNotAllowedHandler(methodNotAllowedHandler),
    )
    

    Both of my notFoundHandler and methodNotAllowedHandler middlewares send a valid JSON response with:

    bunrouter.JSON(w, err)
    

    Where err is a custom HTTPError type with json marshaling tags.

    The JSON marshaling does work and the response received on the client is something like:

    {"code":"not_found","message":"no route found for /"}
    

    But the Content-Type header received is text/plain; charset=utf-8.

    🚀 The expected result

    bunrouter.JSON should always set application/json as the Content-Type, even when used in special middlewares.

  • How to pass a value to middleware?

    How to pass a value to middleware?

    Normally I would use middleware like this:

    router.Use(middleware.MyMiddleware).GET("/demo", myDemoHandler)
    

    But let's say, I want to check in the middleware if the user has permissions to use the handler. Something like this:

    router.Use(middleware.Permissions("RW")).POST("/demo", myDemoHandler)
    

    How can I achieve something like this?

  • 301 status and ERR_TOO_MANY_REDIRECTS for static dir

    301 status and ERR_TOO_MANY_REDIRECTS for static dir

    I have these dirs:

    - static
      - index.html
      - js
        - main.js
    

    and I'm using the code listed here: https://bunrouter.uptrace.dev/guide/serving-static-files.html#fs-sub.

    Single file retrieving is working fine (eg. static/js/main.js).

    The issue I'm having is when I try to open static/. I get in browser a 301 for static/ to static and vice-versa and then ERR_TOO_MANY_REDIRECTS in Chrome.

    Is this my fault?

High-speed, flexible tree-based HTTP router for Go.

httptreemux High-speed, flexible, tree-based HTTP router for Go. This is inspired by Julien Schmidt's httprouter, in that it uses a patricia tree, but

Dec 28, 2022
Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer).

Bxog is a simple and fast HTTP router for Go (HTTP request multiplexer). Usage An example of using the multiplexer: package main import ( "io" "net

Dec 26, 2022
xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework.

gorouter xujiajun/gorouter is a simple and fast HTTP router for Go. It is easy to build RESTful APIs and your web framework. Motivation I wanted a sim

Dec 8, 2022
:rotating_light: Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.
:rotating_light: Is a lightweight, fast and extensible zero allocation HTTP router for Go used to create customizable frameworks.

LARS LARS is a fast radix-tree based, zero allocation, HTTP router for Go. view examples. If looking for a more pure Go solution, be sure to check out

Dec 27, 2022
Fast, simple, and lightweight HTTP router for Golang

Sariaf Fast, simple and lightweight HTTP router for golang Install go get -u github.com/majidsajadi/sariaf Features Lightweight compatible with net/ht

Aug 19, 2022
An extremely fast Go (golang) HTTP router that supports regular expression route matching. Comes with full support for building RESTful APIs.

ozzo-routing You may consider using go-rest-api to jumpstart your new RESTful applications with ozzo-routing. Description ozzo-routing is a Go package

Dec 31, 2022
Pure is a fast radix-tree based HTTP router
Pure is a fast radix-tree based HTTP router

package pure Pure is a fast radix-tree based HTTP router that sticks to the native implementations of Go's "net/http" package; in essence, keeping the

Dec 1, 2022
A powerful HTTP router and URL matcher for building Go web servers with 🦍

gorilla/mux https://www.gorillatoolkit.org/pkg/mux Package gorilla/mux implements a request router and dispatcher for matching incoming requests to th

Jan 9, 2023
lightweight, idiomatic and composable router for building Go HTTP services

chi is a lightweight, idiomatic and composable router for building Go HTTP services. It's especially good at helping you write large REST API services

Jan 8, 2023
:tongue: CleverGo is a lightweight, feature rich and high performance HTTP router for Go.

CleverGo CleverGo is a lightweight, feature rich and trie based high performance HTTP request router. go get -u clevergo.tech/clevergo English 简体中文 Fe

Nov 17, 2022
Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context.

Go-Lightweight-Router Lightweight Router for Golang using net/http standard library with custom route parsing, handler and context. Further developmen

Nov 3, 2021
Go HTTP request router and web framework benchmark

Go HTTP Router Benchmark This benchmark suite aims to compare the performance of HTTP request routers for Go by implementing the routing structure of

Dec 27, 2022
Simple Golang HTTP router
Simple Golang HTTP router

Bellt Simple Golang HTTP router Bellt Package implements a request router with the aim of managing controller actions based on fixed and parameterized

Sep 27, 2022
Go Server/API micro framework, HTTP request router, multiplexer, mux
Go Server/API micro framework, HTTP request router, multiplexer, mux

?? gorouter Go Server/API micro framework, HTTP request router, multiplexer, mux. ?? ABOUT Contributors: Rafał Lorenz Want to contribute ? Feel free t

Dec 16, 2022
A high performance HTTP request router that scales well

HttpRouter HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the

Dec 28, 2022
Go HTTP router

violetear Go HTTP router http://violetear.org Design Goals Keep it simple and small, avoiding extra complexity at all cost. KISS Support for static an

Dec 10, 2022
Simple router build on `net/http` supports custom middleWare.

XMUS-ROUTER Fast lightweight router build on net/http supports delegate and in url params. usage : Create new router using NewRouter() which need rout

Dec 27, 2021
Simple HTTP router for Go

ngamux Simple HTTP router for Go Installation Examples Todo Installation Run this command with correctly configured Go toolchain. go get github.com/ng

Dec 13, 2022
A high performance HTTP request router that scales well

HttpRouter HttpRouter is a lightweight high performance HTTP request router (also called multiplexer or just mux for short) for Go. In contrast to the

Dec 9, 2021