Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
103 lines (92 loc) · 2.49 KB
/
Copy pathmain.go
File metadata and controls
103 lines (92 loc) · 2.49 KB
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
// package main is the entry point for the memcache proxy golemproxy
package main
/**
* Copyright 2018 Tyson Andre
*/
import (
"log"
"github.com/TysonAndre/golemproxy/config"
"github.com/TysonAndre/golemproxy/memcache/proxy"
"github.com/sevlyar/go-daemon"
"flag"
"fmt"
"os"
)
var (
configFileFlag = flag.String("c", "", "Config file path")
statsPortFlag = flag.Uint("s", 22222, "Stats port (set to 0 to disable)")
verboseLevelFlag = flag.Int("v", 5, "Logging level (default: 5, min: 0, max: 11)")
daemonizeFlag = flag.Bool("d", false, "Whether to daemonize")
outputPathFlag = flag.String("o", "", "set logging file (default: stderr)")
pidFilePath = flag.String("p", "", "set pid file (default: off)")
mbufSizeFlag = flag.Int("m", 0, "mbuf chunk size for twemproxy compat (IGNORED)")
statsIntervalFlag = flag.Int("i", 30000, "stats interval in msec for twemproxy compat (IGNORED)")
)
var flagAlias = map[string]string{
"conf-file": "c",
"stats-port": "s",
"daemonize": "d",
"output": "o",
"pid-file": "p",
"verbose": "v",
"mbuf-size": "m",
"stats-interval": "s",
}
func daemonize() bool {
outputFile := *outputPathFlag
pidFile := *pidFilePath
if outputFile == "" {
outputFile = "golemproxy.log"
}
// TODO: Configure the log file
context := &daemon.Context{
LogFileName: outputFile,
LogFilePerm: 0644,
WorkDir: "./",
Umask: 027,
}
if pidFile != "" {
context.PidFileName = pidFile
context.PidFilePerm = 0640
}
d, err := context.Reborn()
if err != nil {
log.Fatal("Unable to run daemon: ", err)
os.Exit(1)
}
return d != nil
}
func parseAllFlags() {
for long, short := range flagAlias {
original := flag.Lookup(short)
flag.Var(original.Value, long, fmt.Sprintf("%s\n(Alias of -%s)", original.Usage, short))
}
flag.Parse()
unexpectedArgs := flag.Args()
if len(unexpectedArgs) > 0 {
fmt.Fprintf(os.Stderr, "Unexpected args: %q\n", unexpectedArgs)
flag.Usage()
os.Exit(1)
}
}
func main() {
parseAllFlags()
configFile := *configFileFlag
if configFile == "" {
fmt.Fprintf(os.Stderr, "-config path/to/config.yml is required\n")
flag.Usage()
os.Exit(1)
}
configs, err := config.ParseFile(configFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Invalid config file %q: %v\n", configFile, err)
}
if *daemonizeFlag {
fmt.Fprintf(os.Stderr, "Going to daemonize\n")
if daemonize() {
os.Exit(0)
}
}
fmt.Fprintf(os.Stderr, "Starting: %#v\n\n", configs)
proxy.Run(configs, *statsPortFlag)
}