package ginSwagger import ( "golang.org/x/net/webdav" "html/template" "regexp" "github.com/gin-gonic/gin" "github.com/swaggo/swag" ) // WrapHandler wraps `http.Handler` into `gin.HandlerFunc`. func WrapHandler(h *webdav.Handler) gin.HandlerFunc { //create a template with name t := template.New("swagger_index.html") index, _ := t.Parse(swagger_index_templ) type pro struct { Host string } var re = 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) { var matches []string if matches = re.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 switch path { case "index.html": s := &pro{ Host: "doc.json", //TODO: provide to customs? } index.Execute(c.Writer, s) case "doc.json": doc, _ := swag.ReadDoc() c.Writer.Write([]byte(doc)) return default: h.ServeHTTP(c.Writer, c.Request) } } } const swagger_index_templ = ` Swagger UI
`