http.go
1.3 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
package main
import (
"fmt"
"os"
"os/signal"
"pro2d/common/components"
"pro2d/conf"
_ "pro2d/conf"
"pro2d/models"
"pro2d/utils/db"
"pro2d/utils/etcd"
"pro2d/utils/logger"
"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) Start() error {
//mongo 初始化
db.MongoDatabase = db.MongoClient.Database(conf.GlobalConf.AccountConf.DBName)
models.InitAccountServerModels()
//Etcd 初始化
var err error
s.EtcdClient, err = etcd.NewEtcdClient(conf.GlobalConf.Etcd)
if err != nil {
return err
}
s.EtcdClient.PutWithLeasePrefix(conf.GlobalConf.AccountConf.Name, conf.GlobalConf.AccountConf.ID, fmt.Sprintf("%s:%d", conf.GlobalConf.AccountConf.IP, conf.GlobalConf.AccountConf.Port), 5)
return nil
}
func main() {
err := make(chan error)
stopChan := make(chan os.Signal)
signal.Notify(stopChan, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL)
web := NewAccountServer("v1")
web.BindHandler(&AccountAction{HttpServer: web})
go func() {
err <- web.Start()
}()
select {
case e := <- err:
logger.Error("game server error: %v", e)
case <-stopChan:
logger.Debug("game stop")
web.Stop()
}
}