-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (96 loc) · 2.12 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"github.com/Songmu/prompter"
"github.com/coreos/go-log/log"
"github.com/go-ini/ini"
_ "github.com/lib/pq"
"github.com/pkg/errors"
"github.com/setnicka/shrecker/game"
"github.com/setnicka/shrecker/server"
"github.com/urfave/cli"
)
func main() {
app := cli.NewApp()
app.Name = "Shrecker"
app.Version = "0.1.0"
app.Usage = "Tracker for puzzle hunt games of multiple teams"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
Value: "config.ini",
},
}
app.Commands = []cli.Command{
{
Name: "init-db",
Usage: "Initialize the DB.",
Action: commandInitDB,
},
{
Name: "run",
Usage: "Run the webserver",
Flags: []cli.Flag{
cli.IntFlag{
Name: "port,p",
Usage: "Listen on port `PORT`",
Value: 8000,
},
},
Action: commandRunServer,
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Printf("Error while executing command: %v\n", err)
os.Exit(1)
}
log.Info("Starting")
}
func commandRunServer(c *cli.Context) error {
// 1. Get Config
configfile := c.GlobalString("config")
config, err := ini.Load(configfile)
if err != nil {
return errors.Wrapf(err, "Cannot open config file '%s'", configfile)
}
// 2. Open connection to the DB
db, err := dbConnect(config)
if err != nil {
return err
}
// 3. Init game
g, err := game.New(config, db)
if err != nil {
return err
}
// 4. Start the server
server, err := server.New(config, g)
if err != nil {
return err
}
return server.Start()
// TODO wait for signal to end or reload
}
func commandInitDB(c *cli.Context) error {
// 1. Get Config
configfile := c.GlobalString("config")
config, err := ini.Load(configfile)
if err != nil {
return errors.Wrapf(err, "Cannot open config file '%s'", configfile)
}
// 2. Open connection to the DB
db, err := dbConnect(config)
if err != nil {
return err
}
// 3. Confirm
fmt.Println("WARNING: Init of the DB will erase all previous records!")
if !prompter.YesNo("Really init the DB?", false) {
return nil
}
// 4. Initialization of the DB
return dbInit(db, config)
}