-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (72 loc) · 2.16 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 provides a way to proxy services.
package main
import (
"crypto/tls"
"log/slog"
"net/http"
"os"
"time"
"golang.org/x/crypto/acme/autocert"
"github.com/plamorg/voltproxy/config"
"github.com/plamorg/voltproxy/dockerapi"
"github.com/plamorg/voltproxy/services"
)
func listen(handler http.Handler, timeout time.Duration) {
server := &http.Server{
Addr: ":http",
Handler: handler,
ReadTimeout: timeout,
}
slog.Error("Error from HTTP server",
slog.Any("error", server.ListenAndServe()),
)
os.Exit(1)
}
func listenTLS(handler http.Handler, timeout time.Duration, tlsConfig *tls.Config) {
tlsServer := &http.Server{
Addr: ":https",
TLSConfig: tlsConfig,
Handler: handler,
ReadTimeout: timeout,
}
slog.Error("Error from HTTPS server", slog.Any("error", tlsServer.ListenAndServeTLS("", "")))
os.Exit(1)
}
func logPanic(msg string, err error) {
slog.Error(msg, slog.Any("error", err))
os.Exit(1)
}
func main() {
confContent, err := os.ReadFile("./config.yml")
if err != nil {
logPanic("Error while reading configuration file", err)
}
conf, err := config.New(confContent)
if err != nil {
logPanic("Error while parsing configuration file", err)
}
if err = conf.LogConfig.Initialize(); err != nil {
logPanic("Error while initializing logging", err)
}
slog.Info("Logging enabled", slog.Any("logger", conf.LogConfig))
docker, err := dockerapi.NewClient()
if err != nil {
logPanic("Error while connecting to Docker", err)
}
slog.Info("Connected to Docker", slog.Any("docker", docker))
serviceMap, err := conf.Services(docker)
if err != nil {
logPanic("Error while fetching services", err)
}
services.LaunchHealthChecks(serviceMap)
tlsHosts := conf.TLSHosts()
slog.Info("Managing certificates", slog.Any("hosts", tlsHosts))
certManager := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(tlsHosts...),
Cache: autocert.DirCache("_certs"),
}
slog.Info("Accepting connections on :80 and :443")
go listen(certManager.HTTPHandler(services.Handler(serviceMap)), conf.ReadTimeout)
listenTLS(services.TLSHandler(serviceMap), conf.ReadTimeout, certManager.TLSConfig())
}