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/chi

Mount

Mount registers all documentation routes on a chi router at the given path prefix.

main.go
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/http compatible, so any standard http.Handler middleware works.
  • The path prefix passed to Mount determines where the Scalar UI and spec JSON are served (e.g. /docs serves the UI at /docs and the spec at /docs/openapi.json).
  • Chi does not have a MountGroup function — use chi's r.Group or r.Route for grouped routes with shared middleware.