Skip to content

Commit a6c351e

Browse files
committed
feat: support gen mocks data, mock server
1 parent 5f06d8a commit a6c351e

File tree

18 files changed

+850
-196
lines changed

18 files changed

+850
-196
lines changed

.vscode/launch.json

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,31 @@
55
"version": "0.2.0",
66
"configurations": [
77
{
8-
"name": "Launch file",
8+
"name": "Generate Docs and Mocks",
99
"type": "go",
1010
"request": "launch",
1111
"mode": "debug",
12-
"cwd": "${workspaceRoot}",
12+
"cwd": "${workspaceRoot}/examples",
1313
"program": "${workspaceFolder}/main.go",
1414
"args": [
15-
"--dir", "examples/svc-user,examples/common",
16-
"--output", "examples/docs",
15+
"--dir", "svc-user,common",
16+
"--output", "docs",
1717
"--template", "markdown",
18+
"--gen-mocks"
1819
]
19-
}
20+
},
21+
{
22+
"name": "Mock Server",
23+
"type": "go",
24+
"request": "launch",
25+
"mode": "debug",
26+
"cwd": "${workspaceRoot}/examples",
27+
"program": "${workspaceFolder}/main.go",
28+
"args": [
29+
"mock",
30+
"--dir", "./docs/mocks",
31+
"--listen", "localhost:8001"
32+
]
33+
},
2034
]
2135
}

README.md

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# apidocgen
22

3-
apidocgen is a tool for Go to generate apis markdown docs.
3+
apidocgen is a tool for Go to generate apis markdown docs and mocks.
44

55
## Install
66

@@ -12,44 +12,40 @@ go install github.com/alovn/apidocgen@latest
1212

1313
```bash
1414
$ apidocgen --help
15-
apidocgen is a tool for Go to generate apis markdown docs.
15+
apidocgen is a tool for Go to generate apis markdown docs and mocks. For example:
1616

17-
Usage:
18-
apidocgen --dir= --excludes= --output= --output-index= --template= --single
19-
20-
Flags:
21-
--dir: Search apis dir, comma separated, default .
22-
--excludes: Exclude directories and files when searching, comma separated
23-
--output: Generate markdown files dir, default ./docs/
24-
--output-index: Generate index file name.
25-
--template: Template name or custom template directory, built-in includes markdown and apidocs, default markdown.
26-
--single: Generate a single markdown file.
27-
```
28-
29-
built-in templates include `markdown` and `apidocs`, default is `markdown`.
17+
apidocgen --dir= --excludes= --output= --output-index= --template= --single --gen-mocks
18+
apidocgen --mock --mock-listen=:8001
19+
apidocgen mock --data= --listen=
3020

31-
run the command in the go module directory.
32-
33-
```bash
34-
cd your-api-service-dir
35-
apidocgen \
36-
--dir=svc-user,common \
37-
--output=./docs
21+
Usage:
22+
apidocgen [flags]
23+
apidocgen [command]
3824

39-
apidocgen \
40-
--dir=svc-user,common \
41-
--template=apidocs \
42-
--output=./docs
25+
Available Commands:
26+
help Help about any command
27+
mock
4328

44-
apidocgen --output-index=index.md //generate index.md
45-
apidocgen --output-index=@{service}.md //generate index file name by your @service comment, example: svc-user.md.
29+
Flags:
30+
--dir string Search apis dir, comma separated (default ".")
31+
--excludes string Exclude directories and files when searching, comma separated
32+
--gen-mocks Is generate the mock files
33+
-h, --help help for apidocgen
34+
--mock-listen string Mock Server listen address (default "localhost:8001")
35+
--mock Serve a mock server
36+
--output string Generate markdown files dir (default "./docs/")
37+
--output-index string Generate index file name
38+
--single Is generate a single doc
39+
--template string Template name or custom template directory, built-in includes markdown and apidocs
40+
41+
Use "apidocgen [command] --help" for more information about a command.
4642
```
4743

4844
## Template
4945

50-
The built-in includes `markdown` and `apidocs`.
46+
The built-in templates include `markdown` and `apidocs`, default is `markdown`.
5147

52-
The built-in template `apidocs` is the template for generate website [apidocs](https://github.com/alovn/apidocs).
48+
The template `apidocs` is the template for generate website [apidocs](https://github.com/alovn/apidocs).
5349

5450
You can also use the custom template:
5551

@@ -96,8 +92,8 @@ apidocgen supported any web frameworks. here are an example using [bytego](https
9692
//@accept json
9793
//@format json
9894
//@request LoginRequest
99-
//@response 200 common.Response{code=0,msg="success",data=LoginResponse} "登录成功返回数据"
100-
//@response 200 common.Response{code=10020,msg="password_error"} "密码错误"
95+
//@response 200 common.Response{code=0,msg="success",data=LoginResponse} "success description" //mock
96+
//@response 200 common.Response{code=10020,msg="password_error"} "error description"
10197
//@author alovn
10298
func (c *Controller) Login(c *bytego.Ctx) {
10399
//bind LoginRequest
@@ -128,7 +124,7 @@ annotation|description|example
128124
--|--|--
129125
service|Required, the service's identification|@service svc-user
130126
baseurl|The base url of api|@baseurl /
131-
group|The group of service|@group account
127+
group|The group of service, add it at api comment or at the head of file comment.|@group account
132128
title|The title of service, group and api|@title UserService
133129
desc|The description of service, group and api|@desc xxx
134130
api|The http api handler|@api POST /account/register
@@ -186,6 +182,7 @@ deprecated|Mark api as deprecated.|@deprecated
186182
import (
187183
"common"
188184
)
185+
189186
//response 200 common.Response{code=0,data=User} "success description"
190187
```
191188
@@ -197,3 +194,40 @@ deprecated|Mark api as deprecated.|@deprecated
197194
Name string `json:"name" example:"user name"`
198195
} //User Infomation
199196
```
197+
198+
## Mock
199+
200+
1. generate apis mocks files. add `//mock` at the end of `response`, default use first.
201+
202+
```go
203+
//response 200 Response{code=0,data=[]User} "success description" //mock
204+
```
205+
206+
generate apis mocks files, default generated in the directory `./docs/mocks`.
207+
208+
```bash
209+
apidocgen --dir=common,svc-user --gen-mocks
210+
```
211+
212+
2. serve the mock server from source code.
213+
214+
```bash
215+
apidocgen --dir=common,svc-user --mock --mock-listen=:8001
216+
```
217+
218+
3. serve the mock server from generated mock files.
219+
220+
```bash
221+
apidocgen mock --data=./mocks --listen=:8001
222+
```
223+
224+
```bash
225+
$ curl -X POST -d "" localhost:8001/user/account/login
226+
{
227+
"code": 0,
228+
"data": {
229+
"welcome_msg": "string"
230+
},
231+
"msg": "success"
232+
}
233+
```

cmd/mock.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright © 2022 alovn <[email protected]>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"log"
9+
10+
"github.com/alovn/apidocgen/mock"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
var (
15+
mockDataDir string
16+
mockListen string
17+
)
18+
19+
// mockCmd represents the mock command
20+
var mockCmd = &cobra.Command{
21+
Use: "mock",
22+
Long: `Serve a mock serve with generated mock files. For example:
23+
24+
apidocgen mock --data=./docs/mocks --listen=localhost:8001`,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
mockServer := mock.New(mockListen)
27+
if err := mockServer.InitFiles(mockDataDir); err != nil {
28+
log.Fatal(err)
29+
}
30+
if err := mockServer.Serve(); err != nil {
31+
log.Fatal(err)
32+
}
33+
},
34+
}
35+
36+
func init() {
37+
rootCmd.AddCommand(mockCmd)
38+
mockCmd.Flags().StringVar(&mockDataDir, "data", "./docs/mocks/", "mocks data directory.")
39+
mockCmd.Flags().StringVar(&mockListen, "listen", "localhost:8001", "mock server listen address.")
40+
}

cmd/root.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright © 2022 alovn <[email protected]>
3+
4+
*/
5+
package cmd
6+
7+
import (
8+
"log"
9+
"os"
10+
11+
"github.com/alovn/apidocgen/gen"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var (
16+
searchDir string
17+
outputDir string
18+
outputIndexName string
19+
templateDir string
20+
excludesDir string
21+
isSingle bool
22+
isGenMocks bool
23+
isMockServer bool
24+
mockServerListen string
25+
)
26+
27+
// rootCmd represents the base command when called without any subcommands
28+
var rootCmd = &cobra.Command{
29+
Use: "apidocgen",
30+
Long: `apidocgen is a tool for Go to generate apis markdown docs and mocks. For example:
31+
32+
apidocgen --dir= --excludes= --output= --output-index= --template= --single --gen-mocks
33+
apidocgen --mock --mock-listen=:8001
34+
apidocgen mock --data= --listen=`,
35+
CompletionOptions: cobra.CompletionOptions{
36+
DisableDefaultCmd: true,
37+
},
38+
Run: func(cmd *cobra.Command, args []string) {
39+
g := gen.New(&gen.Config{
40+
SearchDir: searchDir,
41+
OutputDir: outputDir,
42+
OutputIndexName: outputIndexName,
43+
TemplateDir: templateDir,
44+
ExcludesDir: excludesDir,
45+
IsGenSingleFile: isSingle,
46+
IsGenMocks: isGenMocks,
47+
IsMockServer: isMockServer,
48+
MockServerListen: mockServerListen,
49+
})
50+
if err := g.Build(); err != nil {
51+
log.Fatal(err)
52+
}
53+
},
54+
}
55+
56+
func Execute() {
57+
err := rootCmd.Execute()
58+
if err != nil {
59+
os.Exit(1)
60+
}
61+
}
62+
63+
func init() {
64+
rootCmd.Flags().StringVar(&searchDir, "dir", ".", "Search apis dir, comma separated")
65+
rootCmd.Flags().StringVar(&outputDir, "output", "./docs/", "Generate markdown files dir")
66+
rootCmd.Flags().StringVar(&outputIndexName, "output-index", "", "Generate index file name")
67+
rootCmd.Flags().StringVar(&templateDir, "template", "", "Template name or custom template directory, built-in includes markdown and apidocs")
68+
rootCmd.Flags().StringVar(&excludesDir, "excludes", "", "Exclude directories and files when searching, comma separated")
69+
rootCmd.Flags().BoolVar(&isSingle, "single", false, "Is generate a single doc")
70+
rootCmd.Flags().BoolVar(&isGenMocks, "gen-mocks", false, "Is generate the mock files")
71+
rootCmd.Flags().BoolVar(&isMockServer, "mock", false, "Serve a mock server")
72+
rootCmd.Flags().StringVar(&mockServerListen, "mock-listen", "localhost:8001", "Mock Server listen address")
73+
}

examples/docs/apis-demo.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,44 +157,44 @@ __id__|_param_|int64|false|||DemoID
157157
_body_:
158158

159159
```xml
160-
<request> //object(handler.DemoXMLRequest), XML测试请求对象
161-
<id>0</id> //int64, DemoID
160+
<request> //object(handler.DemoXMLRequest), XML测试请求对象
161+
<id>0</id> //int64, DemoID
162162
</request>
163163
```
164164

165165
__Response__:
166166

167167
```xml
168168
//StatusCode: 200
169-
<response> //object(common.Response), 通用返回结果
170-
<code>0</code> //int, 返回状态码
171-
<demo> //object(handler.DemoXMLResponse), XML测试返回对象
172-
<address>string</address> //string, 地址信息
173-
<city_id>0</city_id> //int64, 城市ID
174-
<id>0</id> //int64, 地址ID
169+
<response> //object(common.Response), 通用返回结果
170+
<code>0</code> //int, 返回状态码
171+
<demo> //object(handler.DemoXMLResponse), XML测试返回对象
172+
<address>string</address> //string, 地址信息
173+
<city_id>0</city_id> //int64, 城市ID
174+
<id>0</id> //int64, 地址ID
175175
</demo>
176-
<msg>success</msg> //string, 返回消息
176+
<msg>success</msg> //string, 返回消息
177177
</response>
178178
```
179179

180180
```xml
181181
//StatusCode: 200
182-
<response> //object(common.Response), 通用返回结果
183-
<code>0</code> //int, 返回状态码
184-
<data> //object(handler.DemoXMLResponse2), XML测试返回对象2
185-
<address>string</address> //string, 地址信息
186-
<city_id>0</city_id> //int64, 城市ID
187-
<id>0</id> //int64, 地址ID
182+
<response> //object(common.Response), 通用返回结果
183+
<code>0</code> //int, 返回状态码
184+
<data> //object(handler.DemoXMLResponse2), XML测试返回对象2
185+
<address>string</address> //string, 地址信息
186+
<city_id>0</city_id> //int64, 城市ID
187+
<id>0</id> //int64, 地址ID
188188
</data>
189-
<msg>success</msg> //string, 返回消息
189+
<msg>success</msg> //string, 返回消息
190190
</response>
191191
```
192192

193193
```xml
194194
//StatusCode: 200
195-
<response> //object(common.Response), 通用返回结果
196-
<code>10010</code> //int, 返回状态码
197-
<msg>sme error</msg> //string, 返回消息
195+
<response> //object(common.Response), 通用返回结果
196+
<code>10010</code> //int, 返回状态码
197+
<msg>sme error</msg> //string, 返回消息
198198
</response>
199199
```
200200

0 commit comments

Comments
 (0)