-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgen_http.go
47 lines (42 loc) · 1018 Bytes
/
gen_http.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
package main
import (
"bytes"
"strings"
"text/template"
)
// rpc service 信息
type serviceDesc struct {
ServiceType string // Greeter
ServiceName string // helloworld.Greeter
Metadata string // api/helloworld/helloworld.proto
GenValidate bool
Methods []*methodDesc
}
// rpc 方法信息
type methodDesc struct {
// method
Name string
Num int
Request string
Reply string
// http_rule
Path string
Method string
HasVars bool
HasBody bool
Body string
ResponseBody string
}
// execute 方法实现也其实不复杂,总起来就是 go 的 temple 包的使用
// 提前写好模板文件,然后拿到所有需要的变量,进行模板渲染,写入文件
func (s *serviceDesc) execute() string {
buf := new(bytes.Buffer)
tmpl, err := template.New("http_server").Parse(strings.TrimSpace(httpCodeTmpl))
if err != nil {
panic(err)
}
if err = tmpl.Execute(buf, s); err != nil {
panic(err)
}
return strings.Trim(buf.String(), "\r\n")
}