package ginSwagger import ( "html/template" "net/http" "os" "path/filepath" "regexp" "sync" "golang.org/x/net/webdav" "github.com/gin-gonic/gin" "github.com/swaggo/swag" ) // Config stores ginSwagger configuration variables. type Config struct { //The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`. URL string DeepLinking bool DocExpansion string DefaultModelsExpandDepth int } // URL presents the url pointing to API definition (normally swagger.json or swagger.yaml). func URL(url string) func(c *Config) { return func(c *Config) { c.URL = url } } // DocExpansion list, full, none. func DocExpansion(docExpansion string) func(c *Config) { return func(c *Config) { c.DocExpansion = docExpansion } } // DeepLinking set the swagger deeplinking configuration func DeepLinking(deepLinking bool) func(c *Config) { return func(c *Config) { c.DeepLinking = deepLinking } } // DefaultModelsExpandDepth set the default expansion depth for models // (set to -1 completely hide the models). func DefaultModelsExpandDepth(depth int) func(c *Config) { return func(c *Config) { c.DefaultModelsExpandDepth = depth } } // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc { defaultConfig := &Config{ URL: "doc.json", DeepLinking: true, DocExpansion: "list", DefaultModelsExpandDepth: 1, } for _, c := range confs { c(defaultConfig) } return CustomWrapHandler(defaultConfig, h) } // CustomWrapHandler wraps `http.Handler` into `gin.HandlerFunc` func CustomWrapHandler(config *Config, handler *webdav.Handler) gin.HandlerFunc { var once sync.Once // create a template with name t := template.New("swagger_index.html") index, _ := t.Parse(swagger_index_templ) var rexp = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`) return func(c *gin.Context) { matches := rexp.FindStringSubmatch(c.Request.RequestURI) if len(matches) != 3 { c.Status(404) _, _ = c.Writer.Write([]byte("404 page not found")) return } path := matches[2] once.Do(func() { handler.Prefix = matches[1] }) switch filepath.Ext(path) { case ".html": c.Header("Content-Type", "text/html; charset=utf-8") case ".css": c.Header("Content-Type", "text/css; charset=utf-8") case ".js": c.Header("Content-Type", "application/javascript") case ".json": c.Header("Content-Type", "application/json; charset=utf-8") } switch path { case "index.html": _ = index.Execute(c.Writer, config) case "doc.json": doc, err := swag.ReadDoc() if err != nil { c.AbortWithStatus(http.StatusInternalServerError) return } _, _ = c.Writer.Write([]byte(doc)) default: handler.ServeHTTP(c.Writer, c.Request) } } } // DisablingWrapHandler turn handler off // if specified environment variable passed func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc { eFlag := os.Getenv(envName) if eFlag != "" { return func(c *gin.Context) { // Simulate behavior when route unspecified and // return 404 HTTP code c.String(404, "") } } return WrapHandler(h) } // DisablingCustomWrapHandler turn handler off // if specified environment variable passed func DisablingCustomWrapHandler(config *Config, h *webdav.Handler, envName string) gin.HandlerFunc { eFlag := os.Getenv(envName) if eFlag != "" { return func(c *gin.Context) { // Simulate behavior when route unspecified and // return 404 HTTP code c.String(404, "") } } return CustomWrapHandler(config, h) } const swagger_index_templ = ` Swagger UI
`