http.go
1.56 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
package main
import (
"fmt"
"os"
"os/signal"
"pro2d/common"
"pro2d/common/components"
"pro2d/common/db/mongoproxy"
"pro2d/common/etcd"
"pro2d/common/logger"
"pro2d/models"
"syscall"
)
type AccountServer struct {
components.IHttp
EtcdClient *etcd.EtcdClient
}
func NewAccountServer(version string, port ...string) *AccountServer {
return &AccountServer{IHttp: components.NewHttpServer(version, port...)}
}
func (s *AccountServer) Init() error {
//mgo init
err := mongoproxy.ConnectMongo(common.GlobalConf.AccountConf.MongoConf)
//Etcd 初始化
s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
if err != nil {
return err
}
models.InitGameModels()
//Etcd 初始化
s.EtcdClient, err = etcd.NewEtcdClient(common.GlobalConf.Etcd)
if err != nil {
return err
}
s.EtcdClient.PutWithLeasePrefix(common.GlobalConf.AccountConf.Name, common.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", common.GlobalConf.AccountConf.IP, common.GlobalConf.AccountConf.Port), 5)
return nil
}
func (s *AccountServer) Start() error {
if err := s.Init(); err != nil {
return err
}
return s.IHttp.Start()
}
func main() {
err := make(chan error)
stopChan := make(chan os.Signal)
signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
web := NewAccountServer("v1", fmt.Sprintf(":%d", common.GlobalConf.AccountConf.Port))
web.BindHandler(&AccountAction{HttpServer: web})
go func() {
err <- web.Start()
}()
select {
case e := <-err:
logger.Error("http server error: %v", e)
case <-stopChan:
logger.Debug("http stop")
web.Stop()
}
}