-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbancho.go
56 lines (45 loc) · 952 Bytes
/
bancho.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
package bancho
import (
"log"
"net/http"
"time"
"github.com/nsogame/common"
)
type BanchoServer struct {
config *Config
db *common.DB
rds *common.RedisAPI
router http.Handler
}
func NewInstance(config *Config) (bancho *BanchoServer, err error) {
// db
db, err := common.ConnectDB(config.DbProvider, config.DbConnection)
if err != nil {
return
}
// redis
rds := common.NewRedis(config.RedisAddr, config.RedisPass, config.RedisDB)
bancho = &BanchoServer{
config: config,
db: db,
rds: rds,
}
// router
router := bancho.Handlers()
bancho.router = router
return
}
func (bancho *BanchoServer) close() {
bancho.db.Close()
}
func (bancho *BanchoServer) Run() {
defer bancho.close()
log.Println("starting...")
server := &http.Server{
Handler: bancho.router,
Addr: bancho.config.BindAddr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(server.ListenAndServe())
}