forked from lonng/nanoserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (61 loc) · 1.46 KB
/
main.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 (
"fmt"
"os"
"runtime/pprof"
"sync"
"time"
"github.com/lonng/nanoserver/internal/game"
"github.com/lonng/nanoserver/internal/web"
_ "github.com/go-sql-driver/mysql"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
// base application info
app.Name = "mahjong server"
app.Author = "MaJong"
app.Version = "0.0.1"
app.Copyright = "majong team reserved"
app.Usage = "majiang server"
// flags
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: "./configs/config.toml",
Usage: "load configuration from `FILE`",
},
cli.BoolFlag{
Name: "cpuprofile",
Usage: "enable cpu profile",
},
}
app.Action = serve
app.Run(os.Args)
}
func serve(c *cli.Context) error {
viper.SetConfigType("toml")
viper.SetConfigFile(c.String("config"))
viper.ReadInConfig()
log.SetFormatter(&log.TextFormatter{DisableColors: true})
if viper.GetBool("core.debug") {
log.SetLevel(log.DebugLevel)
}
if c.Bool("cpuprofile") {
filename := fmt.Sprintf("cpuprofile-%d.pprof", time.Now().Unix())
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, os.ModePerm)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
wg := sync.WaitGroup{}
wg.Add(2)
go func() { defer wg.Done(); game.Startup() }() // 开启游戏服
go func() { defer wg.Done(); web.Startup() }() // 开启web服务器
wg.Wait()
return nil
}