Changes: - forbid to overwrite secrets of other users - add some logs of success - add validation on code (length 50) - use same limiter for POST requests
39 lines
1.2 KiB
Go
39 lines
1.2 KiB
Go
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("/docs", 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
|
|
}
|