Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
82 lines (71 loc) · 2.34 KB
/
Copy pathmain.go
File metadata and controls
82 lines (71 loc) · 2.34 KB
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
package main
import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
"github.com/gabrielopesantos/reverse-proxy/pkg/config"
"github.com/gabrielopesantos/reverse-proxy/pkg/server"
)
func main() {
bootstrapCfg, err := config.LoadBootstrap(os.Args[1:], os.Environ())
if err != nil {
slog.Error("could not load bootstrap configuration", "err", err)
os.Exit(1)
}
logger, cleanupLogger, err := config.NewBootstrapLogger(bootstrapCfg)
if err != nil {
slog.Error("could not initialize logger from bootstrap configuration", "err", err)
os.Exit(1)
}
defer cleanupLogger()
accessLogWrap, cleanupAccessLog, err := server.NewAccessLog(bootstrapCfg.AccessLogOutput, bootstrapCfg.AccessLogFormat)
if err != nil {
logger.Error("could not initialize access log", "err", err)
os.Exit(1)
}
defer cleanupAccessLog()
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
runtimeConfig, err := config.LoadConfig(ctx, bootstrapCfg, logger)
if err != nil {
logger.Error("could not parse runtime configuration file", "path", bootstrapCfg.ConfigPath, "err", err)
os.Exit(1)
}
go func() {
if err := runtimeConfig.Watch(ctx, logger); err != nil {
logger.Error("config watcher exited", "err", err)
os.Exit(1)
}
}()
srvOpts := []server.Option{
server.WithLogger(logger),
server.WithAddress(bootstrapCfg.ListenAddr),
server.WithAdminAddress(bootstrapCfg.AdminAddr),
server.WithReadTimeout(bootstrapCfg.ReadTimeout),
server.WithAccessLog(accessLogWrap),
}
if bootstrapCfg.TLSCertFile != "" {
srvOpts = append(srvOpts, server.WithTLSFiles(bootstrapCfg.TLSCertFile, bootstrapCfg.TLSKeyFile))
}
srv := server.New(runtimeConfig, srvOpts...)
logger.Info(
"starting reverse-proxy",
"config_path", bootstrapCfg.ConfigPath,
"reload_interval", bootstrapCfg.ReloadInterval.String(),
"listen_addr", bootstrapCfg.ListenAddr,
"admin_addr", bootstrapCfg.AdminAddr,
"tls", bootstrapCfg.TLSCertFile != "",
"log_level", bootstrapCfg.LogLevel,
"log_format", bootstrapCfg.LogFormat,
"log_output", bootstrapCfg.LogOutput,
"access_log_output", bootstrapCfg.AccessLogOutput,
"access_log_format", bootstrapCfg.AccessLogFormat,
"read_timeout", bootstrapCfg.ReadTimeout.String(),
)
if err := srv.ListenAndServe(ctx); err != nil {
logger.Error("failed to start server", "err", err)
os.Exit(1)
}
}