Skip to content

Commit

Permalink
Revert "feat: support swagger configuration (swaggo#46)" (swaggo#47)
Browse files Browse the repository at this point in the history
This reverts commit 8148451.
  • Loading branch information
pei0804 authored Mar 23, 2019
1 parent 8148451 commit acdcd7f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
5 changes: 1 addition & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
35 changes: 15 additions & 20 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

return func(c *gin.Context) {

type swaggerUIBundle struct {
URL 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 = 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
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 -->
Expand Down Expand Up @@ -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: [
Expand Down
6 changes: 3 additions & 3 deletions swagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit acdcd7f

Please sign in to comment.