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 15bfbdb
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions swagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (

"github.com/labstack/echo/v4"
"github.com/swaggo/files"
"github.com/swaggo/swag"

"github.com/joshk0/swag"
)

// Config stores echoSwagger configuration variables.
Expand All @@ -26,19 +27,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 +66,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 15bfbdb

Please sign in to comment.