Skip to content

Commit 57499f7

Browse files
committed
Add unauthenticated health check service
1 parent 02cddde commit 57499f7

File tree

2 files changed

+46
-23
lines changed

2 files changed

+46
-23
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Configuration options can be specified via environment variables (all are option
2222
| `LNCD_TLS_KEY_PATH` | `""` | Path to the TLS key file (empty to disable TLS). |
2323
| `LNCD_AUTH_TOKEN` | `""` | Bearer token required to access the server (empty to disable authentication). |
2424
| `LNCD_DEV_UNSAFE_LOG` | `false` | Enable or disable logging of sensitive data. |
25+
| `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. |
26+
| `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. |
2527

2628

2729
## Intended scope
@@ -92,3 +94,4 @@ RESPONSE
9294
- POST http://localhost:7167/rpc : Send a request and get a response from the LNC server.
9395
- GET http://localhost:7167/ : Web UI to test the /rpc endpoint.
9496
- GET http://localhost:7167/health : Health check endpoint.
97+
- GET http://localhost:7168/health : Unauthenticated health check endpoint (if enabled).

lncd/lncd.go

+43-23
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ func getEnvAsBool(key string, defaultValue bool) bool {
5757

5858

5959
var (
60-
LNCD_TIMEOUT = getEnvAsDuration("LNCD_TIMEOUT", 5*time.Minute)
61-
LNCD_LIMIT_ACTIVE_CONNECTIONS = getEnvAsInt("LNCD_LIMIT_ACTIVE_CONNECTIONS", 210)
62-
LNCD_STATS_INTERVAL = getEnvAsDuration("LNCD_STATS_INTERVAL", 1*time.Minute)
63-
LNCD_DEBUG = getEnvAsBool("LNCD_DEBUG", false)
64-
LNCD_PORT = getEnv("LNCD_PORT", "7167")
65-
LNCD_HOST = getEnv("LNCD_HOST", "0.0.0.0")
66-
LNCD_AUTH_TOKEN = getEnv("LNCD_AUTH_TOKEN", "")
67-
LNCD_TLS_CERT_PATH = getEnv("LNCD_TLS_CERT_PATH", "")
68-
LNCD_TLS_KEY_PATH = getEnv("LNCD_TLS_KEY_PATH", "")
60+
LNCD_TIMEOUT = getEnvAsDuration("LNCD_TIMEOUT", 5*time.Minute)
61+
LNCD_LIMIT_ACTIVE_CONNECTIONS = getEnvAsInt("LNCD_LIMIT_ACTIVE_CONNECTIONS", 210)
62+
LNCD_STATS_INTERVAL = getEnvAsDuration("LNCD_STATS_INTERVAL", 1*time.Minute)
63+
LNCD_DEBUG = getEnvAsBool("LNCD_DEBUG", false)
64+
LNCD_PORT = getEnv("LNCD_PORT", "7167")
65+
LNCD_HOST = getEnv("LNCD_HOST", "0.0.0.0")
66+
LNCD_AUTH_TOKEN = getEnv("LNCD_AUTH_TOKEN", "")
67+
LNCD_TLS_CERT_PATH = getEnv("LNCD_TLS_CERT_PATH", "")
68+
LNCD_TLS_KEY_PATH = getEnv("LNCD_TLS_KEY_PATH", "")
69+
LNCD_HEALTHCHECK_SERVICE_PORT = getEnv("LNCD_HEALTHCHECK_SERVICE_PORT", "7168")
70+
LNCD_HEALTHCHECK_SERVICE_HOST = getEnv("LNCD_HEALTHCHECK_SERVICE_HOST", "127.0.0.1")
6971
)
7072

7173
// //////////////////////////////
@@ -474,7 +476,9 @@ func main() {
474476
log.Infof("LNCD_HOST: %v", LNCD_HOST)
475477
log.Infof("LNCD_TLS_CERT_PATH: %v", LNCD_TLS_CERT_PATH)
476478
log.Infof("LNCD_TLS_KEY_PATH: %v", LNCD_TLS_KEY_PATH)
477-
479+
log.Infof("LNCD_HEALTHCHECK_SERVICE_PORT: %v", LNCD_HEALTHCHECK_SERVICE_PORT)
480+
log.Infof("LNCD_HEALTHCHECK_SERVICE_HOST: %v", LNCD_HEALTHCHECK_SERVICE_HOST)
481+
478482
if UNSAFE_LOGS {
479483
log.Infof("LNCD_AUTH_TOKEN: %v", LNCD_AUTH_TOKEN)
480484
log.Infof("!!! UNSAFE LOGGING ENABLED !!!")
@@ -488,20 +492,36 @@ func main() {
488492
http.HandleFunc("/health", authMiddleware(healthCheckHandler))
489493
http.HandleFunc("/", formHandler)
490494

491-
log.Infof("Server starting at "+LNCD_HOST+":" + LNCD_PORT)
492-
var isTLS = LNCD_TLS_CERT_PATH != "" && LNCD_TLS_KEY_PATH != ""
493-
if isTLS {
494-
log.Infof("TLS enabled")
495-
if err := http.ListenAndServeTLS(LNCD_HOST+":"+LNCD_PORT, LNCD_TLS_CERT_PATH, LNCD_TLS_KEY_PATH, nil); err != nil {
496-
log.Errorf("Error starting server: %v", err)
497-
exit(err)
498-
}
499-
} else {
500-
if err := http.ListenAndServe(LNCD_HOST+":"+LNCD_PORT, nil); err != nil {
501-
log.Errorf("Error starting server: %v", err)
502-
exit(err)
495+
go func() {
496+
log.Infof("Server starting at "+LNCD_HOST+":" + LNCD_PORT)
497+
var isTLS = LNCD_TLS_CERT_PATH != "" && LNCD_TLS_KEY_PATH != ""
498+
if isTLS {
499+
log.Infof("TLS enabled")
500+
if err := http.ListenAndServeTLS(LNCD_HOST+":"+LNCD_PORT, LNCD_TLS_CERT_PATH, LNCD_TLS_KEY_PATH, nil); err != nil {
501+
log.Errorf("Error starting server: %v", err)
502+
exit(err)
503+
}
504+
} else {
505+
if err := http.ListenAndServe(LNCD_HOST+":"+LNCD_PORT, nil); err != nil {
506+
log.Errorf("Error starting server: %v", err)
507+
exit(err)
508+
}
503509
}
504-
}
510+
}()
511+
512+
513+
if LNCD_HEALTHCHECK_SERVICE_HOST != "" && LNCD_HEALTHCHECK_SERVICE_PORT != "" {
514+
go func() {
515+
log.Infof("HealthCheck service starting at "+LNCD_HEALTHCHECK_SERVICE_HOST+":" + LNCD_HEALTHCHECK_SERVICE_PORT)
516+
var rawHealthMux *http.ServeMux = http.NewServeMux()
517+
rawHealthMux.HandleFunc("/health", healthCheckHandler)
518+
if err := http.ListenAndServe(LNCD_HEALTHCHECK_SERVICE_HOST + ":" + LNCD_HEALTHCHECK_SERVICE_PORT, rawHealthMux); err != nil {
519+
log.Errorf("Error starting HealthCheck server: %v", err)
520+
exit(err)
521+
}
522+
}()
523+
}
524+
505525

506526
<-shutdownInterceptor.ShutdownChannel()
507527
log.Infof("Shutting down daemon")

0 commit comments

Comments
 (0)