Merge pull request #85 from waouooo/support_assign_schemes
feature: 支持指定schemes参数
This commit is contained in:
commit
862371e153
@ -86,10 +86,10 @@ GOPROXY=https://goproxy.cn/,direct go install github.com/zeromicro/goctl-swagger
|
|||||||
goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .
|
goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .
|
||||||
```
|
```
|
||||||
|
|
||||||
* 指定Host,basePath [api-host-and-base-path](https://swagger.io/docs/specification/2-0/api-host-and-base-path/)
|
* 指定Host,basePath,schemes [api-host-and-base-path](https://swagger.io/docs/specification/2-0/api-host-and-base-path/)
|
||||||
|
|
||||||
```shell script
|
```shell script
|
||||||
goctl api plugin -plugin goctl-swagger="swagger -filename user.json -host 127.0.0.2 -basepath /api" -api user.api -dir .
|
goctl api plugin -plugin goctl-swagger="swagger -filename user.json -host 127.0.0.2 -basepath /api -schemes https,wss" -api user.api -dir .
|
||||||
```
|
```
|
||||||
|
|
||||||
* swagger ui 查看生成的文档
|
* swagger ui 查看生成的文档
|
||||||
|
@ -19,5 +19,6 @@ func Generator(ctx *cli.Context) error {
|
|||||||
}
|
}
|
||||||
basepath := ctx.String("basepath")
|
basepath := ctx.String("basepath")
|
||||||
host := ctx.String("host")
|
host := ctx.String("host")
|
||||||
return generate.Do(fileName, host, basepath, p)
|
schemes := ctx.String("schemes")
|
||||||
|
return generate.Do(fileName, host, basepath, schemes, p)
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,8 @@ import (
|
|||||||
"github.com/zeromicro/go-zero/tools/goctl/plugin"
|
"github.com/zeromicro/go-zero/tools/goctl/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Do(filename string, host string, basePath string, in *plugin.Plugin) error {
|
func Do(filename string, host string, basePath string, schemes string, in *plugin.Plugin) error {
|
||||||
swagger, err := applyGenerate(in, host, basePath)
|
swagger, err := applyGenerate(in, host, basePath, schemes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -54,7 +55,7 @@ func parseRangeOption(option string) (float64, float64, bool) {
|
|||||||
return min, max, true
|
return min, max, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func applyGenerate(p *plugin.Plugin, host string, basePath string) (*swaggerObject, error) {
|
func applyGenerate(p *plugin.Plugin, host string, basePath string, schemes string) (*swaggerObject, error) {
|
||||||
title, _ := strconv.Unquote(p.Api.Info.Properties["title"])
|
title, _ := strconv.Unquote(p.Api.Info.Properties["title"])
|
||||||
version, _ := strconv.Unquote(p.Api.Info.Properties["version"])
|
version, _ := strconv.Unquote(p.Api.Info.Properties["version"])
|
||||||
desc, _ := strconv.Unquote(p.Api.Info.Properties["desc"])
|
desc, _ := strconv.Unquote(p.Api.Info.Properties["desc"])
|
||||||
@ -80,6 +81,19 @@ func applyGenerate(p *plugin.Plugin, host string, basePath string) (*swaggerObje
|
|||||||
s.BasePath = basePath
|
s.BasePath = basePath
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(schemes) > 0 {
|
||||||
|
supportedSchemes := []string{"http", "https", "ws", "wss"}
|
||||||
|
ss := strings.Split(schemes, ",")
|
||||||
|
for i := range ss {
|
||||||
|
scheme := ss[i]
|
||||||
|
scheme = strings.TrimSpace(scheme)
|
||||||
|
if !contains(supportedSchemes, scheme) {
|
||||||
|
log.Fatalf("unsupport scheme: [%s], only support [http, https, ws, wss]", scheme)
|
||||||
|
}
|
||||||
|
ss[i] = scheme
|
||||||
|
}
|
||||||
|
s.Schemes = ss
|
||||||
|
}
|
||||||
s.SecurityDefinitions = swaggerSecurityDefinitionsObject{}
|
s.SecurityDefinitions = swaggerSecurityDefinitionsObject{}
|
||||||
newSecDefValue := swaggerSecuritySchemeObject{}
|
newSecDefValue := swaggerSecuritySchemeObject{}
|
||||||
newSecDefValue.Name = "Authorization"
|
newSecDefValue.Name = "Authorization"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user