add host and basepath config
This commit is contained in:
parent
d658701630
commit
0e70444ab8
@ -81,6 +81,10 @@ $ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/zeromic
|
|||||||
```shell script
|
```shell script
|
||||||
$ 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/)
|
||||||
|
```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 查看生成的文档
|
* swagger ui 查看生成的文档
|
||||||
```shell script
|
```shell script
|
||||||
$ docker run --rm -p 8083:8080 -e SWAGGER_JSON=/foo/user.json -v $PWD:/foo swaggerapi/swagger-ui
|
$ docker run --rm -p 8083:8080 -e SWAGGER_JSON=/foo/user.json -v $PWD:/foo swaggerapi/swagger-ui
|
||||||
|
@ -7,7 +7,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Generator(ctx *cli.Context) error {
|
func Generator(ctx *cli.Context) error {
|
||||||
|
|
||||||
fileName := ctx.String("filename")
|
fileName := ctx.String("filename")
|
||||||
|
|
||||||
if len(fileName) == 0 {
|
if len(fileName) == 0 {
|
||||||
@ -18,5 +17,7 @@ func Generator(ctx *cli.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return generate.Do(fileName, p)
|
basepath := ctx.String("basepath")
|
||||||
|
host := ctx.String("host")
|
||||||
|
return generate.Do(fileName, host, basepath, p)
|
||||||
}
|
}
|
||||||
|
@ -9,9 +9,8 @@ import (
|
|||||||
plugin2 "github.com/tal-tech/go-zero/tools/goctl/plugin"
|
plugin2 "github.com/tal-tech/go-zero/tools/goctl/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Do(filename string, in *plugin2.Plugin) error {
|
func Do(filename string, host string, basePath string, in *plugin2.Plugin) error {
|
||||||
|
swagger, err := applyGenerate(in, host, basePath)
|
||||||
swagger, err := applyGenerate(in)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -13,9 +13,7 @@ import (
|
|||||||
plugin2 "github.com/tal-tech/go-zero/tools/goctl/plugin"
|
plugin2 "github.com/tal-tech/go-zero/tools/goctl/plugin"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var strColon = []byte(":")
|
||||||
strColon = []byte(":")
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultOption = "default"
|
defaultOption = "default"
|
||||||
@ -27,8 +25,7 @@ const (
|
|||||||
equalToken = "="
|
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"])
|
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"])
|
||||||
@ -47,6 +44,12 @@ func applyGenerate(p *plugin2.Plugin) (*swaggerObject, error) {
|
|||||||
Description: desc,
|
Description: desc,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
if len(host) > 0 {
|
||||||
|
s.Host = host
|
||||||
|
}
|
||||||
|
if len(basePath) > 0 {
|
||||||
|
s.BasePath = basePath
|
||||||
|
}
|
||||||
|
|
||||||
s.SecurityDefinitions = swaggerSecurityDefinitionsObject{}
|
s.SecurityDefinitions = swaggerSecurityDefinitionsObject{}
|
||||||
newSecDefValue := swaggerSecuritySchemeObject{}
|
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) {
|
func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swaggerPathsObject, requestResponseRefs refMap) {
|
||||||
|
|
||||||
for _, group := range groups {
|
for _, group := range groups {
|
||||||
|
|
||||||
for _, route := range group.Routes {
|
for _, route := range group.Routes {
|
||||||
path := route.Path
|
path := route.Path
|
||||||
parameters := swaggerParametersObject{}
|
parameters := swaggerParametersObject{}
|
||||||
@ -126,13 +127,12 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
|
|||||||
|
|
||||||
parameters = append(parameters, sp)
|
parameters = append(parameters, sp)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name())
|
reqRef := fmt.Sprintf("#/definitions/%s", route.RequestType.Name())
|
||||||
|
|
||||||
if len(route.RequestType.Name()) > 0 {
|
if len(route.RequestType.Name()) > 0 {
|
||||||
var schema = swaggerSchemaObject{
|
schema := swaggerSchemaObject{
|
||||||
schemaCore: schemaCore{
|
schemaCore: schemaCore{
|
||||||
Ref: reqRef,
|
Ref: reqRef,
|
||||||
},
|
},
|
||||||
@ -204,7 +204,6 @@ func renderServiceRoutes(service spec.Service, groups []spec.Group, paths swagge
|
|||||||
paths[path] = pathItemObject
|
paths[path] = pathItemObject
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.Type, refs refMap) {
|
func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.Type, refs refMap) {
|
||||||
@ -249,7 +248,6 @@ func renderReplyAsDefinition(d swaggerDefinitionsObject, m messageMap, p []spec.
|
|||||||
|
|
||||||
d[i2.Name()] = schema
|
d[i2.Name()] = schema
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func schemaOfField(member spec.Member) swaggerSchemaObject {
|
func schemaOfField(member spec.Member) swaggerSchemaObject {
|
||||||
@ -380,6 +378,7 @@ func countParams(path string) uint16 {
|
|||||||
n += uint16(bytes.Count(s, strColon))
|
n += uint16(bytes.Count(s, strColon))
|
||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s []string, str string) bool {
|
func contains(s []string, str string) bool {
|
||||||
for _, v := range s {
|
for _, v := range s {
|
||||||
if v == str {
|
if v == str {
|
||||||
|
14
main.go
14
main.go
@ -2,10 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/urfave/cli/v2"
|
|
||||||
"github.com/zeromicro/goctl-swagger/action"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"github.com/zeromicro/goctl-swagger/action"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -16,6 +17,14 @@ var (
|
|||||||
Usage: "generates swagger.json",
|
Usage: "generates swagger.json",
|
||||||
Action: action.Generator,
|
Action: action.Generator,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "host",
|
||||||
|
Usage: "api request address",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "basepath",
|
||||||
|
Usage: "url request prefix",
|
||||||
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "filename",
|
Name: "filename",
|
||||||
Usage: "swagger save file name",
|
Usage: "swagger save file name",
|
||||||
@ -26,7 +35,6 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Usage = "a plugin of goctl to generate swagger.json"
|
app.Usage = "a plugin of goctl to generate swagger.json"
|
||||||
app.Version = fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH)
|
app.Version = fmt.Sprintf("%s %s/%s", version, runtime.GOOS, runtime.GOARCH)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user