-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
69 lines (60 loc) · 1.6 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
package main
import (
"context"
"log"
"net"
"net/http"
"os"
"os/signal"
"github.com/kofoworola/tunnelify/config"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
app = kingpin.New("proxify", "A lightweight and easily deployable proxy server written in go")
start = app.Command("start", "start the proxify server.")
configFile = start.Arg("config", "config file to start proxify server with.").String()
)
func main() {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-ch
cancel()
}()
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
case start.FullCommand():
config, err := config.LoadConfig(*configFile)
if err != nil {
log.Fatalf("error loading configuration: %v", err)
}
log.Printf("server listening on :%s", config.Port)
gateway, err := NewGateway(config)
if err != nil {
log.Fatalf("error creating server: %v", err)
}
go func() {
if err := gateway.Start(); err != nil {
log.Fatalf("error starting server: %v", err)
}
}()
go func() {
if err := StartServer(config, gateway); err != nil {
log.Fatalf("error running liveness server: %v", err)
}
}()
<-ctx.Done()
log.Println("shutting down server...")
gateway.Close()
}
}
func StartServer(cfg *config.Config, listener net.Listener) error {
if cfg.LivenessStatus == 0 {
return nil
}
http.HandleFunc(cfg.LivenessPath, func(writer http.ResponseWriter, req *http.Request) {
writer.WriteHeader(cfg.LivenessStatus)
writer.Write([]byte(cfg.LivenessBody))
})
return http.Serve(listener, http.DefaultServeMux)
}