Skip to content

Commit

Permalink
feat: ability to set "schemes" programatically (#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored and easonlin404 committed Jul 8, 2019
1 parent e1fae5f commit 9fbf26c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func main() {
docs.SwaggerInfo.Version = "1.0"
docs.SwaggerInfo.Host = "petstore.swagger.io"
docs.SwaggerInfo.BasePath = "/v2"

docs.SwaggerInfo.Schemes = []string{"http", "https"}

r := gin.New()

// use ginSwagger middleware to serve the API docs
Expand Down
22 changes: 19 additions & 3 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path"
"strings"
"text/template"
"time"

Expand Down Expand Up @@ -64,6 +65,12 @@ func (g *Gen) Build(config *Config) error {
}
swagger := p.GetSwagger()

schemes := []string{}
for _, scheme := range swagger.Schemes {
schemes = append(schemes, fmt.Sprintf("%q", scheme))
}
swagger.Schemes = []string{}

b, err := json.MarshalIndent(swagger, "", " ")
if err != nil {
return err
Expand Down Expand Up @@ -107,9 +114,11 @@ func (g *Gen) Build(config *Config) error {
if err := packageTemplate.Execute(docs, struct {
Timestamp time.Time
Doc string
Schemes string
}{
Timestamp: time.Now(),
Doc: "`" + string(b) + "`",
Doc: "`" + strings.Replace(string(b), "{", "{\n \"schemes\": {{ marshal .Schemes }},", 1) + "`",
Schemes: "[]string{" + strings.Join(schemes, ",") + "}",
}); err != nil {
return err
}
Expand All @@ -129,6 +138,7 @@ package docs
import (
"bytes"
"encoding/json"
"github.com/alecthomas/template"
"github.com/swaggo/swag"
Expand All @@ -140,17 +150,23 @@ type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo swaggerInfo
var SwaggerInfo = swaggerInfo{ Schemes: {{.Schemes}}}
type s struct{}
func (s *s) ReadDoc() string {
t, err := template.New("swagger_info").Parse(doc)
t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface {}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}
Expand Down

0 comments on commit 9fbf26c

Please sign in to comment.