/
templates.go
287 lines (247 loc) · 6.54 KB
/
templates.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package base
import (
"bytes"
"os"
"path"
"strings"
"text/template"
"github.com/TarsCloud/TarsGo/tars/tools/tarsgo/internal/bindata"
"github.com/TarsCloud/TarsGo/tars/tools/tarsgo/internal/consts"
)
const (
cmakeStartTemplateFile = "start.sh"
cmakeStartTemplate = `#!/bin/bash
set -ex
mkdir -p build
cd build
cmake ..
make
cd -
./build/bin/{{.Server}} --config=config/config.conf
`
cmakeListsTxtTemplateFile = "CMakeLists.txt"
cmakeListsTxtTemplate = `
execute_process(COMMAND go env GOPATH OUTPUT_VARIABLE GOPATH)
string(REGEX REPLACE "\n$" "" GOPATH "${GOPATH}")
include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/tars-tools.cmake)
cmake_minimum_required(VERSION 2.8)
project({{.Server}} Go) # select GO compile
gen_server({{.App}} {{.Server}})
add_subdirectory(client)
# go env -w GO111MODULE=on
# go mod init
# mkdir build
# cd build
# cmake ..
# make`
clientCMakeListsTxtTemplateFile = "client/CMakeLists.txt"
clientCMakeListsTxtTemplate = `
cmake_minimum_required(VERSION 2.8)
project(client Go) # select GO compile
gen_server({{.App}} client)
# go mod init
# mkdir build
# cd build
# cmake ..
# make`
makeStartTemplateFile = "start.sh"
makeStartTemplate = `#!/bin/bash
set -ex
make
./{{.Server}} --config=config/config.conf
`
makefileTemplateFile = "Makefile"
makefileTemplate = `APP := {{.App}}
TARGET := {{.Server}}
MFLAGS :=
DFLAGS :=
CONFIG := client
STRIP_FLAG:= N
J2GO_FLAG:=
-include scripts/makefile.tars.gomod.mk
`
mainGoTemplateFile = "main.go"
mainGoTemplate = `package main
import (
"fmt"
"os"
"github.com/TarsCloud/TarsGo/tars"
"{{.GoModuleName}}/tars-protocol/{{.App}}"
)
func main() {
// Get server config
cfg := tars.GetServerConfig()
// New servant imp
imp := new({{.Servant}}Imp)
err := imp.Init()
if err != nil {
fmt.Printf("{{.Servant}}Imp init fail, err:(%s)\n", err)
os.Exit(-1)
}
// New servant
app := new({{.App}}.{{.Servant}})
// Register Servant
app.AddServantWithContext(imp, cfg.App+"."+cfg.Server+".{{.Servant}}Obj")
// Run application
tars.Run()
}
`
clientGoTemplateFile = "client/client.go"
clientGoTemplate = `package main
import (
"fmt"
"github.com/TarsCloud/TarsGo/tars"
"{{.GoModuleName}}/tars-protocol/{{.App}}"
)
func main() {
comm := tars.NewCommunicator()
obj := fmt.Sprintf("{{.App}}.{{.Server}}.{{.Servant}}Obj@tcp -h 127.0.0.1 -p 10015 -t 60000")
app := new({{.App}}.{{.Servant}})
comm.StringToProxy(obj, app)
var out, i int32
i = 123
ret, err := app.Add(i, i*2, &out)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret, out)
}
`
configConfTemplateFile = "config/config.conf"
configConfTemplate = `<tars>
<application>
<server>
app={{.App}}
server={{.Server}}
local=tcp -h 127.0.0.1 -p 10014 -t 30000
logpath=/tmp
<{{.App}}.{{.Server}}.{{.Servant}}ObjAdapter>
allow
endpoint=tcp -h 127.0.0.1 -p 10015 -t 60000
handlegroup={{.App}}.{{.Server}}.{{.Servant}}ObjAdapter
maxconns=200000
protocol=tars
queuecap=10000
queuetimeout=60000
servant={{.App}}.{{.Server}}.{{.Servant}}Obj
shmcap=0
shmkey=0
threads=1
</{{.App}}.{{.Server}}.{{.Servant}}ObjAdapter>
</server>
</application>
</tars>
`
servantTarsTemplateFile = "Servant.tars"
servantTarsTemplate = `module {{.App}}
{
interface {{.Servant}}
{
int Add(int a,int b,out int c); // Some example function
int Sub(int a,int b,out int c); // Some example function
};
};
`
servantImpGoTemplateFile = "Servant_imp.go"
servantImpGoTemplate = `package main
import (
"context"
)
// {{.Servant}}Imp servant implementation
type {{.Servant}}Imp struct {
}
// Init servant init
func (imp *{{.Servant}}Imp) Init() error {
//initialize servant here:
//...
return nil
}
// Destroy servant destroy
func (imp *{{.Servant}}Imp) Destroy() {
//destroy servant here:
//...
}
func (imp *{{.Servant}}Imp) Add(ctx context.Context, a int32, b int32, c *int32) (int32, error) {
//Doing something in your function
//...
return 0, nil
}
func (imp *{{.Servant}}Imp) Sub(ctx context.Context, a int32, b int32, c *int32) (int32, error) {
//Doing something in your function
//...
return 0, nil
}
`
dumpstackTemplateFile = "debugtool/dumpstack.go"
dumpstackTemplate = `package main
import (
"fmt"
"github.com/TarsCloud/TarsGo/tars"
"github.com/TarsCloud/TarsGo/tars/protocol/res/adminf"
)
func main() {
comm := tars.NewCommunicator()
obj := "{{.App}}.{{.Server}}.{{.Servant}}Obj@tcp -h 127.0.0.1 -p 10014 -t 60000"
app := new(adminf.AdminF)
comm.StringToProxy(obj, app)
ret, err := app.Notify("tars.dumpstack")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(ret)
}
`
)
var makeTemplates = map[string]string{
makeStartTemplateFile: makeStartTemplate,
makefileTemplateFile: makefileTemplate,
configConfTemplateFile: configConfTemplate,
mainGoTemplateFile: mainGoTemplate,
clientGoTemplateFile: clientGoTemplate,
servantTarsTemplateFile: servantTarsTemplate,
servantImpGoTemplateFile: servantImpGoTemplate,
dumpstackTemplateFile: dumpstackTemplate,
}
var cmakeTemplates = map[string]string{
cmakeStartTemplateFile: cmakeStartTemplate,
cmakeListsTxtTemplateFile: cmakeListsTxtTemplate,
clientCMakeListsTxtTemplateFile: clientCMakeListsTxtTemplate,
configConfTemplateFile: configConfTemplate,
mainGoTemplateFile: mainGoTemplate,
clientGoTemplateFile: clientGoTemplate,
servantTarsTemplateFile: servantTarsTemplate,
servantImpGoTemplateFile: servantImpGoTemplate,
dumpstackTemplateFile: dumpstackTemplate,
}
func DoGenProject(p *Project, to string, mgrType string) error {
templates := makeTemplates
if mgrType == consts.CMake {
templates = cmakeTemplates
}
for filename, content := range templates {
filename = path.Join(to, filename)
if err := os.MkdirAll(path.Dir(filename), os.ModePerm); err != nil {
return err
}
t := template.Must(template.New(filename).Parse(content))
buffer := new(bytes.Buffer)
err := t.Execute(buffer, p)
if err != nil {
return err
}
if strings.HasSuffix(filename, ".sh") {
err = os.WriteFile(filename, buffer.Bytes(), os.ModePerm)
} else {
err = os.WriteFile(filename, buffer.Bytes(), 0666)
}
if err != nil {
return err
}
}
if mgrType == consts.CMake {
return bindata.RestoreAssets(to, "cmake")
}
return bindata.RestoreAsset(to, "scripts/makefile.tars.gomod.mk")
}