go-api/swagger.go
Snider 095c38a8c4 feat: add Swagger UI endpoint with runtime spec serving
Co-Authored-By: Virgil <virgil@lethean.io>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-20 15:56:29 +00:00

38 lines
969 B
Go

// SPDX-License-Identifier: EUPL-1.2
package api
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"
)
// swaggerSpec holds a minimal OpenAPI spec for runtime serving.
type swaggerSpec struct {
title string
description string
version string
}
// ReadDoc returns the Swagger 2.0 JSON document for this spec.
func (s *swaggerSpec) ReadDoc() string {
return `{
"swagger": "2.0",
"info": {
"title": "` + s.title + `",
"description": "` + s.description + `",
"version": "` + s.version + `"
},
"basePath": "/",
"paths": {}
}`
}
// registerSwagger mounts the Swagger UI and doc.json endpoint.
func registerSwagger(g *gin.Engine, title, description, version string) {
spec := &swaggerSpec{title: title, description: description, version: version}
swag.Register(swag.Name, spec)
g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}