Skip to content

Commit

Permalink
Add unauthenticated health check service
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardobl committed Jan 1, 2025
1 parent 02cddde commit 57499f7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Configuration options can be specified via environment variables (all are option
| `LNCD_TLS_KEY_PATH` | `""` | Path to the TLS key file (empty to disable TLS). |
| `LNCD_AUTH_TOKEN` | `""` | Bearer token required to access the server (empty to disable authentication). |
| `LNCD_DEV_UNSAFE_LOG` | `false` | Enable or disable logging of sensitive data. |
| `LNCD_HEALTHCHECK_SERVICE_PORT` | `7168` | Additional healthcheck service port. If LNCD_HEALTHCHECK_SERVICE_PORT and LNCD_HEALTHCHECK_SERVICE_HOST are set, an additional unauthenticated and unencrypted healthcheck service will be started on the specified port and host. |
| `LNCD_HEALTHCHECK_SERVICE_HOST` | `127.0.0.1` | Additional healthcheck service host. If LNCD_HEALTHCHECK_SERVICE_PORT and LNCD_HEALTHCHECK_SERVICE_HOST are set, an additional unauthenticated and unencrypted healthcheck service will be started on the specified port and host. |


## Intended scope
Expand Down Expand Up @@ -92,3 +94,4 @@ RESPONSE
- POST http://localhost:7167/rpc : Send a request and get a response from the LNC server.
- GET http://localhost:7167/ : Web UI to test the /rpc endpoint.
- GET http://localhost:7167/health : Health check endpoint.
- GET http://localhost:7168/health : Unauthenticated health check endpoint (if enabled).
66 changes: 43 additions & 23 deletions lncd/lncd.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,17 @@ func getEnvAsBool(key string, defaultValue bool) bool {


var (
LNCD_TIMEOUT = getEnvAsDuration("LNCD_TIMEOUT", 5*time.Minute)
LNCD_LIMIT_ACTIVE_CONNECTIONS = getEnvAsInt("LNCD_LIMIT_ACTIVE_CONNECTIONS", 210)
LNCD_STATS_INTERVAL = getEnvAsDuration("LNCD_STATS_INTERVAL", 1*time.Minute)
LNCD_DEBUG = getEnvAsBool("LNCD_DEBUG", false)
LNCD_PORT = getEnv("LNCD_PORT", "7167")
LNCD_HOST = getEnv("LNCD_HOST", "0.0.0.0")
LNCD_AUTH_TOKEN = getEnv("LNCD_AUTH_TOKEN", "")
LNCD_TLS_CERT_PATH = getEnv("LNCD_TLS_CERT_PATH", "")
LNCD_TLS_KEY_PATH = getEnv("LNCD_TLS_KEY_PATH", "")
LNCD_TIMEOUT = getEnvAsDuration("LNCD_TIMEOUT", 5*time.Minute)
LNCD_LIMIT_ACTIVE_CONNECTIONS = getEnvAsInt("LNCD_LIMIT_ACTIVE_CONNECTIONS", 210)
LNCD_STATS_INTERVAL = getEnvAsDuration("LNCD_STATS_INTERVAL", 1*time.Minute)
LNCD_DEBUG = getEnvAsBool("LNCD_DEBUG", false)
LNCD_PORT = getEnv("LNCD_PORT", "7167")
LNCD_HOST = getEnv("LNCD_HOST", "0.0.0.0")
LNCD_AUTH_TOKEN = getEnv("LNCD_AUTH_TOKEN", "")
LNCD_TLS_CERT_PATH = getEnv("LNCD_TLS_CERT_PATH", "")
LNCD_TLS_KEY_PATH = getEnv("LNCD_TLS_KEY_PATH", "")
LNCD_HEALTHCHECK_SERVICE_PORT = getEnv("LNCD_HEALTHCHECK_SERVICE_PORT", "7168")
LNCD_HEALTHCHECK_SERVICE_HOST = getEnv("LNCD_HEALTHCHECK_SERVICE_HOST", "127.0.0.1")
)

// //////////////////////////////
Expand Down Expand Up @@ -474,7 +476,9 @@ func main() {
log.Infof("LNCD_HOST: %v", LNCD_HOST)
log.Infof("LNCD_TLS_CERT_PATH: %v", LNCD_TLS_CERT_PATH)
log.Infof("LNCD_TLS_KEY_PATH: %v", LNCD_TLS_KEY_PATH)

log.Infof("LNCD_HEALTHCHECK_SERVICE_PORT: %v", LNCD_HEALTHCHECK_SERVICE_PORT)
log.Infof("LNCD_HEALTHCHECK_SERVICE_HOST: %v", LNCD_HEALTHCHECK_SERVICE_HOST)

if UNSAFE_LOGS {
log.Infof("LNCD_AUTH_TOKEN: %v", LNCD_AUTH_TOKEN)
log.Infof("!!! UNSAFE LOGGING ENABLED !!!")
Expand All @@ -488,20 +492,36 @@ func main() {
http.HandleFunc("/health", authMiddleware(healthCheckHandler))
http.HandleFunc("/", formHandler)

log.Infof("Server starting at "+LNCD_HOST+":" + LNCD_PORT)
var isTLS = LNCD_TLS_CERT_PATH != "" && LNCD_TLS_KEY_PATH != ""
if isTLS {
log.Infof("TLS enabled")
if err := http.ListenAndServeTLS(LNCD_HOST+":"+LNCD_PORT, LNCD_TLS_CERT_PATH, LNCD_TLS_KEY_PATH, nil); err != nil {
log.Errorf("Error starting server: %v", err)
exit(err)
}
} else {
if err := http.ListenAndServe(LNCD_HOST+":"+LNCD_PORT, nil); err != nil {
log.Errorf("Error starting server: %v", err)
exit(err)
go func() {
log.Infof("Server starting at "+LNCD_HOST+":" + LNCD_PORT)
var isTLS = LNCD_TLS_CERT_PATH != "" && LNCD_TLS_KEY_PATH != ""
if isTLS {
log.Infof("TLS enabled")
if err := http.ListenAndServeTLS(LNCD_HOST+":"+LNCD_PORT, LNCD_TLS_CERT_PATH, LNCD_TLS_KEY_PATH, nil); err != nil {
log.Errorf("Error starting server: %v", err)
exit(err)
}
} else {
if err := http.ListenAndServe(LNCD_HOST+":"+LNCD_PORT, nil); err != nil {
log.Errorf("Error starting server: %v", err)
exit(err)
}
}
}
}()


if LNCD_HEALTHCHECK_SERVICE_HOST != "" && LNCD_HEALTHCHECK_SERVICE_PORT != "" {
go func() {
log.Infof("HealthCheck service starting at "+LNCD_HEALTHCHECK_SERVICE_HOST+":" + LNCD_HEALTHCHECK_SERVICE_PORT)
var rawHealthMux *http.ServeMux = http.NewServeMux()
rawHealthMux.HandleFunc("/health", healthCheckHandler)
if err := http.ListenAndServe(LNCD_HEALTHCHECK_SERVICE_HOST + ":" + LNCD_HEALTHCHECK_SERVICE_PORT, rawHealthMux); err != nil {
log.Errorf("Error starting HealthCheck server: %v", err)
exit(err)
}
}()
}


<-shutdownInterceptor.ShutdownChannel()
log.Infof("Shutting down daemon")
Expand Down

0 comments on commit 57499f7

Please sign in to comment.