mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-01-12 03:21:33 +00:00
add swagger support
- install swaggo - add swagger config option
This commit is contained in:
5
api/error.go
Normal file
5
api/error.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package api
|
||||
|
||||
type Error struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
@@ -11,6 +11,10 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
_ "github.com/aptly-dev/aptly/docs" // import docs
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
var context *ctx.AptlyContext
|
||||
@@ -22,7 +26,27 @@ func apiMetricsGet() gin.HandlerFunc {
|
||||
}
|
||||
}
|
||||
|
||||
func redirectSwagger(c *gin.Context) {
|
||||
if c.Request.URL.Path == "/docs/" {
|
||||
c.Redirect(http.StatusMovedPermanently, "/docs/index.html")
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
|
||||
// Router returns prebuilt with routes http.Handler
|
||||
// @title Aptly API
|
||||
// @version 1.0
|
||||
// @description Aptly REST API Documentation
|
||||
|
||||
// @contact.name Aptly
|
||||
// @contact.url http://github.com/aptly-dev/aptly
|
||||
// @contact.email support@aptly.info
|
||||
|
||||
// @license.name MIT License
|
||||
// @license.url http://www.
|
||||
|
||||
// @BasePath /api
|
||||
func Router(c *ctx.AptlyContext) http.Handler {
|
||||
if aptly.EnableDebug {
|
||||
gin.SetMode(gin.DebugMode)
|
||||
@@ -48,6 +72,12 @@ func Router(c *ctx.AptlyContext) http.Handler {
|
||||
|
||||
router.Use(gin.Recovery(), gin.ErrorLogger())
|
||||
|
||||
if c.Config().EnableSwaggerEndpoint {
|
||||
router.Use(redirectSwagger)
|
||||
url := ginSwagger.URL("/docs/doc.json")
|
||||
router.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
|
||||
}
|
||||
|
||||
if c.Config().EnableMetricsEndpoint {
|
||||
MetricsCollectorRegistrar.Register(router)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user