Skip to content

Commit 6cf0a82

Browse files
author
chenjie
committed
update
1 parent c02563f commit 6cf0a82

31 files changed

+445
-368
lines changed

codegen/tml/api/status/template_status.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func init() {
6565
}
6666
func CreatePathAndFile() {
6767
var e error
68-
if e = os.MkdirAll(path, 0755); e != nil {
68+
if e = os.MkdirAll(path+"deps", 0755); e != nil {
6969
panic(fmt.Sprintf("make dir:%s error:%s", path, e))
7070
}
7171
file, e = os.OpenFile(path+name, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)

codegen/tml/cmd/template_cmd.go

+29
Original file line numberDiff line numberDiff line change
@@ -286,15 +286,29 @@ goto :end
286286
:end
287287
pause
288288
exit /b 0`
289+
const textprobe = `#!/bin/sh
290+
# kubernetes probe port
291+
port8000=*netstat -ltn | grep 8000 | wc -l*
292+
port9000=*netstat -ltn | grep 9000 | wc -l*
293+
if [[ $port9000 -eq 1 && $port8000 -eq 1 ]]
294+
then
295+
exit 0
296+
else
297+
exit 1
298+
fi
299+
`
289300

290301
const path = "./"
291302
const namebash = "cmd.sh"
292303
const namebat = "cmd.bat"
304+
const nameprobe = "probe.sh"
293305

294306
var tmlbash *template.Template
295307
var tmlbat *template.Template
308+
var tmlprobe *template.Template
296309
var filebash *os.File
297310
var filebat *os.File
311+
var fileprobe *os.File
298312

299313
type Data struct {
300314
Pname string
@@ -311,6 +325,10 @@ func init() {
311325
if e != nil {
312326
panic(fmt.Sprintf("create template for %s error:%s", path+namebat, e))
313327
}
328+
tmlprobe, e = template.New("probe").Parse(strings.Replace(textprobe, "*", "`", -1))
329+
if e != nil {
330+
panic(fmt.Sprintf("create template for %s error:%s", path+nameprobe, e))
331+
}
314332
}
315333
func CreatePathAndFile() {
316334
var e error
@@ -329,6 +347,14 @@ func CreatePathAndFile() {
329347
if e != nil {
330348
panic(fmt.Sprintf("make file:%s error:%s", path+namebat, e))
331349
}
350+
fileprobe, e = os.OpenFile(path+nameprobe, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
351+
if e != nil {
352+
panic(fmt.Sprintf("make file:%s error:%s", path+nameprobe, e))
353+
}
354+
e = os.Chmod(path+nameprobe, 0755)
355+
if e != nil {
356+
panic(fmt.Sprintf("change file:%s execute right error:%s", path+nameprobe, e))
357+
}
332358
}
333359
func Execute(pname, gname string) {
334360
if e := tmlbash.Execute(filebash, &Data{Pname: pname, Gname: gname}); e != nil {
@@ -337,4 +363,7 @@ func Execute(pname, gname string) {
337363
if e := tmlbat.Execute(filebat, &Data{Pname: pname, Gname: gname}); e != nil {
338364
panic(fmt.Sprintf("write content into file:%s from template error:%s", path+namebat, e))
339365
}
366+
if e := tmlprobe.Execute(fileprobe, nil); e != nil {
367+
panic(fmt.Sprintf("write content into file:%s from template error:%s", path+nameprobe, e))
368+
}
340369
}

codegen/tml/config/template_config.go

+18-18
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func Close() {
121121
//SourceConfig can't hot update
122122
type sourceConfig struct {
123123
Rpc *RpcConfig $json:"rpc"$
124-
Http *HttpConfig $json:"http"$
124+
Web *WebConfig $json:"web"$
125125
DB map[string]*DBConfig $json:"db"$ //key xx_db
126126
Redis map[string]*RedisConfig $json:"redis"$ //key xx_redis
127127
KafkaPub map[string]*KafkaPubConfig $json:"kafka_pub"$ //key topic name
@@ -138,20 +138,20 @@ type RpcConfig struct {
138138
RpcHeartProbe ctime.Duration $json:"rpc_heart_probe"$ //default 1.5s
139139
}
140140
141-
//HttpConfig -
142-
type HttpConfig struct {
141+
//WebConfig -
142+
type WebConfig struct {
143143
//server
144-
HttpPort uint $json:"http_port"$
145-
HttpTimeout ctime.Duration $json:"http_timeout"$ //default 500ms
146-
HttpStaticFile string $json:"http_staticfile"$
147-
HttpCertFile string $json:"http_certfile"$
148-
HttpKeyFile string $json:"http_keyfile"$
144+
WebPort uint $json:"web_port"$
145+
WebTimeout ctime.Duration $json:"web_timeout"$ //default 500ms
146+
WebStaticFile string $json:"web_staticfile"$
147+
WebCertFile string $json:"web_certfile"$
148+
WebKeyFile string $json:"web_keyfile"$
149149
//cors
150-
HttpCors *HttpCorsConfig $json:"http_cors"$
150+
WebCors *WebCorsConfig $json:"web_cors"$
151151
}
152152
153-
//HttpCorsConfig -
154-
type HttpCorsConfig struct {
153+
//WebCorsConfig -
154+
type WebCorsConfig struct {
155155
CorsOrigin []string $json:"cors_origin"$
156156
CorsHeader []string $json:"cors_header"$
157157
CorsExpose []string $json:"cors_expose"$
@@ -230,11 +230,11 @@ func (c *sourceConfig) validate() {
230230
if c.Rpc.RpcHeartProbe == 0 {
231231
c.Rpc.RpcHeartProbe = ctime.Duration(1500 * time.Millisecond)
232232
}
233-
if c.Http.HttpPort == 0 {
234-
c.Http.HttpPort = 8000
233+
if c.Web.WebPort == 0 {
234+
c.Web.WebPort = 8000
235235
}
236-
if c.Http.HttpTimeout == 0 {
237-
c.Http.HttpTimeout = ctime.Duration(time.Millisecond * 500)
236+
if c.Web.WebTimeout == 0 {
237+
c.Web.WebTimeout = ctime.Duration(time.Millisecond * 500)
238238
}
239239
for _, dbc := range c.DB {
240240
if dbc.Username == "" {
@@ -406,9 +406,9 @@ func GetRpcConfig() *RpcConfig {
406406
return sc.Rpc
407407
}
408408
409-
//GetHttpConfig get the http net config
410-
func GetHttpConfig() *HttpConfig {
411-
return sc.Http
409+
//GetWebConfig get the web net config
410+
func GetWebConfig() *WebConfig {
411+
return sc.Web
412412
}
413413
414414
//GetDB get a db client by db's logic name

codegen/tml/configfile/template_configfile.go

+6-9
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ import (
88

99
const textsource = `{
1010
"rpc":{
11-
"rpc_port":9000,
1211
"rpc_verifydata":"test",
13-
"rpc_timeout":"200ms",
1412
"rpc_timeout":"500ms",
1513
"rpc_conn_timeout":"1s",
1614
"rpc_heart_timeout":"5s",
1715
"rpc_heart_probe":"1.5s"
1816
},
19-
"http":{
20-
"http_port":8000,
21-
"http_timeout":"200ms",
22-
"http_staticfile":"./src",
23-
"http_certfile":"",
24-
"http_keyfile":"",
25-
"http_cors":{
17+
"web":{
18+
"web_timeout":"200ms",
19+
"web_staticfile":"./src",
20+
"web_certfile":"",
21+
"web_keyfile":"",
22+
"web_cors":{
2623
"cors_origin":["*"],
2724
"cors_header":["*"],
2825
"cors_expose":[]

codegen/tml/dao/template_dao.go

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,29 @@ import (
99
const text = `package dao
1010
1111
import (
12-
//"path/to/dependence/grpc/service/example"
12+
//"time"
13+
14+
//"{{.}}/api"
15+
//example "{{.}}/api/deps/example"
16+
//"{{.}}/config"
1317
)
1418
15-
//var ExampleApi *example.Client
19+
//var ExampleRpcApi example.ExampleRpcClient
20+
//var ExampleWebApi example.ExampleWebClient
1621
1722
//NewApi create all dependent service's api we need in this program
1823
//example grpc client,http client
19-
func NewApi() {
20-
//ExampleApi = example.NewClient()
24+
func NewApi() error {
25+
//var e error
26+
//rc := config.GetRpcConfig()
27+
//if ExampleRpcApi, e = example.NewExampleRpcClient(rc.RpcTimeout, rc.RpcConnTimeout, rc.RpcHeartTimeout, rc.RpcHeartProbe, api.Group, api.Name, []byte(rc.RpcVerifyData), nil, nil); e != nil {
28+
// return e
29+
//}
30+
//wc := config.GetWebConfig()
31+
//if ExampleWebApi, e = example.NewExampleWebClient(time.Duration(wc.WebTimeout), api.Group, api.Name, nil, nil); e != nil {
32+
// return e
33+
//}
34+
return nil
2135
}`
2236

2337
const path = "./dao/"

codegen/tml/git/template_ignore.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,10 @@ const text = `*
3232
!/service/*
3333
!/service/**/
3434
!/service/**/*
35-
!/source/
36-
!/source/*
37-
!/source/**/
38-
!/source/**/*
3935
!AppConfig.json
4036
!cmd.sh
4137
!cmd.bat
38+
!probe.sh
4239
!deployment.yaml
4340
!Dockerfile
4441
!go.mod

codegen/tml/gomod/template_gomod.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const text = `module {{.}}
1111
go 1.15
1212
1313
require (
14-
github.com/chenjie199234/Corelib v0.0.7
14+
github.com/chenjie199234/Corelib v0.0.8
1515
github.com/fsnotify/fsnotify v1.4.9
1616
github.com/golang/protobuf v1.4.3
1717
github.com/segmentio/kafka-go v0.4.8

codegen/tml/kubernetes/kubernetes.go

+14-21
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import (
99
const dockerfiletext = `FROM busybox:1.28
1010
1111
RUN mkdir /root/app
12-
13-
COPY main AppConfig.json SourceConfig.json /root/app
14-
1512
WORKDIR /root/app
16-
17-
SHELL ["sh","-c"]
18-
13+
SHELL ["/bin/sh","-c"]
14+
ENV RUN_ENV <RUN_ENV>
15+
ENV DISCOVERY_SERVER_GROUP <DISCOVERY_SERVER_GROUP>
16+
ENV DISCOVERY_SERVER_NAME <DISCOVERY_SERVER_NAME>
17+
ENV DISCOVERY_SERVER_PORT <DISCOVERY_SERVER_PORT>
18+
ENV DISCOVERY_SERVER_VERIFY_DATA <DISCOVERY_SERVER_VERIFY_DATA>
19+
COPY main probe.sh AppConfig.json SourceConfig.json ./
1920
ENTRYPOINT ["./main"]`
2021

2122
const deploymenttext = `apiVersion: apps/v1
@@ -58,27 +59,19 @@ spec:
5859
value: {{.ProjectName}}
5960
- name: DEPLOY_ENV
6061
value: kubernetes
61-
- name: RUN_ENV
62-
value: <RUN_ENV>
63-
- name: DISCOVERY_SERVER_GROUP
64-
value: <DISCOVERY_SERVER_GROUP>
65-
- name: DISCOVERY_SERVER_NAME
66-
value: <DISCOVERY_SERVER_NAME>
67-
- name: DISCOVERY_SERVER_PORT
68-
value: <DISCOVERY_SERVER_PORT>
69-
- name: DISCOVERY_SERVER_VERIFY_DATA
70-
value: <DISCOVERY_SERVER_VERIFY_DATA>
7162
livenessProbe:
72-
tcpSocket:
73-
port: 8000
63+
exec:
64+
command:
65+
- ./probe.sh
7466
initialDelaySeconds: 2
7567
timeoutSeconds: 1
7668
periodSeconds: 1
7769
successThreshold: 1
7870
failureThreshold: 3
7971
readinessProbe:
80-
tcpSocket:
81-
port: 8000
72+
exec:
73+
command:
74+
- ./probe.sh
8275
initialDelaySeconds: 2
8376
timeoutSeconds: 1
8477
periodSeconds: 1
@@ -148,7 +141,7 @@ spec:
148141
- path: /
149142
backend:
150143
serviceName: {{.ProjectName}}-service
151-
serviceport: 80{{ end }}`
144+
servicePort: 80{{ end }}`
152145

153146
const path = "./"
154147
const dockerfilename = "Dockerfile"

codegen/tml/mainfile/template_main.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func main() {
3636
}
3737
}
3838
//start the whole business service
39-
service.StartService()
39+
if e := service.StartService(); e != nil {
40+
log.Error(e)
41+
return
42+
}
4043
//start low level net service
4144
ch := make(chan os.Signal, 1)
4245
wg := &sync.WaitGroup{}
@@ -66,16 +69,16 @@ func main() {
6669
select {
6770
case <-tmer.C:
6871
rpcc := config.GetRpcConfig()
69-
webc := config.GetHttpConfig()
72+
webc := config.GetWebConfig()
7073
regmsg := &discovery.RegMsg{}
7174
if webc != nil {
72-
if webc.HttpKeyFile != "" && webc.HttpCertFile != "" {
75+
if webc.WebKeyFile != "" && webc.WebCertFile != "" {
7376
regmsg.WebScheme = "https"
7477
} else {
7578
regmsg.WebScheme = "http"
7679
}
7780
if webc.HttpPort != 0 {
78-
regmsg.WebPort = int(webc.HttpPort)
81+
regmsg.WebPort = int(webc.WebPort)
7982
}
8083
}
8184
if rpcc != nil {

codegen/tml/server/xrpc/template_xrpc.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,26 @@ import (
1919
"github.com/chenjie199234/Corelib/log"
2020
"github.com/chenjie199234/Corelib/rpc"
2121
"github.com/chenjie199234/Corelib/rpc/mids"
22-
"github.com/chenjie199234/Corelib/stream"
2322
)
2423
2524
var s *rpc.RpcServer
2625
2726
//StartRpcServer -
2827
func StartRpcServer() {
2928
c := config.GetRpcConfig()
30-
rpcc := &stream.InstanceConfig{
31-
HeartbeatTimeout: time.Duration(c.RpcHeartTimeout),
32-
HeartprobeInterval: time.Duration(c.RpcHeartProbe),
33-
TcpC: &stream.TcpConfig{
34-
ConnectTimeout: time.Duration(c.RpcConnTimeout),
35-
AppWriteBufferNum: 65535,
36-
},
29+
rpcc := &rpc.Config{
30+
Timeout: time.Duration(c.RpcTimeout),
31+
ConnTimeout: time.Duration(c.RpcConnTimeout),
32+
HeartTimeout: time.Duration(c.RpcHeartTimeout),
33+
HeartPorbe: time.Duration(c.RpcHeartProbe),
34+
GroupNum: 1,
35+
SocketRBuf: 1024,
36+
SocketWBuf: 1024,
37+
MaxMsgLen: 65535,
38+
MaxBufferedWriteMsgNum: 1024,
3739
}
3840
var e error
39-
if s, e = rpc.NewRpcServer(rpcc, api.Group, api.Name, []byte(c.RpcVerifydata), time.Duration(c.RpcTimeout)); e != nil {
41+
if s, e = rpc.NewRpcServer(rpcc, api.Group, api.Name, []byte(c.RpcVerifydata)); e != nil {
4042
log.Error("[xrpc] new rpc server error:", e)
4143
return
4244
}

codegen/tml/server/xweb/template_xweb.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ var s *web.WebServer
2525
2626
//StartWebServer -
2727
func StartWebServer() {
28-
c := config.GetHttpConfig()
28+
c := config.GetWebConfig()
2929
webc := &web.Config{
30-
Timeout: time.Duration(c.HttpTimeout),
31-
StaticFileRootPath: c.HttpStaticFile,
30+
Timeout: time.Duration(c.WebTimeout),
31+
StaticFileRootPath: c.WebStaticFile,
3232
MaxHeader: 1024,
3333
ReadBuffer: 1024,
3434
WriteBuffer: 1024,
3535
}
36-
if c.HttpCors != nil {
36+
if c.WebCors != nil {
3737
webc.Cors = &web.CorsConfig{
38-
AllowedOrigin: c.HttpCors.CorsOrigin,
39-
AllowedHeader: c.HttpCors.CorsHeader,
40-
ExposeHeader: c.HttpCors.CorsExpose,
38+
AllowedOrigin: c.WebCors.CorsOrigin,
39+
AllowedHeader: c.WebCors.CorsHeader,
40+
ExposeHeader: c.WebCors.CorsExpose,
4141
AllowCredentials: true,
4242
MaxAge: 24 * time.Hour,
4343
}
@@ -62,7 +62,7 @@ func StartWebServer() {
6262
//return
6363
//}
6464
65-
if e = s.StartWebServer(fmt.Sprintf(":%d", c.HttpPort), c.HttpCertFile, c.HttpKeyFile); e != nil {
65+
if e = s.StartWebServer(fmt.Sprintf(":%d", c.WebPort), c.WebCertFile, c.WebKeyFile); e != nil {
6666
log.Error("[xweb] start web server error:", e)
6767
return
6868
}

0 commit comments

Comments
 (0)