forked from realDragonium/Ultraviolet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
79 lines (71 loc) · 2.02 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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/cloudflare/tableflip"
"github.com/realDragonium/Ultraviolet/config"
"github.com/realDragonium/Ultraviolet/proxy"
)
var (
// Isnt this the proper path to put config files into (for execution without docker)
defaultCfgPath = "/etc/ultraviolet"
defaultServerCfgPath = filepath.Join(defaultCfgPath, "config")
defaultUltravioletCfgPath = filepath.Join(defaultCfgPath, "ultraviolet.json")
)
func main() {
log.Println("Starting up")
var (
pidFile = flag.String("pid-file", "/run/ultraviolet.pid", "`Path` to pid file")
mainCfgPath = flag.String("config", defaultUltravioletCfgPath, "`Path` to main config file")
serverCfgsPath = flag.String("server-configs", defaultServerCfgPath, "`Path` to server config files")
)
flag.Parse()
mainCfg, err := config.ReadUltravioletConfig(*mainCfgPath)
if err != nil {
log.Fatalf("Read main config file at '%s' - error: %v", *mainCfgPath, err)
}
serverCfgs, err := config.ReadServerConfigs(*serverCfgsPath)
if err != nil {
log.Fatalf("Something went wrong while reading config files: %v", err)
}
reqCh := make(chan proxy.McRequest)
gateway := proxy.NewGateway()
gateway.StartWorkers(mainCfg, serverCfgs, reqCh)
log.SetPrefix(fmt.Sprintf("%d ", os.Getpid()))
upg, err := tableflip.New(tableflip.Options{
PIDFile: *pidFile,
})
if err != nil {
panic(err)
}
defer upg.Stop()
go func() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP)
for range sig {
err := upg.Upgrade()
if err != nil {
log.Println("upgrade failed:", err)
}
}
}()
ln, err := upg.Listen("tcp", mainCfg.ListenTo)
if err != nil {
log.Fatalf("Can't listen: %v", err)
}
defer ln.Close()
go proxy.ServeListener(ln, reqCh)
log.Printf("Finished starting up")
if err := upg.Ready(); err != nil {
panic(err)
}
<-upg.Exit()
log.Println("Waiting for all open connections to close before shutting down")
gateway.Shutdown()
log.Println("Shutting down")
}