-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
162 lines (132 loc) · 3.62 KB
/
Copy pathmain.go
File metadata and controls
162 lines (132 loc) · 3.62 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"crypto/tls"
"flag"
"fmt"
"log/slog"
"net/http"
"os"
"time"
"github.com/daknob/eldim/config"
"github.com/prometheus/client_golang/prometheus/promhttp"
yaml "gopkg.in/yaml.v3"
)
var (
conf config.Config
clients []config.ClientConfig
)
const (
version = "v0.7.1"
)
func main() {
/* Output logs in JSON or Text */
logFormat := flag.Bool("j", false, "Output logs in JSON")
/* Configuration File Path */
configPath := flag.String("c", "/etc/eldim/eldim.yml", "Path to the configuration file")
/* Parse flags */
flag.Parse()
/* Set up structured logging */
var handler slog.Handler
if *logFormat {
handler = slog.NewJSONHandler(os.Stderr, nil)
} else {
handler = slog.NewTextHandler(os.Stderr, nil)
}
slog.SetDefault(slog.New(handler))
/* Startup logs */
slog.Info("starting eldim", "version", version)
slog.Info("configuration",
"json_logs", *logFormat,
"config_file", *configPath,
)
/* Parse the configuration file */
slog.Info("parsing configuration file")
/* Open the configuration file, and read contents to RAM */
confb, err := os.ReadFile(*configPath)
if err != nil {
slog.Error("could not open configuration file", "path", *configPath, "error", err)
os.Exit(1)
}
/* Attempt to parse it for YAML */
err = yaml.Unmarshal(confb, &conf)
if err != nil {
slog.Error("could not parse YAML configuration file", "path", *configPath, "error", err)
os.Exit(1)
}
slog.Info("configuration file loaded")
/* Validate configuration by appropriate function call */
slog.Info("validating parameters")
err = conf.Validate()
if err != nil {
slog.Error("invalid configuration", "error", err)
os.Exit(1)
}
slog.Info("configuration file validated")
/* Load client file */
clib, err := os.ReadFile(conf.ClientFile)
if err != nil {
slog.Error("could not open clients file", "path", conf.ClientFile, "error", err)
os.Exit(1)
}
err = yaml.Unmarshal(clib, &clients)
if err != nil {
slog.Error("could not parse clients YAML file", "path", conf.ClientFile, "error", err)
os.Exit(1)
}
slog.Info("clients file loaded", "clients", len(clients))
/* Register Prometheus Metrics */
registerPromMetrics()
/* Update configuration-based Metrics */
updateConfMetrics()
/* Various web server configurations */
slog.Info("configuring HTTP server")
/* Create an HTTP Router */
mux := http.NewServeMux()
mux.HandleFunc("GET /{$}", index)
mux.HandleFunc("POST /api/v1/file/upload/", v1fileUpload)
/* Only enable Prometheus metrics if configured */
if conf.PrometheusEnabled {
mux.HandleFunc(
"GET /metrics",
requestBasicAuth(
conf.PrometheusAuthUser,
conf.PrometheusAuthPass,
"Prometheus Metrics",
*promMetricsAuth,
promhttp.Handler().ServeHTTP,
),
)
}
/* Configure TLS */
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
}
/* Configure HTTP */
server := http.Server{
ReadTimeout: time.Duration(conf.ReadTimeout) * time.Second,
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 120 * time.Second,
IdleTimeout: 180 * time.Second,
TLSConfig: tlsConfig,
Handler: mux,
Addr: fmt.Sprintf(":%d", conf.ListenPort),
}
slog.Info("HTTP server configured",
"port", conf.ListenPort,
"read_timeout_s", conf.ReadTimeout,
"tls_chain", conf.TLSChainPath,
"prometheus", conf.PrometheusEnabled,
)
/* Start serving TLS */
slog.Info("starting TLS listener", "port", conf.ListenPort)
err = server.ListenAndServeTLS(
conf.TLSChainPath,
conf.TLSKeyPath,
)
if err != nil {
slog.Error("failed to start HTTP server", "error", err)
os.Exit(1)
}
/* Exit */
slog.Info("eldim quitting")
}