swagger.go 981 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package router
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "github.com/labring/aiproxy/core/docs"
  6. swaggerfiles "github.com/swaggo/files"
  7. ginSwagger "github.com/swaggo/gin-swagger"
  8. "github.com/swaggo/swag"
  9. )
  10. func cloneSwaggerInfo(spec *swag.Spec) *swag.Spec {
  11. newSpec := *spec
  12. return &newSpec
  13. }
  14. func SetSwaggerRouter(router *gin.Engine) {
  15. docs.SwaggerInfo.BasePath = "/"
  16. router.GET("/doc.json", func(ctx *gin.Context) {
  17. ctx.Header("Content-Type", "application/json; charset=utf-8")
  18. if ctx.Request.Host == "" {
  19. ctx.String(http.StatusOK, docs.SwaggerInfo.ReadDoc())
  20. return
  21. }
  22. swagInfo := cloneSwaggerInfo(docs.SwaggerInfo)
  23. swagInfo.Host = ctx.Request.Host
  24. ctx.String(http.StatusOK, swagInfo.ReadDoc())
  25. })
  26. router.GET("/swagger", func(ctx *gin.Context) {
  27. ctx.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
  28. })
  29. router.GET("/swagger/*any",
  30. ginSwagger.WrapHandler(
  31. swaggerfiles.Handler,
  32. ginSwagger.URL("/doc.json"),
  33. ),
  34. )
  35. }