-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
135 lines (124 loc) · 2.82 KB
/
server.go
File metadata and controls
135 lines (124 loc) · 2.82 KB
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
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/codegangsta/cli"
"gopkg.in/redis.v5"
"strconv"
"github.com/qjw/git-notify/qywechat"
"github.com/qjw/git-notify/gateway"
"github.com/qjw/git-notify/utils"
"github.com/qjw/kelly"
)
func GetCorpFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
EnvVar: "GIT_NOTIFY_CORP_ID",
Name: "corp_id",
Usage: "企业微信corpid",
Value: "",
},
cli.StringFlag{
EnvVar: "GIT_NOTIFY_CORP_SECRET",
Name: "corp_secret",
Usage: "企业微信应用secret",
Value: "",
},
cli.Int64Flag{
EnvVar: "GIT_NOTIFY_CORP_SECRET",
Name: "corp_appid",
Usage: "企业微信应用id",
Value: 0,
},
cli.StringFlag{
EnvVar: "GIT_NOTIFY_CORP_SECRET",
Name: "receive_department",
Usage: "接收消息的部门",
Value: "",
},
}
}
func CombineSliceArray(first []cli.Flag, lst ...[]cli.Flag) []cli.Flag {
for _, item := range lst {
first = append(first, item...)
}
return first
}
var serverCmd = cli.Command{
Name: "server",
Usage: "启动账户服务器",
Action: func(c *cli.Context) {
if err := server(c); err != nil {
log.Fatal(err)
}
},
Flags: CombineSliceArray([]cli.Flag{
cli.StringFlag{
EnvVar: "GIT_NOTIFY_HOST",
Name: "host",
Usage: "服务器主机",
Value: "localhost",
},
cli.IntFlag{
EnvVar: "GIT_NOTIFY_PORT",
Name: "port",
Usage: "服务器端口",
Value: 13577,
},
cli.StringFlag{
EnvVar: "GIT_NOTIFY_REDIS_HOST",
Name: "redis_host",
Usage: "redis服务器主机",
Value: "localhost",
},
cli.IntFlag{
EnvVar: "GIT_NOTIFY_REDIS_PORT",
Name: "redis_port",
Usage: "redis服务器端口",
Value: 6379,
},
cli.IntFlag{
EnvVar: "GIT_NOTIFY_REDIS_DB",
Name: "redis_db",
Usage: "redis服务器数据库",
Value: 9,
},
cli.StringFlag{
EnvVar: "GIT_NOTIFY_REDIS_PASSWORD",
Name: "redis_password",
Usage: "redis服务器密码",
Value: "",
},
},
GetCorpFlags(),
GetGitlabFlags(),
),
}
/**
初始化Redis
*/
func initRedis(c *cli.Context) *redis.Client{
log.Print("start to init redis")
redisClient := redis.NewClient(&redis.Options{
Addr: c.String("redis_host") + ":" + strconv.Itoa(c.Int("redis_port")),
Password: c.String("redis_password"),
DB: c.Int("redis_db"),
})
if err := redisClient.Ping().Err(); err != nil {
log.Fatal("failed to connect redis")
}
log.Print("init redis success")
return redisClient
}
func server(c *cli.Context) error {
utils.InitLog(c)
redisClient := initRedis(c)
remoteApi, err := setupRemote(c)
if err != nil {
return err
}
r := kelly.NewClassic()
wxapi := qywechat.InitMessageApi(c, redisClient)
gateway.InitializeApiRoutes(r.Group("/gateway"),remoteApi,wxapi)
r.Run(c.String("host") + ":" + strconv.Itoa(c.Int("port")))
return nil
}