Merge branch 'master' of http://gitlab.fjmaimaimai.com/mmm-go/ability
正在显示
5 个修改的文件
包含
266 行增加
和
0 行删除
README.md
0 → 100644
1 | +## 目录 | ||
2 | +* [代码声明](#代码声明) | ||
3 | + * [命名规范](#命名规范) | ||
4 | + * [项目约定](#项目约定) | ||
5 | + * [注意事项](#注意事项) | ||
6 | + | ||
7 | +### 代码声明 | ||
8 | + | ||
9 | +#### 命名规范 | ||
10 | + | ||
11 | +1.包名 | ||
12 | + | ||
13 | +``` | ||
14 | +* | ||
15 | +package名和目录保持一致,需避免和标准库冲突 | ||
16 | +小写 | ||
17 | +* | ||
18 | +package comm | ||
19 | +``` | ||
20 | + | ||
21 | +2.命名 | ||
22 | + | ||
23 | +``` | ||
24 | +package pkg | ||
25 | + | ||
26 | +*1.错误* | ||
27 | +/* | ||
28 | +定义在包的首部,所有错误都定义在一起, | ||
29 | +并且以Err开头 | ||
30 | +*/ | ||
31 | +var ErrFooBar = fmt.Errorf("pkg: ...") | ||
32 | + | ||
33 | +*2.变量* | ||
34 | +/* | ||
35 | +采用驼峰命名 | ||
36 | +*/ | ||
37 | +var fooBar int | ||
38 | + | ||
39 | +*3.常量* | ||
40 | +/* | ||
41 | +大写+下划线 | ||
42 | +*/ | ||
43 | +var( | ||
44 | + FOO = 1 | ||
45 | + BAR = 2 | ||
46 | + FOO_BAR = 3 | ||
47 | +) | ||
48 | + | ||
49 | +*4.结构* | ||
50 | +/* | ||
51 | +采用驼峰命名法 | ||
52 | +*/ | ||
53 | +type FooBar struct{ | ||
54 | + foo int | ||
55 | + Bar string | ||
56 | +} | ||
57 | + | ||
58 | + | ||
59 | +*5.方法接口* | ||
60 | +/* | ||
61 | +采用驼峰命名法 | ||
62 | +非对外方法,首字母需为小写 | ||
63 | +*/ | ||
64 | +func foo(){} | ||
65 | +func Foo(){} | ||
66 | +``` | ||
67 | + | ||
68 | +3.目录结构 | ||
69 | +``` | ||
70 | +* conf //配置 | ||
71 | + * dev.conf | ||
72 | + * prod.conf | ||
73 | +* controller //控制器 | ||
74 | + * v1 | ||
75 | + * auth.go | ||
76 | +* internal | ||
77 | + * repository //存储层 | ||
78 | + * user.go | ||
79 | + * *_mock.go | ||
80 | +* model //模型 | ||
81 | +* protocol //协议层 | ||
82 | +* routers //路由 | ||
83 | +* service //逻辑层 | ||
84 | + * auth | ||
85 | +* static //静态数据 | ||
86 | +* views //视图 | ||
87 | + | ||
88 | +* main.go | ||
89 | +* Dockerfile | ||
90 | +* README.md | ||
91 | +* *.sh //脚本 | ||
92 | +``` | ||
93 | + | ||
94 | +#### 项目约定 | ||
95 | + | ||
96 | +1. 入参跟返回值都要定义一个协议结构 protocol.XxxRequest *protocol.XxxResponse,方便扩展 | ||
97 | +2. 函数方法的变量都定义在函数的头几行 var( ... ) | ||
98 | +3. 服务调用尽量封装成接口,方便测试扩展 比如:ISmsServe{ Send() error } | ||
99 | +4. 公用的基础代码库,需要做一下封装放在 [gocomm](http://gitlab.fjmaimaimai.com/mmm-go/gocomm) ,方便在其他项目中调用 | ||
100 | +4. 可以使用代码生成固定格式, [项目地址](http://gitlab.fjmaimaimai.com/mmm-go/gencode) 脚本:.\gencode.exe new -c Auth -m Login | ||
101 | + | ||
102 | +``` | ||
103 | +package pkg | ||
104 | +//Login | ||
105 | +func(this *AuthController)Login(){ | ||
106 | + var msg *mybeego.Message | ||
107 | + defer func(){ | ||
108 | + this.Resp(msg) | ||
109 | + }() | ||
110 | + var request *protocol.LoginRequest | ||
111 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
112 | + log.Error(err) | ||
113 | + msg = mybeego.NewMessage(1) | ||
114 | + return | ||
115 | + } | ||
116 | + if b,m :=this.Valid(request);!b{ | ||
117 | + msg = m | ||
118 | + return | ||
119 | + } | ||
120 | + msg = this.GenMessage(auth.Login(request)) | ||
121 | +} | ||
122 | + | ||
123 | +/*Login */ | ||
124 | +type LoginRequest struct { | ||
125 | + Xxx string`json:"xxx" valid:"Required"` | ||
126 | +} | ||
127 | +type LoginResponse struct { | ||
128 | +} | ||
129 | + | ||
130 | +func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){ | ||
131 | + var ( | ||
132 | + | ||
133 | + ) | ||
134 | + rsp =&protocol.LoginResponse{} | ||
135 | + return | ||
136 | +} | ||
137 | +``` | ||
138 | + | ||
139 | + | ||
140 | +#### 注意事项 | ||
141 | + | ||
142 | +1. 启动一个groutine(eg:go func(){}()),需要在函数内进行recover,不然groutine里面panic,会导致外部程序一起崩溃掉; | ||
143 | +2. 当接受者是map, chan, func, 不要使用指针传递,因为它们本身就是引用类型 | ||
144 | +3. 当接受者类型是一个结构体并且很庞大,或者是一个大数组,建议使用指针传递来提高性能,其他场景使用值传递即可 | ||
145 | +4. 当接受者是一个结构体,并且包含了sync.Mutex或者类似的用于同步的成员。必须使用指针传递,避免成员拷贝 | ||
146 | +5. 当函数内部需要修改接受者,必须使用指针传递 | ||
147 | +6. 声明空的slice应该使用下面的格式: var t []string 不要使用 t := []string{} ,前者声明了一个nil slice而后者是一个长度为0的非nil的slice。 | ||
148 | + |
rollback.sh
0 → 100755
1 | +#!/bin/bash | ||
2 | + | ||
3 | +pwd=`pwd` | ||
4 | +target=`basename $pwd` | ||
5 | + | ||
6 | +# Kill running program | ||
7 | +pid=`ps -C ${target} -o pid=` | ||
8 | +if [ -n "$pid" ]; then | ||
9 | + | ||
10 | + echo "Stopping old version, PID: ${pid}" | ||
11 | + if [ "$1" = "-f" ]; then | ||
12 | + # force shutdown | ||
13 | + echo "Force shutdown..." | ||
14 | + kill $(ps -C ${target} -o pid=) | ||
15 | + else | ||
16 | + kill -s 2 $(ps -C ${target} -o pid=) | ||
17 | + fi | ||
18 | + | ||
19 | + # wait for program to stop | ||
20 | + pid=`ps -C ${target} -o pid=` | ||
21 | + while [ -n "$pid" ]; do | ||
22 | + sleep 1 | ||
23 | + done | ||
24 | +fi | ||
25 | + | ||
26 | +# Rollback | ||
27 | +if [ -f "${target}-backup" ]; then | ||
28 | + echo "Rolling back..." | ||
29 | + if [ -f "${target}" ]; then | ||
30 | + rm "${target}" | ||
31 | + fi | ||
32 | + mv ${target}-backup ${target} | ||
33 | + echo "Rollback Complete" | ||
34 | +fi | ||
35 | + | ||
36 | +# run | ||
37 | +echo "Starting..." | ||
38 | +./run.sh ${target} | ||
39 | +echo "Done" |
run.sh
0 → 100755
start.sh
0 → 100755
1 | +#!/bin/bash | ||
2 | + | ||
3 | +pwd=`pwd` | ||
4 | +target=`basename $pwd` | ||
5 | +# kill | ||
6 | +pid=`ps -C ${target} -o pid=` | ||
7 | +if [ -n "$pid" ]; then | ||
8 | + echo "Stopping old version, PID: ${pid}" | ||
9 | + if [ "$1" = "-f" ]; then | ||
10 | + # force shutdown | ||
11 | + echo "Force shutdown..." | ||
12 | + kill -9 $(ps -C ${target} -o pid=) | ||
13 | + else | ||
14 | + kill -9 $(ps -C ${target} -o pid=) | ||
15 | + fi | ||
16 | + # wait for program to stop | ||
17 | + pid=`ps -C ${target} -o pid=` | ||
18 | + while [ -n "$pid" ]; do | ||
19 | + sleep 1 | ||
20 | + done | ||
21 | +fi | ||
22 | + | ||
23 | +# upgrade | ||
24 | +if [ -f "${target}-new" ]; then | ||
25 | + echo "Upgrading..." | ||
26 | + if [ -f "${target}-backup" ]; then | ||
27 | + backupdt=`date +%Y%m%d-%H` | ||
28 | + mv "${target}-backup" "${target}-backup-${backupdt}" | ||
29 | + fi | ||
30 | + | ||
31 | + mv ${target} ${target}-backup | ||
32 | + mv ${target}-new ${target} | ||
33 | + | ||
34 | + echo "Upgrade Complete" | ||
35 | +fi | ||
36 | + | ||
37 | +# run | ||
38 | +echo "Starting..." | ||
39 | +./run.sh ${target} | ||
40 | +echo "Done" |
stop.sh
0 → 100755
1 | +#!/bin/bash | ||
2 | + | ||
3 | +pwd=`pwd` | ||
4 | +target=`basename $pwd` | ||
5 | + | ||
6 | +# kill | ||
7 | +pid=`ps -C ${target} -o pid=` | ||
8 | +if [ -n "$pid" ]; then | ||
9 | + | ||
10 | + echo "Stopping old version, PID: ${pid}" | ||
11 | + if [ "$1" = "-f" ]; then | ||
12 | + # force shutdown | ||
13 | + echo "Force shutdown..." | ||
14 | + kill -9 $(ps -C ${target} -o pid=) | ||
15 | + else | ||
16 | + kill -9 $(ps -C ${target} -o pid=) | ||
17 | + fi | ||
18 | + | ||
19 | + # wait for program to stop | ||
20 | + pid=`ps -C ${target} -o pid=` | ||
21 | + while [ -n "$pid" ]; do | ||
22 | + sleep 1 | ||
23 | + done | ||
24 | + | ||
25 | +fi | ||
26 | + | ||
27 | +echo "Done" |
-
请 注册 或 登录 后发表评论