-
Notifications
You must be signed in to change notification settings - Fork 211
/
main.go
94 lines (76 loc) · 1.78 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
/**
* main.go - entry point
* @author Yaroslav Pogrebnyak <yyyaroslav@gmail.com>
*/
package main
import (
"log"
"math/rand"
"os"
"runtime"
"time"
"github.com/yyyar/gobetween/api"
"github.com/yyyar/gobetween/cmd"
"github.com/yyyar/gobetween/config"
"github.com/yyyar/gobetween/info"
"github.com/yyyar/gobetween/logging"
"github.com/yyyar/gobetween/manager"
"github.com/yyyar/gobetween/metrics"
"github.com/yyyar/gobetween/utils/codec"
)
/**
* version,revision,branch should be set while build using ldflags (see Makefile)
*/
var (
version string
revision string
branch string
)
/**
* Initialize package
*/
func init() {
// Set GOMAXPROCS if not set
if os.Getenv("GOMAXPROCS") == "" {
runtime.GOMAXPROCS(runtime.NumCPU())
}
// Init random seed
rand.Seed(time.Now().UnixNano())
// Save info
info.Version = version
info.Revision = revision
info.Branch = branch
info.StartTime = time.Now()
}
/**
* Entry point
*/
func main() {
log.Printf("gobetween v%s", version)
env := os.Getenv("GOBETWEEN")
if env != "" && len(os.Args) > 1 {
log.Fatal("Passed GOBETWEEN env var and command-line arguments: only one allowed")
}
// Try parse env var to args
if env != "" {
a := []string{}
if err := codec.Decode(env, &a, "json"); err != nil {
log.Fatal("Error converting env var to parameters: ", err, " ", env)
}
os.Args = append([]string{""}, a...)
log.Println("Using parameters from env var: ", os.Args)
}
// Process flags and start
cmd.Execute(func(cfg *config.Config) {
// Configure logging
logging.Configure(cfg.Logging.Output, cfg.Logging.Level, cfg.Logging.Format)
// Start manager
manager.Initialize(*cfg)
/* setup metrics */
metrics.Start((*cfg).Metrics)
// Start API
api.Start((*cfg).Api)
// block forever
<-(chan string)(nil)
})
}