This reverts commit 814845170e.
This commit is contained in:
@@ -64,11 +64,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
|
||||||
config := &ginSwagger.Config{
|
// use ginSwagger middleware to
|
||||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
}
|
|
||||||
// use ginSwagger middleware to
|
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
|
|
||||||
|
|
||||||
r.Run()
|
r.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-4
@@ -25,10 +25,7 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
|
||||||
config := &ginSwagger.Config{
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
|
||||||
}
|
|
||||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
|
|
||||||
|
|
||||||
r.Run()
|
r.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-20
@@ -11,28 +11,21 @@ import (
|
|||||||
"github.com/swaggo/swag"
|
"github.com/swaggo/swag"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config stores ginSwagger configuration variables.
|
|
||||||
type Config struct {
|
|
||||||
//The url pointing to API definition (normally swagger.json or swagger.yaml).
|
|
||||||
URL string
|
|
||||||
}
|
|
||||||
|
|
||||||
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
|
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
|
||||||
func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
|
func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
||||||
//create a template with name
|
//create a template with name
|
||||||
t := template.New("swagger_index.html")
|
t := template.New("swagger_index.html")
|
||||||
index, _ := t.Parse(swagger_index_templ)
|
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)[\?|.]*`)
|
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) {
|
return func(c *gin.Context) {
|
||||||
|
|
||||||
type swaggerUIBundle struct {
|
|
||||||
URL string
|
|
||||||
}
|
|
||||||
|
|
||||||
var matches []string
|
var matches []string
|
||||||
if matches = rexp.FindStringSubmatch(c.Request.RequestURI); len(matches) != 3 {
|
if matches = re.FindStringSubmatch(c.Request.RequestURI); len(matches) != 3 {
|
||||||
c.Status(404)
|
c.Status(404)
|
||||||
c.Writer.Write([]byte("404 page not found"))
|
c.Writer.Write([]byte("404 page not found"))
|
||||||
return
|
return
|
||||||
@@ -43,9 +36,10 @@ func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
|
|||||||
|
|
||||||
switch path {
|
switch path {
|
||||||
case "index.html":
|
case "index.html":
|
||||||
index.Execute(c.Writer, &swaggerUIBundle{
|
s := &pro{
|
||||||
URL: config.URL,
|
Host: "doc.json", //TODO: provide to customs?
|
||||||
})
|
}
|
||||||
|
index.Execute(c.Writer, s)
|
||||||
case "doc.json":
|
case "doc.json":
|
||||||
doc, err := swag.ReadDoc()
|
doc, err := swag.ReadDoc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -55,13 +49,14 @@ func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
|
|||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
h.ServeHTTP(c.Writer, c.Request)
|
h.ServeHTTP(c.Writer, c.Request)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// DisablingWrapHandler turn handler off
|
// DisablingWrapHandler turn handler off
|
||||||
// if specified environment variable passed
|
// if specified environment variable passed
|
||||||
func DisablingWrapHandler(config *Config, h *webdav.Handler, envName string) gin.HandlerFunc {
|
func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc {
|
||||||
eFlag := os.Getenv(envName)
|
eFlag := os.Getenv(envName)
|
||||||
if eFlag != "" {
|
if eFlag != "" {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
@@ -71,7 +66,7 @@ func DisablingWrapHandler(config *Config, h *webdav.Handler, envName string) gin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return WrapHandler(config, h)
|
return WrapHandler(h)
|
||||||
}
|
}
|
||||||
|
|
||||||
const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
|
const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
|
||||||
@@ -149,7 +144,7 @@ const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
|
|||||||
window.onload = function() {
|
window.onload = function() {
|
||||||
// Build a system
|
// Build a system
|
||||||
const ui = SwaggerUIBundle({
|
const ui = SwaggerUIBundle({
|
||||||
url: "{{.URL}}",
|
url: "{{.Host}}",
|
||||||
dom_id: '#swagger-ui',
|
dom_id: '#swagger-ui',
|
||||||
validatorUrl: null,
|
validatorUrl: null,
|
||||||
presets: [
|
presets: [
|
||||||
|
|||||||
+3
-3
@@ -16,7 +16,7 @@ func TestWrapHandler(t *testing.T) {
|
|||||||
gin.SetMode(gin.TestMode)
|
gin.SetMode(gin.TestMode)
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
|
|
||||||
router.GET("/*any", WrapHandler(&Config{}, swaggerFiles.Handler))
|
router.GET("/*any", WrapHandler(swaggerFiles.Handler))
|
||||||
|
|
||||||
w1 := performRequest("GET", "/index.html", router)
|
w1 := performRequest("GET", "/index.html", router)
|
||||||
assert.Equal(t, 200, w1.Code)
|
assert.Equal(t, 200, w1.Code)
|
||||||
@@ -37,7 +37,7 @@ func TestDisablingWrapHandler(t *testing.T) {
|
|||||||
router := gin.New()
|
router := gin.New()
|
||||||
disablingKey := "SWAGGER_DISABLE"
|
disablingKey := "SWAGGER_DISABLE"
|
||||||
|
|
||||||
router.GET("/simple/*any", DisablingWrapHandler(&Config{}, swaggerFiles.Handler, disablingKey))
|
router.GET("/simple/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
|
||||||
|
|
||||||
w1 := performRequest("GET", "/simple/index.html", router)
|
w1 := performRequest("GET", "/simple/index.html", router)
|
||||||
assert.Equal(t, 200, w1.Code)
|
assert.Equal(t, 200, w1.Code)
|
||||||
@@ -53,7 +53,7 @@ func TestDisablingWrapHandler(t *testing.T) {
|
|||||||
|
|
||||||
os.Setenv(disablingKey, "true")
|
os.Setenv(disablingKey, "true")
|
||||||
|
|
||||||
router.GET("/disabling/*any", DisablingWrapHandler(&Config{}, swaggerFiles.Handler, disablingKey))
|
router.GET("/disabling/*any", DisablingWrapHandler(swaggerFiles.Handler, disablingKey))
|
||||||
|
|
||||||
w11 := performRequest("GET", "/disabling/index.html", router)
|
w11 := performRequest("GET", "/disabling/index.html", router)
|
||||||
assert.Equal(t, 404, w11.Code)
|
assert.Equal(t, 404, w11.Code)
|
||||||
|
|||||||
Reference in New Issue
Block a user