-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
86 lines (70 loc) · 1.56 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
package main
import (
"flag"
"fmt"
"os"
"strconv"
"github.com/IMQS/gowinsvc/service"
"github.com/IMQS/router/server"
)
func main() {
os.Exit(realMain())
}
func realMain() (result int) {
result = 0
defer func() {
if err := recover(); err != nil {
result = 1
fmt.Printf("%v\n", err)
}
}()
flags := flag.NewFlagSet("router", flag.ExitOnError)
configFile := flags.String("config", "", "Optional config file for testing")
showHttpPort := flags.Bool("show-http-port", false, "print the http port to stdout and exit")
if len(os.Args) > 1 {
flags.Parse(os.Args[1:])
}
config := &server.Config{}
err := config.LoadFile(*configFile)
if err != nil {
panic(fmt.Errorf("Error loading '%s': %v", *configFile, err))
}
if os.Getenv("DISABLE_HTTPS_REDIRECT") == "1" {
config.HTTP.RedirectHTTP = false
}
envHttpPort := os.Getenv("HTTP_PORT")
if envHttpPort != "" {
port, err := strconv.ParseInt(envHttpPort, 10, 64)
if err != nil {
panic(fmt.Errorf("Invalid HTTP_PORT environment variable '%v'", envHttpPort))
}
config.HTTP.Port = uint16(port)
}
if *showHttpPort {
fmt.Printf("%v", config.HTTP.GetPort())
result = 0
return
}
server, err := server.NewServer(config)
if err != nil {
panic(fmt.Errorf("Error starting server: %v", err))
}
handler := func() error {
return server.ListenAndServe()
}
handlerNoRet := func() {
handler()
}
success := true
if !service.RunAsService(handlerNoRet) {
// Run in the foreground
success = false
fmt.Print(handler())
}
if success {
result = 0
} else {
result = 1
}
return
}