api/swagger.go
Snider de63217168 feat: AX v0.8.0 — Core primitives, Result returns, zero disallowed imports
- api.go: errors.Is → core.Is
- openapi.go: Build returns core.Result, encoding/json → core.JSONMarshal
- export.go: rewrite with core.Fs, core.JSONUnmarshal, returns core.Result
- codegen.go: rewrite with c.Process(), core.Fs, App.Find — no os/exec
- sse.go: encoding/json → core.JSONMarshalString, fmt → core.Sprintf
- swagger.go: fmt → core.Sprintf, Build caller updated for core.Result
- middleware.go: strings → core.HasPrefix/SplitN/Lower
- authentik.go: strings → core.HasPrefix/TrimPrefix/Split/Trim/Contains
- brotli.go: strings → core.Contains

Transport boundary files (net/http, io for compression) retained.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-26 08:39:59 +00:00

55 lines
1.4 KiB
Go

// SPDX-License-Identifier: EUPL-1.2
package api
import (
core "dappco.re/go/core"
"sync"
"sync/atomic"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/swaggo/swag"
)
// swaggerSeq provides unique instance names so multiple Engine instances
// (common in tests) do not collide in the global swag registry.
var swaggerSeq atomic.Uint64
// swaggerSpec wraps SpecBuilder to satisfy the swag.Spec interface.
// The spec is built once on first access and cached.
type swaggerSpec struct {
builder *SpecBuilder
groups []RouteGroup
once sync.Once
doc string
}
// ReadDoc returns the OpenAPI 3.1 JSON document for this spec.
func (s *swaggerSpec) ReadDoc() string {
s.once.Do(func() {
r := s.builder.Build(s.groups)
if !r.OK {
s.doc = `{"openapi":"3.1.0","info":{"title":"error","version":"0.0.0"},"paths":{}}`
return
}
s.doc = string(r.Value.([]byte))
})
return s.doc
}
// registerSwagger mounts the Swagger UI and doc.json endpoint.
func registerSwagger(g *gin.Engine, title, description, version string, groups []RouteGroup) {
spec := &swaggerSpec{
builder: &SpecBuilder{
Title: title,
Description: description,
Version: version,
},
groups: groups,
}
name := core.Sprintf("swagger_%d", swaggerSeq.Add(1))
swag.Register(name, spec)
g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.NewHandler(), ginSwagger.InstanceName(name)))
}