forked from lazada/swgui
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Swagger UI to v3.43.0 and add go1.16 embedding (#16)
- Loading branch information
Showing
20 changed files
with
200 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"html/template" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/swaggest/swgui" | ||
) | ||
|
||
// Handler handles swagger UI request. | ||
type Handler struct { | ||
swgui.Config | ||
|
||
ConfigJson template.JS | ||
|
||
tpl *template.Template | ||
staticServer http.Handler | ||
} | ||
|
||
// NewHandlerWithConfig returns a HTTP handler for swagger UI. | ||
func NewHandlerWithConfig(config swgui.Config, assetsBase, faviconBase string, staticServer http.Handler) *Handler { | ||
config.BasePath = strings.TrimSuffix(config.BasePath, "/") + "/" | ||
|
||
h := &Handler{ | ||
Config: config, | ||
} | ||
j, _ := json.Marshal(h.Config) | ||
h.ConfigJson = template.JS(j) | ||
h.tpl, _ = template.New("index").Parse(IndexTpl(assetsBase, faviconBase)) | ||
if staticServer != nil { | ||
h.staticServer = http.StripPrefix(h.BasePath, staticServer) | ||
} | ||
return h | ||
} | ||
|
||
// ServeHTTP implements http.Handler interface to handle swagger UI request. | ||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(h.BasePath, "/") && h.staticServer != nil { | ||
h.staticServer.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/html") | ||
if err := h.tpl.Execute(w, h); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,23 @@ | ||
package v3 | ||
|
||
import ( | ||
"encoding/json" | ||
"html/template" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/swaggest/swgui" | ||
"github.com/swaggest/swgui/internal" | ||
) | ||
|
||
// Handler handles swagger UI request. | ||
type Handler struct { | ||
swgui.Config | ||
|
||
ConfigJson template.JS | ||
|
||
tpl *template.Template | ||
staticServer http.Handler | ||
} | ||
type Handler = internal.Handler | ||
|
||
// NewHandler returns a HTTP handler for swagger UI. | ||
func NewHandler(title, swaggerJSONPath string, basePath string) *Handler { | ||
h := &Handler{} | ||
h.Title = title | ||
h.SwaggerJSON = swaggerJSONPath | ||
h.BasePath = strings.TrimSuffix(basePath, "/") + "/" | ||
|
||
j, _ := json.Marshal(h.Config) | ||
h.ConfigJson = template.JS(j) | ||
h.tpl, _ = template.New("index").Parse(indexTpl) | ||
if staticServer != nil { | ||
h.staticServer = http.StripPrefix(basePath, staticServer) | ||
} | ||
return h | ||
return NewHandlerWithConfig(swgui.Config{ | ||
Title: title, | ||
SwaggerJSON: swaggerJSONPath, | ||
BasePath: basePath, | ||
}) | ||
} | ||
|
||
// NewHandlerWithConfig returns a HTTP handler for swagger UI. | ||
func NewHandlerWithConfig(config swgui.Config) *Handler { | ||
config.BasePath = strings.TrimSuffix(config.BasePath, "/") + "/" | ||
|
||
h := &Handler{ | ||
Config: config, | ||
} | ||
j, _ := json.Marshal(h.Config) | ||
h.ConfigJson = template.JS(j) | ||
h.tpl, _ = template.New("index").Parse(indexTpl) | ||
if staticServer != nil { | ||
h.staticServer = http.StripPrefix(h.BasePath, staticServer) | ||
} | ||
return h | ||
} | ||
|
||
// ServeHTTP implements http.Handler interface to handle swagger UI request. | ||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
if strings.TrimSuffix(r.URL.Path, "/") != strings.TrimSuffix(h.BasePath, "/") && h.staticServer != nil { | ||
h.staticServer.ServeHTTP(w, r) | ||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/html") | ||
if err := h.tpl.Execute(w, h); err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
} | ||
return internal.NewHandlerWithConfig(config, assetsBase, faviconBase, staticServer) | ||
} |
Oops, something went wrong.