/
main.go
80 lines (65 loc) · 1.41 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
package main
import (
"flag"
"fmt"
"net"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/bojand/ghz/web/api"
"github.com/bojand/ghz/web/config"
"github.com/bojand/ghz/web/database"
"github.com/bojand/ghz/web/router"
)
var (
// set by goreleaser with -ldflags="-X main.version=..."
version = "dev"
beta = true
date = "unknown"
cPath = flag.String("config", "", "Path to the config file.")
v = flag.Bool("v", false, "Print the version.")
)
var usage = `Usage: ghz-web [options...]
Options:
-config Path to the config JSON file.
-v Print the version.
`
func main() {
// fix version
if version != "dev" && beta {
version = version + "-beta"
}
flag.Usage = func() {
fmt.Fprint(os.Stderr, usage)
}
flag.Parse()
if *v {
fmt.Println(version)
os.Exit(0)
}
cfgPath := strings.TrimSpace(*cPath)
conf, err := config.Read(cfgPath)
if err != nil {
panic(err)
}
db, err := database.New(conf.Database.Type, conf.Database.Connection, conf.Log.Level == "debug")
if err != nil {
panic(err)
}
defer db.Close()
info := &api.ApplicationInfo{
Version: version,
BuildDate: date,
GOVersion: runtime.Version(),
StartTime: time.Now(),
}
server, err := router.New(db, info, conf)
if err != nil {
panic(err)
}
router.PrintRoutes(server)
hostPort := net.JoinHostPort("", strconv.FormatUint(uint64(conf.Server.Port), 10))
server.Logger.Fatal(server.Start(hostPort))
}