Skip to content

Commit

Permalink
feat(WrapHandler): add config using functional options (swaggo#52)
Browse files Browse the repository at this point in the history
* feat(WrapHandler): add config using functional options

* refine comment
  • Loading branch information
easonlin404 authored and pei0804 committed Mar 29, 2019
1 parent f9b4e46 commit 9668210
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
6 changes: 2 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ 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.CustomWrapHandler(config, swaggerFiles.Handler))
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") //The url pointing to API definition
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))

r.Run()
}
19 changes: 15 additions & 4 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,27 @@ import (

// Config stores ginSwagger configuration variables.
type Config struct {
//The url pointing to API definition (normally swagger.json or swagger.yaml).
//The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
URL string
}

var defaultConfig = &Config{
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
// 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) 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)
}

Expand Down

0 comments on commit 9668210

Please sign in to comment.