Adapters
Chi Adapter
The Chi adapter mounts open-swag-go documentation routes onto a chi router. Chi is a lightweight, idiomatic router built on net/http, so the adapter works seamlessly with standard middleware.
Installation
go get github.com/andrianprasetya/open-swag-go/adapters/chiMount
Mount registers all documentation routes on a chi router at the given path prefix.
package main
import (
"net/http"
"github.com/go-chi/chi/v5"
openswag "github.com/andrianprasetya/open-swag-go"
swagchi "github.com/andrianprasetya/open-swag-go/adapters/chi"
)
func main() {
r := chi.NewRouter()
docs := openswag.New(openswag.Config{
Title: "My API",
Version: "1.0.0",
})
docs.Add(openswag.Endpoint{
Method: "GET",
Path: "/health",
Summary: "Health check",
Responses: []openswag.Response{
{Status: 200, Description: "OK"},
},
})
swagchi.Mount(r, "/docs", docs)
http.ListenAndServe(":8080", r)
}Middleware
Chi's middleware stack applies to mounted documentation routes automatically. For example, you can protect the docs with authentication middleware:
r.Group(func(r chi.Router) {
r.Use(authMiddleware)
swagchi.Mount(r, "/docs", docs)
})Notes
- Chi is
net/httpcompatible, so any standardhttp.Handlermiddleware works. - The path prefix passed to
Mountdetermines where the Scalar UI and spec JSON are served (e.g./docsserves the UI at/docsand the spec at/docs/openapi.json). - Chi does not have a
MountGroupfunction — use chi'sr.Grouporr.Routefor grouped routes with shared middleware.