Codevault initial commit
This commit is contained in:
35
internal/api/swagger.go
Normal file
35
internal/api/swagger.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
// RegisterSwaggerHandlers serves the OpenAPI spec and an interactive Swagger UI
|
||||
// (embedded swagger-ui-dist assets, no CDN) under /api.
|
||||
func RegisterSwaggerHandlers(router *gin.Engine) error {
|
||||
spec, err := GetSwagger()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error on building swagger specification: %w", err)
|
||||
}
|
||||
specBody, err := spec.MarshalJSON()
|
||||
if err != nil {
|
||||
return fmt.Errorf("error on marshaling swagger specification: %w", err)
|
||||
}
|
||||
|
||||
// /api/docs/*any (rather than /api/*any) so this wildcard doesn't collide with
|
||||
// the static /api/openapi.json route at the same tree level.
|
||||
router.GET("/api/openapi.json", func(c *gin.Context) {
|
||||
c.Data(http.StatusOK, "application/json", specBody)
|
||||
})
|
||||
router.GET("/api", func(c *gin.Context) {
|
||||
c.Redirect(http.StatusFound, "/api/docs/index.html")
|
||||
})
|
||||
router.GET("/api/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("/api/openapi.json")))
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user