mirror of
https://github.com/tale/headplane.git
synced 2026-08-05 11:47:41 +00:00
feat: automate health check with a written file
This commit is contained in:
@@ -5,27 +5,37 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// hp_healthcheck pings the Headplane /healthz endpoint. It's invoked
|
||||
// from the Docker HEALTHCHECK directive, so it must work whether the
|
||||
// container is serving plain HTTP or has TLS termination enabled.
|
||||
// hp_healthcheck pings the Headplane healthz endpoint. It's invoked
|
||||
// from the Docker HEALTHCHECK directive inside the same container
|
||||
// as Headplane itself.
|
||||
//
|
||||
// Configuration (via env, all optional):
|
||||
// - HEADPLANE_HEALTHCHECK_URL full URL, takes precedence over the
|
||||
// pieces below (default: http://localhost:3000/admin/healthz)
|
||||
// - HEADPLANE_HEALTHCHECK_TLS "true" to use https://
|
||||
// - HEADPLANE_HEALTHCHECK_HOST default: localhost
|
||||
// - HEADPLANE_HEALTHCHECK_PORT default: 3000
|
||||
// - HEADPLANE_HEALTHCHECK_PATH default: /admin/healthz
|
||||
// To stay fully zero-config, Headplane writes the exact URL the
|
||||
// healthcheck should hit — scheme, port, and basename included — to
|
||||
// listenFile when it starts accepting connections (see
|
||||
// runtime/http.ts and app/server/main.ts). This binary just reads
|
||||
// that file and GETs the URL verbatim. No env vars, no YAML
|
||||
// parsing, no path-joining, no compile-time knowledge of the
|
||||
// basename.
|
||||
//
|
||||
// If the file is missing (e.g. the server hasn't finished booting
|
||||
// on the very first probe, or this is an old image being run with
|
||||
// a new healthcheck) we fall back to the historical default.
|
||||
const (
|
||||
listenFile = "/tmp/headplane-listen"
|
||||
defaultURL = "http://localhost:3000/admin/healthz"
|
||||
)
|
||||
|
||||
func main() {
|
||||
url := healthcheckURL()
|
||||
url := readListenFile()
|
||||
|
||||
client := http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
Transport: &http.Transport{
|
||||
// We just care that the server is alive and with us
|
||||
// Self-signed certs are normal for in-process TLS termination
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
},
|
||||
}
|
||||
@@ -43,33 +53,16 @@ func main() {
|
||||
}
|
||||
|
||||
fmt.Println("Health check passed.")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func healthcheckURL() string {
|
||||
if v := os.Getenv("HEADPLANE_HEALTHCHECK_URL"); v != "" {
|
||||
return v
|
||||
func readListenFile() string {
|
||||
data, err := os.ReadFile(listenFile)
|
||||
if err != nil {
|
||||
return defaultURL
|
||||
}
|
||||
|
||||
scheme := "http"
|
||||
if os.Getenv("HEADPLANE_HEALTHCHECK_TLS") == "true" {
|
||||
scheme = "https"
|
||||
url := strings.TrimSpace(string(data))
|
||||
if url == "" {
|
||||
return defaultURL
|
||||
}
|
||||
|
||||
host := os.Getenv("HEADPLANE_HEALTHCHECK_HOST")
|
||||
if host == "" {
|
||||
host = "localhost"
|
||||
}
|
||||
|
||||
port := os.Getenv("HEADPLANE_HEALTHCHECK_PORT")
|
||||
if port == "" {
|
||||
port = "3000"
|
||||
}
|
||||
|
||||
path := os.Getenv("HEADPLANE_HEALTHCHECK_PATH")
|
||||
if path == "" {
|
||||
path = "/admin/healthz"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s://%s:%s%s", scheme, host, port, path)
|
||||
return url
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user