After seeing the unfortunate news that gorilla/mux was archived I needed a new router. After not seeing anything that felt quite like that I decided to build my own.
To install run go get github.com/lukasmwerner/pine
Example Usage for the router:
p := pine.New()
p.HandleFunc("/hello/{name}", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(fmt.Sprintf("Hello, %s", pine.Var(r, "name"))))
})
log.Fatalln(http.ListenAndServe(":8080", p))
All handlers are parsed in first to last definition order (e.g. if you have a variable in the folder defined first that will run first)
Gorilla/Mux style middlewares are supported by the type pine.MiddlewareFunc
which is just a func(http.Handler) http.Handler
under the hood.
Usage:
p := pine.New()
p.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// make sure to call `next` otherwise the handler will not get called
next.ServeHTTP(w, r)
})
})
Note: Middlewares are run in order of being added
There are a few pre-made middlewares that are defined in the middlewares
package such as:
middlewares.HTTPLogger
Logs the HTTP Request/Responses in the following format:2023/04/02 12:48:37 host: localhost:8080 method: GET uri: / status: 200 Ok
middlewares.STolinskiTiming
Puts the requests into different time buckets such based on the (slow, middle) durations passed. Inserts aX-Duration
header to based on those timing buckets
- lukasmwerner/shakespeareswords
- lukasmwerner/pine/examples/markdown-server
- lukasmwerner/pine/examples/hello-world
- routing based on paths
- variables on paths
- middlewares
- Firewatch Soundtrack - Chris Remo
- Discovery - Daft Punk
- Tron: Legacy - Daft Punk
- Guardians of the Galaxy Vol. 2
While I appreciate any contributions, this is still MY router so if it is not in the spirit of the API I want to use then feel free to make your own fork modify it however you would like.