sonda/internal/api/http/docs.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

40 lines
1.3 KiB
Go

package httpapi
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/swaggest/swgui/v5emb"
"git.kmalloc.org/squid/sonda/internal/api/openapi"
)
const (
specPath = "/api/v1/openapi.yaml"
docsPath = "/api/docs/"
)
// registerDocs serves the OpenAPI spec and a Swagger UI for it. Both are public:
// they describe the API and contain no data. The UI's assets are embedded in the
// binary, so opening it never contacts another server.
func registerDocs(r *gin.Engine, v1 *gin.RouterGroup) {
v1.GET("/openapi.yaml", func(c *gin.Context) {
c.Header("Cache-Control", "no-cache")
c.Data(http.StatusOK, "application/yaml; charset=utf-8", openapi.Spec)
})
ui := gin.WrapH(v5emb.New("Sonda API", specPath, docsPath))
r.GET("/api/docs", func(c *gin.Context) { c.Redirect(http.StatusMovedPermanently, docsPath) })
r.GET("/api/docs/*any", docsHeaders, ui)
r.HEAD("/api/docs/*any", docsHeaders, ui)
}
// docsHeaders protects the page where people paste their token: it cannot be
// framed by another site, and the browser must not guess content types.
func docsHeaders(c *gin.Context) {
h := c.Writer.Header()
h.Set("X-Frame-Options", "DENY")
h.Set("Content-Security-Policy", "frame-ancestors 'none'")
h.Set("X-Content-Type-Options", "nosniff")
h.Set("Referrer-Policy", "no-referrer")
}