Fiber
For Fiber v3, on fasthttp.
bash
go get go.chanx.dev/adapters/fibergo
func Fiber(engine *chanx.Engine) *fiber.App {
app := fiber.New()
auth := func(c fiber.Ctx) error {
c.Locals("user", c.Get("X-User")) // copied before the upgrade, read by chanx.UserFrom("user")
return c.Next()
}
chanxfiber.Mount(app.Group("/", auth), engine) // "/ws/chat/:room"
chanxfiber.MountDocs(app, engine, "/asyncapi", asyncapi.Info{Title: "Chat"})
return app
}go
engine.Handle("/ws/chat/{room}", ChatConsumer{}, chanx.UserFrom("user"))- Routes, groups and middleware are Fiber's.
{room}is registered as:room. - Middleware runs before the upgrade, and an error it returns goes through Fiber's
ErrorHandler. - The upgrade uses Fiber's own WebSocket package;
chanxfiber.WebSocket(cfg)sets its origins, subprotocols and buffers. - Docs:
chanxfiber.MountDocsserves/asyncapi,.jsonand.yaml.
Fiber recycles its context
Once the upgrade returns, Fiber reuses the request's context and its strings for the next request. The adapter copies what chanx reads before that happens: path parameters, query, headers, cookies, the client IP, and every Locals value under a string key. A value under another key type must be named:
go
func FiberTypedLocals(engine *chanx.Engine) *fiber.App {
app := fiber.New()
auth := func(c fiber.Ctx) error {
c.Locals(userKey{}, c.Get("X-User"))
return c.Next()
}
// A non-string Locals key must be named to be copied before the upgrade.
chanxfiber.Mount(app.Group("/", auth), engine, chanxfiber.Locals(userKey{}))
return app
}Request.Native() returns nil on Fiber, since the context it would return is no longer yours.
Next: Channel layers.