Revert "feat: support swagger configuration (#46)" (#47)

This reverts commit 814845170e.
This commit is contained in:
pei 2019-03-23 19:16:08 +09:00 committed by GitHub
parent 814845170e
commit acdcd7f6da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 32 deletions

View File

@ -64,11 +64,8 @@ import (
func main() {
r := gin.New()
config := &ginSwagger.Config{
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
}
// use ginSwagger middleware to
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
// use ginSwagger middleware to
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run()
}

View File

@ -25,10 +25,7 @@ import (
func main() {
r := gin.New()
config := &ginSwagger.Config{
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
}
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
r.Run()
}

View File

@ -11,28 +11,21 @@ import (
"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`.
func WrapHandler(config *Config, h *webdav.Handler) 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)
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) {
type swaggerUIBundle struct {
URL 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.Writer.Write([]byte("404 page not found"))
return
@ -43,9 +36,10 @@ func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
switch path {
case "index.html":
index.Execute(c.Writer, &swaggerUIBundle{
URL: config.URL,
})
s := &pro{
Host: "doc.json", //TODO: provide to customs?
}
index.Execute(c.Writer, s)
case "doc.json":
doc, err := swag.ReadDoc()
if err != nil {
@ -55,13 +49,14 @@ func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
return
default:
h.ServeHTTP(c.Writer, c.Request)
}
}
}
// DisablingWrapHandler turn handler off
// 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)
if eFlag != "" {
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 -->
@ -149,7 +144,7 @@ const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
window.onload = function() {
// Build a system
const ui = SwaggerUIBundle({
url: "{{.URL}}",
url: "{{.Host}}",
dom_id: '#swagger-ui',
validatorUrl: null,
presets: [

View File

@ -16,7 +16,7 @@ func TestWrapHandler(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
router.GET("/*any", WrapHandler(&Config{}, swaggerFiles.Handler))
router.GET("/*any", WrapHandler(swaggerFiles.Handler))
w1 := performRequest("GET", "/index.html", router)
assert.Equal(t, 200, w1.Code)
@ -37,7 +37,7 @@ func TestDisablingWrapHandler(t *testing.T) {
router := gin.New()
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)
assert.Equal(t, 200, w1.Code)
@ -53,7 +53,7 @@ func TestDisablingWrapHandler(t *testing.T) {
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)
assert.Equal(t, 404, w11.Code)