Skip to content

Commit 1bf3974

Browse files
authored
Merge pull request #104 from NUAA-Open-Source/dev
Go public!
2 parents 0adb8bb + 0937d0b commit 1bf3974

File tree

18 files changed

+481
-80
lines changed

18 files changed

+481
-80
lines changed

.github/workflows/go.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Go
2+
on: [push, pull_request]
3+
jobs:
4+
5+
build:
6+
name: Build
7+
runs-on: ubuntu-latest
8+
steps:
9+
10+
- name: Set up Go 1.12
11+
uses: actions/setup-go@v1
12+
with:
13+
go-version: 1.12
14+
id: go
15+
16+
- name: Check out code into the Go module directory
17+
uses: actions/checkout@v1
18+
19+
- name: Get dependencies
20+
run: |
21+
go get -v -t -d ./...
22+
if [ -f Gopkg.toml ]; then
23+
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
24+
dep ensure
25+
fi
26+
27+
- name: Build
28+
run: go build -v .

ERRORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,5 @@
6060

6161
| Error Code | Error Message (en) | Error Message (zh) |
6262
| :----: | :----: | :----: |
63-
| 20501 | Incorrect password | 密码错误 |
63+
| 20501 | Password incorrect | 密码错误 |
64+
| 20502 | Password required | 需要密码 |

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"Database": "safeu",
5656
"MaxIdleConns": 30,
5757
"MaxOpenConns": 100,
58+
"ConnMaxLifetime": 3600,
5859
"Debug": false
5960
},
6061
"Redis": {
@@ -120,7 +121,7 @@ Cross-site Request Forgery 跨域请求伪造防护设计。
120121
### Build from Source
121122

122123
```bash
123-
$ git clone https://github.com/Triple-Z/safeu-backend.git
124+
$ git clone https://github.com/NUAA-Open-Source/safeu-backend.git
124125
$ cd safeu-backend/
125126
$ go get -u github.com/kardianos/govendor
126127
$ govendor sync
@@ -129,6 +130,15 @@ $ go run main.go
129130

130131
> 要事先做好数据库的建立和配置。
131132
133+
2019.9.1 Update: 支持了 `gomod` 作为依赖管理,可以直接使用 `go build` 直接构建:
134+
135+
```bash
136+
$ git clone https://github.com/NUAA-Open-Source/safeu-backend.git
137+
$ cd safeu-backend/
138+
$ go build
139+
$ ./safeu-backend
140+
```
141+
132142
### Run via Docker
133143

134144
通过 Docker 容器来运行应用。
@@ -206,16 +216,20 @@ $ ./prod-docker-compose.sh up
206216

207217
## Code of Conducts
208218

219+
TBD.
220+
209221
## API Documentation
210222

211223
API 文档位于 `api/` 文件夹下,采用 [OpenAPI 3.0.0](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md) 标准编写。
212-
224+
size
213225
Online Documentation: https://app.swaggerhub.com/apis-docs/a2os/safeu
214226

215227
> 更多信息见 [SafeU 内部文档](https://docs.google.com/document/d/1UiFHogsqDSqw3SrEAnEMukOoJq3fyxqIIEP-OE7ask0/edit?ts=5c56f70d) **需要权限**
216228
217229
## Development Workflow
218230

231+
TBD.
232+
219233
## License
220234

221235
该项目采用 [`Apache 2.0`](LICENSE) 许可证。
@@ -235,3 +249,4 @@ Online Documentation: https://app.swaggerhub.com/apis-docs/a2os/safeu
235249
See the License for the specific language governing permissions and
236250
limitations under the License.
237251
```
252+
$ ./dev-docker-compose.sh up

common/const.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package common
33
var CloudConfig *CloudConfiguration
44

55
const (
6-
DEBUG = false
6+
DEBUG = true
77
MAINTENANCE = false
88
PORT = "8080"
99
)

common/database.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313
)
1414

1515
type Db struct {
16-
User string
17-
Pass string
18-
Host string
19-
Port string
20-
Database string
21-
MaxIdleConns int
22-
MaxOpenConns int
23-
Debug bool
16+
User string
17+
Pass string
18+
Host string
19+
Port string
20+
Database string
21+
MaxIdleConns int
22+
MaxOpenConns int
23+
ConnMaxLifetime int
24+
Debug bool
2425
}
2526

2627
type RedisDb struct {
@@ -66,7 +67,7 @@ func InitDB() *gorm.DB {
6667
db *gorm.DB
6768
e error
6869
)
69-
connectString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&collation=utf8mb4_bin&parseTime=True&loc=%s", DBConf.Master.User, DBConf.Master.Pass, DBConf.Master.Host, DBConf.Master.Port, DBConf.Master.Database, MYSQLTIMEZONE)
70+
connectString := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&collation=utf8mb4_bin&parseTime=True&loc=%s&timeout=10s", DBConf.Master.User, DBConf.Master.Pass, DBConf.Master.Host, DBConf.Master.Port, DBConf.Master.Database, MYSQLTIMEZONE)
7071
// 重试连接
7172
for db, e = gorm.Open("mysql", connectString); e != nil; {
7273
fmt.Println("Gorm Open DB Err: ", e)
@@ -76,11 +77,19 @@ func InitDB() *gorm.DB {
7677

7778
log.Println("Connected to database ", DBConf.Master.User, " ", DBConf.Master.Pass, " ", DBConf.Master.Host, ":", DBConf.Master.Port, " ", DBConf.Master.Database)
7879
db.DB().SetMaxIdleConns(DBConf.Master.MaxIdleConns)
80+
db.DB().SetMaxOpenConns(DBConf.Master.MaxOpenConns)
81+
db.DB().SetConnMaxLifetime(time.Duration(DBConf.Master.ConnMaxLifetime) * time.Second)
7982
DB = db
83+
DB.LogMode(true)
8084
return DB
8185
}
8286

8387
func GetDB() *gorm.DB {
88+
// Ping
89+
err := DB.DB().Ping()
90+
if err != nil {
91+
log.Println("Cannot access the database (PING FAILED)")
92+
}
8493
return DB
8594
}
8695

common/error.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,5 +162,6 @@ var Errors = map[int]string{
162162
20306: "The retrieve code mismatch auth",
163163
20307: "The retrieve code repeat",
164164

165-
20501: "Incorrect password",
165+
20501: "Password incorrect",
166+
20502: "Password required",
166167
}

conf/db.example.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
"User": "your_db_username",
44
"Pass": "your_db_password",
55
"Host": "your_db_ip_address",
6-
"Port": "3306",
6+
"Port": "your_db_port",
77
"Database": "safeu",
88
"MaxIdleConns": 30,
99
"MaxOpenConns": 100,
10+
"ConnMaxLifetime":3600,
1011
"Debug": false
1112
},
1213
"Redis": {
13-
"Host": "safeu-redis",
14-
"Port": "6379",
15-
"Pass": ""
14+
"Host": "your_redis_ip_address",
15+
"Port": "your_redis_port",
16+
"Pass": "your_redis_password"
1617
}
1718
}

conf/redis/redis.conf

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
notify-keyspace-events "Ex"
2-
# requirepass "safeu"
3-
save 60 100
4-
save 900 1
5-
save 300 10
6-
stop-writes-on-bgsave-error no
7-
rdbcompression yes
8-
dbfilename dump.rdb
9-
10-
appendonly no
11-
appendfsync everysec
12-
no-appendfsync-on-rewrite no
13-
auto-aof-rewrite-percentage 100
14-
auto-aof-rewrite-min-size 64mb
15-
16-
dir /data
1+
notify-keyspace-events "Ex"
2+
# requirepass "safeu"
3+
save 60 100
4+
save 900 1
5+
save 300 10
6+
stop-writes-on-bgsave-error no
7+
rdbcompression yes
8+
dbfilename dump.rdb
9+
10+
appendonly no
11+
appendfsync everysec
12+
no-appendfsync-on-rewrite no
13+
auto-aof-rewrite-percentage 100
14+
auto-aof-rewrite-min-size 64mb
15+
16+
dir /data

deployments/dev-safeu/docker-compose.yml

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ services:
2828
- "9000:8080"
2929
networks:
3030
- safeu-app-net
31-
depends_on:
31+
# depends_on:
3232
# - db
33-
- safeu-redis
33+
# - safeu-redis
3434
restart: always
3535

3636
# db:
@@ -48,20 +48,20 @@ services:
4848
# networks:
4949
# - safeu-app-net
5050
# restart: on-failure
51-
52-
safeu-redis:
53-
image: redis:5.0.3-alpine
54-
environment:
55-
- TZ=Asia/Shanghai
56-
volumes:
57-
- ../../conf/redis/redis.conf:/usr/local/etc/redis/redis.conf
58-
- ../../data-dev/redis:/data # for redis persistent storage
59-
entrypoint: redis-server /usr/local/etc/redis/redis.conf
60-
ports:
61-
- "6379:6379"
62-
networks:
63-
- safeu-app-net
64-
restart: on-failure
51+
52+
# safeu-redis:
53+
# image: redis:5.0.3-alpine
54+
# environment:
55+
# - TZ=Asia/Shanghai
56+
# volumes:
57+
# - ../../conf/redis/redis.conf:/usr/local/etc/redis/redis.conf
58+
# - ../../data-dev/redis:/data # for redis persistent storage
59+
# entrypoint: redis-server /usr/local/etc/redis/redis.conf
60+
# ports:
61+
# - "6379:6379"
62+
# networks:
63+
# - safeu-app-net
64+
# restart: on-failure
6565

6666
networks:
6767
safeu-app-net:

deployments/prod-safeu/docker-compose.yml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ services:
3030
- "8080:8080"
3131
networks:
3232
- safeu-app-net
33-
depends_on:
34-
- safeu-redis
33+
# depends_on:
34+
# - safeu-redis
3535
restart: on-failure
3636

3737
safeu2:
@@ -47,8 +47,8 @@ services:
4747
- "8081:8080"
4848
networks:
4949
- safeu-app-net
50-
depends_on:
51-
- safeu-redis
50+
# depends_on:
51+
# - safeu-redis
5252
restart: on-failure
5353

5454
safeu3:
@@ -64,8 +64,8 @@ services:
6464
- "8082:8080"
6565
networks:
6666
- safeu-app-net
67-
depends_on:
68-
- safeu-redis
67+
# depends_on:
68+
# - safeu-redis
6969
restart: on-failure
7070

7171
# db:
@@ -82,19 +82,19 @@ services:
8282
# networks:
8383
# - dbnet
8484
# restart: on-failure
85-
safeu-redis:
86-
image: redis:5.0.3-alpine
87-
environment:
88-
- TZ=Asia/Shanghai
89-
volumes:
90-
- ../../conf/redis/redis.conf:/usr/local/etc/redis/redis.conf
91-
- ../../data/redis:/data # for redis persistent storage
92-
entrypoint: redis-server /usr/local/etc/redis/redis.conf
93-
ports:
94-
- "6379:6379"
95-
networks:
96-
- safeu-app-net
97-
restart: on-failure
85+
# safeu-redis:
86+
# image: redis:5.0.3-alpine
87+
# environment:
88+
# - TZ=Asia/Shanghai
89+
# volumes:
90+
# - ../../conf/redis/redis.conf:/usr/local/etc/redis/redis.conf
91+
# - ../../data/redis:/data # for redis persistent storage
92+
# entrypoint: redis-server /usr/local/etc/redis/redis.conf
93+
# ports:
94+
# - "6379:6379"
95+
# networks:
96+
# - safeu-app-net
97+
# restart: on-failure
9898

9999
networks:
100100
safeu-app-net:

0 commit comments

Comments
 (0)