package ginSwagger import ( "html/template" "os" "regexp" "strings" "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 } // 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 } } // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(h *webdav.Handler, confs ...func(c *Config)) gin.HandlerFunc { defaultConfig := &Config{ URL: "doc.json", } for _, c := range confs { c(defaultConfig) } return CustomWrapHandler(defaultConfig, h) } // CustomWrapHandler wraps `http.Handler` into `gin.HandlerFunc` func CustomWrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc { //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) { type swaggerUIBundle struct { URL string } var matches []string if matches = rexp.FindStringSubmatch(c.Request.RequestURI); len(matches) != 3 { c.Status(404) c.Writer.Write([]byte("404 page not found")) return } path := matches[2] prefix := matches[1] h.Prefix = prefix if strings.HasSuffix(path, ".html") { c.Header("Content-Type", "text/html; charset=utf-8") } else if strings.HasSuffix(path, ".css") { c.Header("Content-Type", "text/css; charset=utf-8") } else if strings.HasSuffix(path, ".js") { c.Header("Content-Type", "application/javascript") } else if strings.HasSuffix(path, ".json") { c.Header("Content-Type", "application/json; charset=utf-8") } switch path { case "index.html": index.Execute(c.Writer, &swaggerUIBundle{ URL: config.URL, }) case "doc.json": doc, err := swag.ReadDoc() if err != nil { panic(err) } c.Writer.Write([]byte(doc)) return default: h.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 = `