Skip to content

Commit

Permalink
Ability to create EchoWrapHandler with specific API ID
Browse files Browse the repository at this point in the history
For backwards compatibility, preserve the functionality of
EchoWrapHandler but create new EchoWrapHandlerName which accepts a name
parameter.

See: swaggo/swag#885
  • Loading branch information
joshk0 committed Feb 15, 2021
1 parent 090c69b commit a570a54
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ func URL(url string) func(c *Config) {
// WrapHandler wraps swaggerFiles.Handler and returns echo.HandlerFunc
var WrapHandler = EchoWrapHandler()

// EchoWrapHandler wraps `http.Handler` into `echo.HandlerFunc`.
// EchoWrapHandler is the same as EchoWrapHandlerName, but uses the default swag ID.
func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc {
return EchoWrapHandlerName(swag.Name, confs...)
}

// EchoWrapHandlerName wraps `http.Handler` into `echo.HandlerFunc` with a specific swag ID.
func EchoWrapHandlerName(swagID string, confs ...func(c *Config)) echo.HandlerFunc {

handler := swaggerFiles.Handler

config := &Config{
URL: "doc.json",
}

for _, c := range confs {
c(config)
}

// create a template with name
t := template.New("swagger_index.html")
index, _ := t.Parse(indexTempl)
Expand All @@ -64,7 +65,7 @@ func EchoWrapHandler(confs ...func(c *Config)) echo.HandlerFunc {

index.Execute(c.Response().Writer, config)
case "doc.json":
doc, _ := swag.ReadDoc()
doc, _ := swag.ReadDocName(swagID)
c.Response().Write([]byte(doc))
default:
handler.ServeHTTP(c.Response().Writer, c.Request())
Expand Down

0 comments on commit a570a54

Please sign in to comment.