feat: support swagger configuration
This commit is contained in:
parent
ea4ea9de0c
commit
86684947e9
|
|
@ -64,8 +64,11 @@ import (
|
|||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
// use ginSwagger middleware to
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
config := &ginSwagger.Config{
|
||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
||||
}
|
||||
// use ginSwagger middleware to
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
|
||||
|
||||
r.Run()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,10 @@ import (
|
|||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
config := &ginSwagger.Config{
|
||||
URL: "http://localhost:8080/swagger/doc.json", //The url pointing to API definition
|
||||
}
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(config, swaggerFiles.Handler))
|
||||
|
||||
r.Run()
|
||||
}
|
||||
|
|
|
|||
36
swagger.go
36
swagger.go
|
|
@ -11,21 +11,29 @@ import (
|
|||
"github.com/swaggo/swag"
|
||||
)
|
||||
|
||||
// Config stores ginSwagger configuration variables.
|
||||
type Config struct {
|
||||
//The url pointing to API definition (normally swagger.json or swagger.yaml).
|
||||
URL string
|
||||
}
|
||||
|
||||
// WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.
|
||||
func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
||||
func WrapHandler(config *Config, h *webdav.Handler) gin.HandlerFunc {
|
||||
//create a template with name
|
||||
t := template.New("swagger_index.html")
|
||||
index, _ := t.Parse(swagger_index_templ)
|
||||
|
||||
type pro struct {
|
||||
Host string
|
||||
index, err := t.Parse(swagger_index_templ)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
type swaggerUIBundle struct {
|
||||
URL string
|
||||
}
|
||||
|
||||
var re = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`)
|
||||
var rexp = regexp.MustCompile(`(.*)(index\.html|doc\.json|favicon-16x16\.png|favicon-32x32\.png|/oauth2-redirect\.html|swagger-ui\.css|swagger-ui\.css\.map|swagger-ui\.js|swagger-ui\.js\.map|swagger-ui-bundle\.js|swagger-ui-bundle\.js\.map|swagger-ui-standalone-preset\.js|swagger-ui-standalone-preset\.js\.map)[\?|.]*`)
|
||||
|
||||
return func(c *gin.Context) {
|
||||
var matches []string
|
||||
if matches = re.FindStringSubmatch(c.Request.RequestURI); len(matches) != 3 {
|
||||
if matches = rexp.FindStringSubmatch(c.Request.RequestURI); len(matches) != 3 {
|
||||
c.Status(404)
|
||||
c.Writer.Write([]byte("404 page not found"))
|
||||
return
|
||||
|
|
@ -36,10 +44,11 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
|||
|
||||
switch path {
|
||||
case "index.html":
|
||||
s := &pro{
|
||||
Host: "doc.json", //TODO: provide to customs?
|
||||
if err := index.Execute(c.Writer, &swaggerUIBundle{
|
||||
URL: config.URL,
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
index.Execute(c.Writer, s)
|
||||
case "doc.json":
|
||||
doc, err := swag.ReadDoc()
|
||||
if err != nil {
|
||||
|
|
@ -49,14 +58,13 @@ func WrapHandler(h *webdav.Handler) gin.HandlerFunc {
|
|||
return
|
||||
default:
|
||||
h.ServeHTTP(c.Writer, c.Request)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// DisablingWrapHandler turn handler off
|
||||
// if specified environment variable passed
|
||||
func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc {
|
||||
func DisablingWrapHandler(config *Config, h *webdav.Handler, envName string) gin.HandlerFunc {
|
||||
eFlag := os.Getenv(envName)
|
||||
if eFlag != "" {
|
||||
return func(c *gin.Context) {
|
||||
|
|
@ -66,7 +74,7 @@ func DisablingWrapHandler(h *webdav.Handler, envName string) gin.HandlerFunc {
|
|||
}
|
||||
}
|
||||
|
||||
return WrapHandler(h)
|
||||
return WrapHandler(config, h)
|
||||
}
|
||||
|
||||
const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
|
||||
|
|
@ -144,7 +152,7 @@ const swagger_index_templ = `<!-- HTML for static distribution bundle build -->
|
|||
window.onload = function() {
|
||||
// Build a system
|
||||
const ui = SwaggerUIBundle({
|
||||
url: "{{.Host}}",
|
||||
url: "{{.URL}}",
|
||||
dom_id: '#swagger-ui',
|
||||
validatorUrl: null,
|
||||
presets: [
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ func TestWrapHandler(t *testing.T) {
|
|||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
|
||||
router.GET("/*any", WrapHandler(swaggerFiles.Handler))
|
||||
router.GET("/*any", WrapHandler(&Config{}, swaggerFiles.Handler))
|
||||
|
||||
w1 := performRequest("GET", "/index.html", router)
|
||||
assert.Equal(t, 200, w1.Code)
|
||||
|
|
|
|||
Loading…
Reference in New Issue