-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
117 lines (97 loc) · 2.52 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
117
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"codeberg.org/UnifiedPush/common-proxies/config"
. "codeberg.org/UnifiedPush/common-proxies/config"
)
var configFile = flag.String("c", "config.toml", "path to toml file for config")
// various translaters
var handlers = []Handler{}
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}
func main() {
flag.Parse()
err := ParseConf(*configFile)
if err != nil {
log.Fatalln(err)
}
log.Println("Starting", Config.GetUserAgent())
handlers = []Handler{
&Config.Rewrite.FCM,
&Config.Gateway.Matrix,
&Config.Gateway.Generic,
}
myRouter := http.NewServeMux()
for _, i := range handlers {
if i.Path() != "" {
myRouter.HandleFunc(i.Path(), handle(i))
if config.Config.Verbose {
fmt.Println("Handling", i.Path())
}
}
}
myRouter.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(config.Config.GetUserAgent() + " OK"))
})
myRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Endpoint doesn't exist\n"))
})
server := &http.Server{
Addr: Config.ListenAddr,
Handler: myRouter,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGHUP)
go func() {
for {
switch <-quit {
case syscall.SIGHUP:
err := config.ParseConf(*configFile)
if err != nil {
log.Println("Unable to reload config: ", err)
}
case os.Interrupt:
log.Println("Server is shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
return
default:
log.Println("UNKNOWN SIGNAL")
}
}
}()
log.Println("Server is ready to handle requests at", Config.ListenAddr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v\n", Config.ListenAddr, err)
}
<-done
log.Println("Server stopped")
}
func handle(handler Handler) HttpHandler {
if h, ok := handler.(Gateway); ok {
return bothHandler(gatewayHandler(h))
} else if h, ok := handler.(Proxy); ok {
return bothHandler(proxyHandler(h))
} else {
//should be const so np abt fatal
log.Fatalf("UNABLE TO HANDLE HANDLER %#v\n", handler)
return nil
}
}