feat(WrapHandler): add config using functional options (#52)
* feat(WrapHandler): add config using functional options * refine comment
This commit is contained in:
+2
-4
@@ -25,10 +25,8 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
|
||||||
config := &ginSwagger.Config{
|
url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") //The url pointing to API definition
|
||||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, url))
|
||||||
}
|
|
||||||
r.GET("/swagger/*any", ginSwagger.CustomWrapHandler(config, swaggerFiles.Handler))
|
|
||||||
|
|
||||||
r.Run()
|
r.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-4
@@ -13,16 +13,27 @@ import (
|
|||||||
|
|
||||||
// Config stores ginSwagger configuration variables.
|
// Config stores ginSwagger configuration variables.
|
||||||
type Config struct {
|
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
|
URL string
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultConfig = &Config{
|
// URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).
|
||||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
func URL(url string) func(c *Config) {
|
||||||
|
return func(c *Config) {
|
||||||
|
c.URL = url
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
|
// 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)
|
return CustomWrapHandler(defaultConfig, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user