/
gateway.go
74 lines (66 loc) · 1.8 KB
/
gateway.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
package main
import (
"time"
"github.com/henrylee2cn/cfgo"
tp "github.com/henrylee2cn/teleport"
micro "github.com/xiaoenai/tp-micro"
"github.com/xiaoenai/tp-micro/gateway"
"github.com/xiaoenai/tp-micro/gateway/helper/agent"
"github.com/xiaoenai/tp-micro/gateway/helper/gray"
"github.com/xiaoenai/tp-micro/gateway/logic"
"github.com/xiaoenai/tp-micro/gateway/types"
"github.com/xiaoenai/tp-micro/model/etcd"
"github.com/xiaoenai/tp-micro/model/mysql"
"github.com/xiaoenai/tp-micro/model/redis"
)
type config struct {
Gw gateway.Config `yaml:"gw"`
GraySocketClient micro.CliConfig `yaml:"gray_socket_client"`
GrayEtcd etcd.EasyConfig `yaml:"gray_etcd"`
Redis redis.Config `yaml:"redis"`
Mysql mysql.Config `yaml:"mysql"`
}
func (c *config) Reload(bind cfgo.BindFunc) error {
c.Gw.OuterHttpServer.AllowCross = true
err := bind()
return err
}
func main() {
logic.SetApiVersion("v1")
cfg := config{
Gw: *gateway.NewConfig(),
GraySocketClient: micro.CliConfig{
Failover: 3,
HeartbeatSecond: 60,
},
GrayEtcd: etcd.EasyConfig{
Endpoints: []string{"http://127.0.0.1:2379"},
},
Redis: *redis.NewConfig(),
Mysql: *mysql.NewConfig(),
}
cfgo.AllowAppsShare(true)
cfgo.MustReg("gateway", &cfg)
biz := types.DefaultBusiness()
redisClient, err := redis.NewClient(&cfg.Redis)
if err != nil {
tp.Fatalf("%v", err)
}
agent.Init(redisClient, redisClient)
biz.SocketHooks = agent.GetSocketHooks()
_, err = gray.SetGray(biz, cfg.GraySocketClient, cfg.GrayEtcd, cfg.Mysql, cfg.Redis, nil)
if err != nil {
tp.Fatalf("%v", err)
}
go func() {
t := time.NewTicker(time.Second)
for {
<-t.C
tp.Infof("total conn: %d", gateway.TotalConn())
}
}()
err = gateway.Run(cfg.Gw, biz, nil)
if err != nil {
tp.Fatalf("%v", err)
}
}