Merge pull request #85 from waouooo/support_assign_schemes

feature: 支持指定schemes参数
This commit is contained in:
MaxToby
2023-11-24 10:39:49 +08:00
committed by GitHub
5 changed files with 25 additions and 6 deletions

View File

@ -9,8 +9,8 @@ import (
"github.com/zeromicro/go-zero/tools/goctl/plugin"
)
func Do(filename string, host string, basePath string, in *plugin.Plugin) error {
swagger, err := applyGenerate(in, host, basePath)
func Do(filename string, host string, basePath string, schemes string, in *plugin.Plugin) error {
swagger, err := applyGenerate(in, host, basePath, schemes)
if err != nil {
fmt.Println(err)
}

View File

@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"reflect"
"regexp"
@ -54,7 +55,7 @@ func parseRangeOption(option string) (float64, float64, bool) {
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"])
version, _ := strconv.Unquote(p.Api.Info.Properties["version"])
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
}
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{}
newSecDefValue := swaggerSecuritySchemeObject{}
newSecDefValue.Name = "Authorization"