/
service_generator.go
176 lines (145 loc) · 4.92 KB
/
service_generator.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package gen
import (
"go/build"
"path"
"github.com/johnnyeven/libtools/codegen"
)
type ServiceGenerator struct {
PathName string
ServiceName string
DatabaseName string
Root string
}
func (g *ServiceGenerator) Load(cwd string) {
}
func (g *ServiceGenerator) Pick() {
}
func (g *ServiceGenerator) Output(cwd string) codegen.Outputs {
outputs := codegen.Outputs{}
codegen.NewGenFile("main", path.Join(g.ServiceName, "doc.go")).
WithData(g).
OutputTo(outputs)
outputs.WriteFiles()
pkg, _ := build.ImportDir(path.Join(cwd, g.ServiceName), build.ImportComment)
g.Root = pkg.ImportPath
if g.DatabaseName != "" {
codegen.NewGenFile("database", path.Join(g.ServiceName, "database/db.go")).
WithData(g).
Block(`
var DB{{ .ExposeVar .Data.DatabaseName }} = {{ ( .PureUse "github.com/johnnyeven/libtools/sqlx" )}}.NewDatabase("{{ .Data.DatabaseName }}")
`,
).
OutputTo(outputs)
outputs.WriteFiles()
}
codegen.NewGenFile("global", path.Join(g.ServiceName, "global/config.go")).
WithData(g).
Block(`
func init() {
{{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.SetServiceName("{{ .Data.ServiceName }}")
{{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.ConfP(&Config)
{{ if .Data.DatabaseName }}
{{ .PureUse .Data.Root "database" }}.DB{{ .ExposeVar .Data.DatabaseName }}.MustMigrateTo(Config.MasterDB.Get(), !{{ .PureUse "github.com/johnnyeven/libtools/servicex" }}.AutoMigrate)
{{ end }}
}
var Config = struct {
Log *{{ ( .PureUse "github.com/johnnyeven/libtools/log" ) }}.Log
Server {{ ( .PureUse "github.com/johnnyeven/libtools/courier/transport_http" ) }}.ServeHTTP
{{ if .Data.DatabaseName }}
MasterDB *{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL
SlaveDB *{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL
{{ end }}
}{
Log: &{{ ( .PureUse "github.com/johnnyeven/libtools/log" ) }}.Log{
Level: "DEBUG",
},
Server: {{ ( .PureUse "github.com/johnnyeven/libtools/courier/transport_http" ) }}.ServeHTTP{
WithCORS: true,
Port: 8000,
},
{{ if .Data.DatabaseName }}
MasterDB: &{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL{
Name: "{{ .Data.DatabaseName }}",
Port: 33306,
User: "root",
Password: "root",
Host: "....",
},
SlaveDB: &{{ .PureUse "github.com/johnnyeven/libtools/sqlx/mysql" }}.MySQL{
Name: "{{ .Data.DatabaseName }}-readonly",
Port: 33306,
User: "root",
Password: "root",
Host: "....",
},
{{ end }}
}
`,
).OutputTo(outputs)
codegen.NewGenFile("types", path.Join(g.ServiceName, "constants/types/doc.go")).WithData(g).Block(`
// Defined enum types here
`).OutputTo(outputs)
codegen.NewGenFile("modules", path.Join(g.ServiceName, "modules/doc.go")).WithData(g).Block(`
// Defined sub modules here
`).OutputTo(outputs)
codegen.NewGenFile("errors", path.Join(g.ServiceName, "constants/errors/status_err_codes.go")).
WithData(g).
Block(`
//go:generate libtools gen error
const ServiceStatusErrorCode = 0 * 1e3 // todo rename this
const (
// 请求参数错误
BadRequest {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusBadRequest*1e6 + ServiceStatusErrorCode + iota
)
const (
// 未找到
NotFound {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusNotFound*1e6 + ServiceStatusErrorCode + iota
)
const (
// @errTalk 未授权
Unauthorized {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusUnauthorized*1e6 + ServiceStatusErrorCode + iota
)
const (
// @errTalk 操作冲突
Conflict {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusConflict*1e6 + ServiceStatusErrorCode + iota
)
const (
// @errTalk 不允许操作
Forbidden {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusForbidden*1e6 + ServiceStatusErrorCode + iota
)
const (
// 内部处理错误
InternalError {{ .PureUse "github.com/johnnyeven/libtools/courier/status_error" }}.StatusErrorCode = http.StatusInternalServerError*1e6 + ServiceStatusErrorCode + iota
)
`,
).
OutputTo(outputs)
codegen.NewGenFile("routes", path.Join(g.ServiceName, "routes/root.go")).
WithData(g).
Block(`
var RootRouter = {{ .PureUse "github.com/johnnyeven/libtools/courier" }}.NewRouter(GroupRoot{})
func init() {
RootRouter.Register({{ .PureUse "github.com/johnnyeven/libtools/courier/swagger" }}.SwaggerRouter)
}
type GroupRoot struct {
courier.EmptyOperator
}
func (root GroupRoot) Path() string {
return "/{{ .Data.PathName }}"
}
`,
).
OutputTo(outputs)
outputs.WriteFiles()
codegen.NewGenFile("main", path.Join(g.ServiceName, "main.go")).
WithData(g).
Block(`
func main() {
{{( .PureUse "github.com/johnnyeven/libtools/servicex" )}}.Execute()
{{( .PureUse .Data.Root "global" )}}.Config.Server.Serve({{ ( .PureUse .Data.Root "routes" ) }}.RootRouter)
}
`,
).
OutputTo(outputs)
return outputs
}