Skip to content

Commit

Permalink
feat: add SSL/TLS support for secure connections
Browse files Browse the repository at this point in the history
- Add environment variables for SSL configuration
- Implement conditional SSL server startup based on configuration
  • Loading branch information
chanyongkit committed Nov 19, 2024
1 parent 023b3c1 commit 3f7679a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func main() {

port, host := config.Port, config.Host
logstashUrl := config.LogstashUrl
enableSSL := config.EnableSSL == "TRUE"
sslCertDir := config.SSLCertDir
sslKeyDir := config.SSLKeyDir

slog.Debug("application starting... ")
versionInfo := config.GetVersionInfo()
Expand All @@ -56,8 +59,14 @@ func main() {
prometheus.MustRegister(collectorManager)

slog.Info("starting server on", "host", host, "port", port)
if err := appServer.ListenAndServe(); err != nil {
if enableSSL {
err = appServer.ListenAndServeTLS(sslCertDir, sslKeyDir)
} else {
err = appServer.ListenAndServe()
}

if err != nil {
slog.Error("failed to listen and serve", "err", err)
os.Exit(1)
os.Exit(1)
}
}
15 changes: 15 additions & 0 deletions config/server_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
package config

var (
// SSL determines if the exporter should use HTTPS instead of HTTP
// Defaults to "FALSE"
// Can be overridden by setting the SSL environment variable
EnableSSL = getEnvWithDefault("ENABLE_SSL", "FALSE")

// SSL_CERT_DIR specifies the directory path containing the SSL certificate file
// Must be set if SSL is "TRUE"
// Can be overridden by setting the SSL_CERT_DIR environment variable
SSLCertDir = getEnvWithDefault("SSL_CERT_DIR","")

// SSL_KEY_DIR specifies the directory path containing the SSL private key file
// Must be set if SSL is "TRUE"
// Can be overridden by setting the SSL_KEY_DIR environment variable
SSLKeyDir = getEnvWithDefault("SSL_KEY_DIR","")

// Port is the port the exporter will listen on.
// Defaults to 9198
// Can be overridden by setting the PORT environment variable
Expand Down

0 comments on commit 3f7679a

Please sign in to comment.