-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
153 lines (123 loc) · 3.36 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
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
package main
import (
"crypto/tls"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/daknob/eldim/config"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/julienschmidt/httprouter"
"github.com/sirupsen/logrus"
yaml "gopkg.in/yaml.v2"
)
var (
conf config.Config
clients []config.ClientConfig
)
const (
version = "v0.6.0"
)
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 the log format to JSON if requested */
if *logFormat == true {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{})
}
/* Startup logs */
logrus.Printf("starting eldim...")
logrus.Printf("log in JSON: %v", *logFormat)
logrus.Printf("configuration file: %s", *configPath)
/* Parse the configuration file */
logrus.Printf("parsing the configuration file...")
/* Open the configuration file, and read contents to RAM */
confb, err := ioutil.ReadFile(*configPath)
if err != nil {
logrus.Fatalf("could not open configuration file: %v", err)
}
/* Attempt to parse it for YAML */
err = yaml.Unmarshal(confb, &conf)
if err != nil {
logrus.Fatalf("could not parse the YAML configuration file: %v", err)
}
logrus.Printf("configuration file loaded.")
/* Validate configuration by appropriate function call */
logrus.Printf("validating parameters...")
err = conf.Validate()
if err != nil {
logrus.Fatalf("invalid configuration: %v", err)
}
logrus.Printf("configuration file validated.")
/* Load client file */
clib, err := ioutil.ReadFile(conf.ClientFile)
if err != nil {
logrus.Fatalf("could not open clients file: %v", err)
}
err = yaml.Unmarshal(clib, &clients)
if err != nil {
logrus.Fatalf("could not parse clients YML file: %v", err)
}
/* Register Prometheus Metrics */
registerPromMetrics()
/* Update configuration-based Metrics */
updateConfMetrics()
/* Various web server configurations */
logrus.Printf("configuring the HTTP Server...")
/* Create an HTTP Router */
router := httprouter.New()
router.GET("/", index)
router.POST("/api/v1/file/upload/", v1fileUpload)
/* Only enable Prometheus metrics if configured */
if conf.PrometheusEnabled {
router.GET(
"/metrics",
requestBasicAuth(
conf.PrometheusAuthUser,
conf.PrometheusAuthPass,
"Prometheus Metrics",
*promMetricsAuth,
httpHandlerToHTTPRouterHandler(
promhttp.Handler(),
),
),
)
}
/* Configure TLS */
tlsConfig := &tls.Config{
PreferServerCipherSuites: true,
CurvePreferences: []tls.CurveID{
tls.CurveP256,
tls.X25519,
},
MinVersion: tls.VersionTLS12,
}
/* Configure HTTP */
server := http.Server{
ReadHeaderTimeout: 5 * time.Second,
WriteTimeout: 120 * time.Second,
IdleTimeout: 180 * time.Second,
TLSConfig: tlsConfig,
Handler: router,
Addr: fmt.Sprintf(":%d", conf.ListenPort),
}
logrus.Printf("HTTP Server Configured.")
/* Start serving TLS */
logrus.Printf("serving on :%d ...", conf.ListenPort)
err = server.ListenAndServeTLS(
conf.TLSChainPath,
conf.TLSKeyPath,
)
if err != nil {
logrus.Fatalf("failed to start HTTP Server: %v", err)
}
/* Exit */
logrus.Printf("eldim quitting...")
}