Fast Alice is a port of alice for fasthttp. Fast Alice provides a convenient way to chain your Fast HTTP middleware functions and the app handler.
In short, it transforms
Middleware1(Middleware2(Middleware3(App)))
to
fastalice.New(Middleware1, Middleware2, Middleware3).Then(App)
None of the other middleware chaining solutions behaves exactly like Alice. Alice is as minimal as it gets: in essence, it's just a for loop that does the wrapping for you.
Check out this blog post for explanation how Alice is different from other chaining solutions.
Your middleware constructors should have the form of
package main
func (fasthttp.RequestHandler) fasthttp.RequestHandler
Some middleware provide this out of the box. For ones that don't, it's trivial to write one yourself.
package main
func myLog(req fasthttp.RequestHandler) fasthttp.RequestHandler {
return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
log.Printf("%s %s - %v",
ctx.Method(),
ctx.RequestURI(),
ctx.Response.Header.StatusCode(),
)
})
}
This complete example shows the full power of Fast Alice.
package main
import (
"fmt"
"github.com/AubSs/fasthttplogger"
"github.com/brunvieira/fast-alice"
"github.com/brunvieira/fastcsrf"
"github.com/valyala/fasthttp"
)
func fastHTTPHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hi there! RequestURI is %q", ctx.RequestURI())
}
func main() {
chained := fastalice.New(fastcsrf.CSRF, fasthttplogger.CombinedColored).Then(fastHTTPHandler)
fasthttp.ListenAndServe(":8080", chained)
}
Here, the request will pass fastcsrf first, then fasthttplogger and will finally reach our handler.
Note that Fast Alice, as Alice, makes no guarantees for how one or another piece of middleware will behave. Once it passes the execution to the outer layer of middleware, it has no saying in whether middleware will execute the inner handlers. This is intentional behavior.
Fast Alice works with Go 1.0 and higher.
- Find an issue that bugs you / open a new one.
- Discuss.
- Branch off, commit, test.
- Make a pull request / attach the commits to the issue.