-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
116 lines (97 loc) · 2.69 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
113
114
115
116
package main
import (
"flag"
"fmt"
"io"
"net/http"
_ "net/http/pprof"
"os"
"runtime"
"strings"
"time"
"github.com/sasha-s/go-deadlock"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/valinurovam/garagemq/admin"
"github.com/valinurovam/garagemq/config"
"github.com/valinurovam/garagemq/metrics"
"github.com/valinurovam/garagemq/server"
)
func init() {
viper.SetEnvPrefix("gmq")
viper.AutomaticEnv()
replacer := strings.NewReplacer("-", "_")
viper.SetEnvKeyReplacer(replacer)
flag.Bool("help", false, "Shows the help message")
flag.String("config", "", "The configuration file to use for the GarageMQ.")
var levels []string
for _, l := range logrus.AllLevels {
levels = append(levels, l.String())
}
flag.String("log-file", "stdout", "Log file")
flag.String("log-level", "info", fmt.Sprintf("Log level (%s)", strings.Join(levels, ", ")))
flag.Bool("hprof", false, "Starts server with hprof profiler.")
flag.String("hprof-host", "0.0.0.0", "hprof profiler host.")
flag.String("hprof-port", "8080", "hprof profiler port.")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
}
func main() {
// server has an issue with locks deadlock
// solving is in progress
deadlock.Opts.Disable = true
if viper.GetBool("help") {
flag.Usage()
os.Exit(0)
}
initLogger(viper.GetString("log-level"), viper.GetString("log-file"))
var cfg *config.Config
var err error
if viper.GetString("config") != "" {
if cfg, err = config.CreateFromFile(viper.GetString("config")); err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
cfg, _ = config.CreateDefault()
}
if viper.GetBool("hprof") {
// for hprof debugging
go http.ListenAndServe(fmt.Sprintf("%s:%s", viper.GetString("hprof-host"), viper.GetString("hprof-port")), nil)
}
runtime.GOMAXPROCS(runtime.NumCPU())
metrics.NewTrackRegistry(15, time.Second, false)
srv := server.NewServer(cfg.TCP.IP, cfg.TCP.Port, cfg.Proto, cfg)
adminServer := admin.NewAdminServer(srv, cfg.Admin.IP, cfg.Admin.Port)
// Start admin server
go func() {
if err := adminServer.Start(); err != nil {
panic("Failed to start adminServer - " + err.Error())
}
}()
// Start GarageMQ broker
srv.Start()
}
func initLogger(lvl string, path string) {
level, err := logrus.ParseLevel(lvl)
if err != nil {
panic(err)
}
var output io.Writer
switch path {
case "stdout":
output = os.Stdout
case "stderr":
output = os.Stderr
default:
output, err = os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
panic(err)
}
}
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true})
logrus.SetOutput(output)
logrus.SetLevel(level)
}