gin
bash
go get go.chanx.dev/adapters/gingo
func Gin(engine *chanx.Engine) *gin.Engine {
r := gin.Default()
auth := func(c *gin.Context) {
c.Set("user", c.GetHeader("X-User")) // read by chanx.UserFrom("user")
}
chanxgin.Mount(r.Group("/", auth), engine) // "/ws/chat/:room"
chanxgin.MountDocs(r, engine, "/asyncapi", asyncapi.Info{Title: "Chat"})
return r
}go
engine.Handle("/ws/chat/{room}", ChatConsumer{}, chanx.UserFrom("user"))- Routes live in gin's router, so its groups, middleware and
NoRouteapply.{room}is registered as:room. - Middleware runs before the upgrade. A value it stores with
c.Set("user", u)is the connection's user throughchanx.UserFrom("user"), orr.Value("user")inAuthenticate. A middleware that aborts answers with an HTTP status, and no socket is opened. - The client IP is
c.ClientIP(), with gin's trusted proxies. - Docs:
chanxgin.MountDocsserves/asyncapi,.jsonand.yaml.
| Option | Does |
|---|---|
chanxgin.AcceptOptions(&coderws.Options{...}) | origins, subprotocols, compression |
chanxgin.Upgrade(fn) | another WebSocket library, such as a function calling gorillaws.Accept |
chanxgin.Handler(engine, route, nil) registers one route by hand. Shut down with engine.Shutdown(ctx) as well as your http.Server's.
Next: echo.