add host and basepath config

This commit is contained in:
MaxToby 2021-07-30 14:33:10 +08:00
parent d658701630
commit 0e70444ab8
5 changed files with 32 additions and 21 deletions

View File

@ -81,6 +81,10 @@ $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromic
```shell script
$ goctl api plugin -plugin goctl-swagger="swagger -filename user.json" -api user.api -dir .
```
* 指定HostbasePath [api-host-and-base-path](https://swagger.io/docs/specification/2-0/api-host-and-base-path/)
```shell script
$ goctl api plugin -plugin goctl-swagger="swagger -filename user.json -host 127.0.0.2 -basepath /api" -api user.api -dir .
```
* swagger ui 查看生成的文档
```shell script
$ docker run --rm -p 8083:8080 -e SWAGGER_JSON=/foo/user.json -v $PWD:/foo swaggerapi/swagger-ui

View File

@ -7,7 +7,6 @@ import (
)
func Generator(ctx *cli.Context) error {
fileName := ctx.String("filename")
if len(fileName) == 0 {
@ -18,5 +17,7 @@ func Generator(ctx *cli.Context) error {
if err != nil {
return err
}
return generate.Do(fileName, p)
basepath := ctx.String("basepath")
host := ctx.String("host")
return generate.Do(fileName, host, basepath, p)
}

View File

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

View File

@ -13,9 +13,7 @@ import (
plugin2 "github.com/tal-tech/go-zero/tools/goctl/plugin"
)
var (
strColon = []byte(":")
)
var strColon = []byte(":")
const (
defaultOption = "default"
@ -27,8 +25,7 @@ const (
equalToken = "="
)
func applyGenerate(p *plugin2.Plugin) (*swaggerObject, error) {
func applyGenerate(p *plugin2.Plugin, host string, basePath 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"])
@ -47,6 +44,12 @@ func applyGenerate(p *plugin2.Plugin) (*swaggerObject, error) {
Description: desc,
},
}
if len(host) > 0 {
s.Host = host
}
if len(basePath) > 0 {
s.BasePath = basePath
}
s.SecurityDefinitions = swaggerSecurityDefinitionsObject{}
newSecDefValue := swaggerSecuritySchemeObject{}
@ -66,9 +69,7 @@ func applyGenerate(p *plugin2.Plugin) (*swaggerObject, error) {
}
func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swaggerPathsObject, requestResponseRefs refMap) {
for _, group := range groups {
for _, route := range group.Routes {
path := route.Path
parameters := swaggerParametersObject{}
@ -126,13 +127,12 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
parameters = append(parameters, sp)
}
} else {
reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name())
if len(route.RequestType.Name()) > 0 {
var schema = swaggerSchemaObject{
schema := swaggerSchemaObject{
schemaCore: schemaCore{
Ref: reqRef,
},
@ -204,7 +204,6 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
paths[path] = pathItemObject
}
}
}
func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.Type, refs refMap) {
@ -249,14 +248,13 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
d[i2.Name()] = schema
}
}
func schemaOfField(member spec.Member) swaggerSchemaObject {
ret := swaggerSchemaObject{}
var core schemaCore
//spew.Dump(member)
// spew.Dump(member)
kind := swaggerMapTypes[member.Type.Name()]
var props *swaggerSchemaObjectProperties
@ -266,7 +264,7 @@ func schemaOfField(member spec.Member) swaggerSchemaObject {
switch ft := kind; ft {
case reflect.Invalid: //[]Struct 也有可能是 Struct
// []Struct
//map[ArrayType:map[Star:map[StringExpr:UserSearchReq] StringExpr:*UserSearchReq] StringExpr:[]*UserSearchReq]
// map[ArrayType:map[Star:map[StringExpr:UserSearchReq] StringExpr:*UserSearchReq] StringExpr:[]*UserSearchReq]
refTypeName := strings.Replace(member.Type.Name(), "[", "", 1)
refTypeName = strings.Replace(refTypeName, "]", "", 1)
refTypeName = strings.Replace(refTypeName, "*", "", 1)
@ -380,6 +378,7 @@ func countParams(path string) uint16 {
n += uint16(bytes.Count(s, strColon))
return n
}
func contains(s []string, str string) bool {
for _, v := range s {
if v == str {

14
main.go
View File

@ -2,10 +2,11 @@ package main
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/zeromicro/goctl-swagger/action"
"os"
"runtime"
"github.com/urfave/cli/v2"
"github.com/zeromicro/goctl-swagger/action"
)
var (
@ -16,6 +17,14 @@ var (
Usage: "generates swagger.json",
Action: action.Generator,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "host",
Usage: "api request address",
},
&cli.StringFlag{
Name: "basepath",
Usage: "url request prefix",
},
&cli.StringFlag{
Name: "filename",
Usage: "swagger save file name",
@ -26,7 +35,6 @@ var (
)
func main() {
app := cli.NewApp()
app.Usage = "a plugin of goctl to generate swagger.json"
app.Version = fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH)