-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
154 lines (129 loc) · 2.87 KB
/
render.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package validator
import (
"encoding/json"
"fmt"
"html/template"
"io"
"os"
"strings"
"github.com/go-wyvern/leego"
)
var AppApis []Api
const codeTag = "```"
type Api struct {
Description string
Method string
Path string
Handler leego.HandlerFunc
SuccessStdOut interface{}
SuccessFormat []byte
FailStdOut interface{}
FailFormat []byte
StdFormat string
CodeTag string
Validator *Validator
}
type Module struct {
ModuleName string
Apis []Api
}
type Project struct {
ProjectName string
Modules []Module
}
func Find(method, path string) *Validator {
for _, r := range AppApis {
if r.Method == method && r.Path == path {
return r.Validator
}
}
return nil
}
func NewProject(name string) *Project {
p := new(Project)
p.ProjectName = name
return p
}
func NewModule(module_name string) *Module {
module := new(Module)
module.ModuleName = module_name
return module
}
func NewApi(method, path, d string, h leego.HandlerFunc, v *Validator) *Api {
r := new(Api)
r.Method = method
r.Description = d
r.Path = path
r.Handler = h
r.Validator = v
r.CodeTag = codeTag
return r
}
func (c *Api) SetSuccessStdOut(s interface{}) {
c.SuccessStdOut = s
if c.StdFormat == "json" {
c.SuccessFormat, _ = json.MarshalIndent(s, "", " ")
}
}
func (c *Api) SetFailStdOut(s interface{}) {
c.FailStdOut = s
if c.StdFormat == "json" {
c.FailFormat, _ = json.MarshalIndent(s, "", " ")
}
}
func (c *Module) Use(a Api) *Module {
c.Apis = append(c.Apis, a)
AppApis = append(AppApis, a)
return c
}
func (c *Project) Use(m Module) *Project {
c.Modules = append(c.Modules, m)
return c
}
func (c *Project) RenderMarkdown(filename string) error {
var err error
f, err := os.Create(filename)
if err != nil {
fmt.Println(err.Error())
return err
}
err = tmpl(f, MarkdownTemplate, c)
if err != nil {
return err
}
return nil
}
var MarkdownTemplate = `{{with .}}# {{.ProjectName}}
{{range .Modules}}
## {{.ModuleName}}
{{range .Apis}}
### {{.Method}} {{.Path}} {{.Description}}
请求参数:
| 名称 | 类型 | 说明 | 是否必须 |
| -----|:-----:|:---------:|:-----:|
{{range $name, $params :=.Validator.ApiParams}}|**{{$name}}**|{{$params.Type}}|{{$params.Description}}|{{$params.Require}}|
{{end}}
请求正确返回:
{{.CodeTag}}
{{.SuccessFormat|printf "%s"|unescaped}}
{{.CodeTag}}
请求错误返回:
{{.CodeTag}}
{{.FailFormat | printf "%s"|unescaped}}
{{.CodeTag}}
{{end}}{{end}}{{end}}
`
func tmpl(w io.Writer, text string, data interface{}) error {
t := template.New("top")
t.Funcs(template.FuncMap{"trim": func(s template.HTML) template.HTML {
return template.HTML(strings.TrimSpace(string(s)))
}})
t.Funcs(template.FuncMap{"unescaped": func(x string) interface{} {
return template.HTML(x)
}})
template.Must(t.Parse(text))
if err := t.Execute(w, data); err != nil {
return err
}
return nil
}