Adapters
Gin Adapter
The Gin adapter mounts open-swag-go documentation routes onto a Gin engine or route group. Gin is a high-performance HTTP framework with a martini-like API.
Installation
go get github.com/andrianprasetya/open-swag-go/adapters/ginMount
Mount registers documentation routes on a Gin engine at the given path prefix.
package main
import (
"github.com/gin-gonic/gin"
openswag "github.com/andrianprasetya/open-swag-go"
swaggin "github.com/andrianprasetya/open-swag-go/adapters/gin"
)
func main() {
r := gin.Default()
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"},
},
})
swaggin.Mount(r, "/docs", docs)
r.Run(":8080")
}MountGroup
MountGroup attaches documentation routes to a Gin RouterGroup. This is useful when you want to share middleware (e.g. authentication) across the docs routes.
api := r.Group("/api/v1")
api.Use(authMiddleware())
swaggin.MountGroup(api, "/docs", docs)
// Docs served at /api/v1/docsNotes
- Gin uses its own
gin.Contextrather thannet/httpdirectly, so make sure you import the Gin adapter — not the net/http one. MountGroupis the preferred approach when your docs live under a versioned or authenticated route group.- The adapter handles both the Scalar UI HTML page and the OpenAPI JSON spec endpoint.