Refuse invalid settings at start, health check follows PORT (closes #31)
check / check (push) Successful in 2m49s
check / check (push) Successful in 2m49s
PORT (1 to 65535) and XDG_DATA_HOME (an absolute path) are now checked in the config package, which is built before the database opens, so a bad value stops the start with an error and a non-zero exit instead of leaving the daemon running without HTTP or putting the database somewhere else. entrypoint.sh refuses a MALLOC_ARENA_MAX that is not a positive whole number, since glibc ignores a bad one silently. The HEALTHCHECK probes the port PORT names. The README corrects the Linux state directory, lists XDG_DATA_HOME, and adds a "Running under upaas" section. Model: opus-5-5
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,6 +19,12 @@ const (
|
||||
|
||||
// defaultRouteExpirationMinutes is the default route expiration timeout in minutes
|
||||
defaultRouteExpirationMinutes = 5
|
||||
|
||||
// defaultPort is the HTTP port used when PORT is not set
|
||||
defaultPort = 8080
|
||||
|
||||
// maxPort is the highest TCP port number
|
||||
maxPort = 65535
|
||||
)
|
||||
|
||||
// Config holds configuration for the entire application
|
||||
@@ -25,6 +32,9 @@ type Config struct {
|
||||
// StateDir is the directory for all application state (database, snapshots)
|
||||
StateDir string
|
||||
|
||||
// Port is the TCP port the HTTP server listens on
|
||||
Port int
|
||||
|
||||
// MaxRuntime is the maximum runtime (0 = run forever)
|
||||
MaxRuntime time.Duration
|
||||
|
||||
@@ -43,8 +53,14 @@ func New() (*Config, error) {
|
||||
return nil, fmt.Errorf("failed to determine state directory: %w", err)
|
||||
}
|
||||
|
||||
port, err := getPort()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Config{
|
||||
StateDir: stateDir,
|
||||
Port: port,
|
||||
MaxRuntime: 0, // Run forever by default
|
||||
EnableBatchedDatabaseWrites: true, // Enable batching by default
|
||||
RouteExpirationTimeout: defaultRouteExpirationMinutes * time.Minute, // For active route monitoring
|
||||
@@ -69,13 +85,20 @@ func getStateDirectory() (string, error) {
|
||||
return filepath.Join(home, "Library", "Application Support", AppIdentifier), nil
|
||||
|
||||
case "linux", "freebsd", "openbsd", "netbsd":
|
||||
// The XDG spec requires an absolute path; a relative one would put
|
||||
// the database somewhere unexpected.
|
||||
xdgData := os.Getenv("XDG_DATA_HOME")
|
||||
if xdgData != "" && !filepath.IsAbs(xdgData) {
|
||||
return "", fmt.Errorf("XDG_DATA_HOME must be an absolute path, got %q", xdgData)
|
||||
}
|
||||
|
||||
// Unix-like: /var/lib/berlin.sneak.app.routewatch if root, else XDG_DATA_HOME
|
||||
if os.Geteuid() == 0 {
|
||||
return filepath.Join("/var/lib", AppIdentifier), nil
|
||||
}
|
||||
|
||||
// Check XDG_DATA_HOME first
|
||||
if xdgData := os.Getenv("XDG_DATA_HOME"); xdgData != "" {
|
||||
if xdgData != "" {
|
||||
return filepath.Join(xdgData, AppIdentifier), nil
|
||||
}
|
||||
|
||||
@@ -92,6 +115,21 @@ func getStateDirectory() (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// getPort returns the HTTP port from PORT, or defaultPort when PORT is not set
|
||||
func getPort() (int, error) {
|
||||
value := os.Getenv("PORT")
|
||||
if value == "" {
|
||||
return defaultPort, nil
|
||||
}
|
||||
|
||||
port, err := strconv.Atoi(value)
|
||||
if err != nil || port < 1 || port > maxPort {
|
||||
return 0, fmt.Errorf("PORT must be a whole number from 1 to %d, got %q", maxPort, value)
|
||||
}
|
||||
|
||||
return port, nil
|
||||
}
|
||||
|
||||
// EnsureDirectories creates all necessary directories if they don't exist
|
||||
func (c *Config) EnsureDirectories() error {
|
||||
// Ensure state directory exists
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewReadsPort(t *testing.T) {
|
||||
tests := map[string]int{
|
||||
"": defaultPort,
|
||||
"1": 1,
|
||||
"9090": 9090,
|
||||
"65535": 65535,
|
||||
}
|
||||
|
||||
for value, want := range tests {
|
||||
t.Run(value, func(t *testing.T) {
|
||||
t.Setenv("PORT", value)
|
||||
t.Setenv("XDG_DATA_HOME", "")
|
||||
|
||||
cfg, err := New()
|
||||
if err != nil {
|
||||
t.Fatalf("New() with PORT=%q: %v", value, err)
|
||||
}
|
||||
|
||||
if cfg.Port != want {
|
||||
t.Errorf("New() with PORT=%q: Port = %d, want %d", value, cfg.Port, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRefusesInvalidPort(t *testing.T) {
|
||||
for _, value := range []string{"0", "65536", "-1", "http", "80.5"} {
|
||||
t.Run(value, func(t *testing.T) {
|
||||
t.Setenv("PORT", value)
|
||||
t.Setenv("XDG_DATA_HOME", "")
|
||||
|
||||
if _, err := New(); err == nil {
|
||||
t.Errorf("New() with PORT=%q returned no error", value)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRefusesRelativeXDGDataHome(t *testing.T) {
|
||||
if runtime.GOOS == "darwin" {
|
||||
t.Skip("macOS does not read XDG_DATA_HOME")
|
||||
}
|
||||
|
||||
t.Setenv("PORT", "")
|
||||
|
||||
t.Setenv("XDG_DATA_HOME", "relative/path")
|
||||
if _, err := New(); err == nil {
|
||||
t.Error("New() with a relative XDG_DATA_HOME returned no error")
|
||||
}
|
||||
|
||||
t.Setenv("XDG_DATA_HOME", "/var/lib")
|
||||
if _, err := New(); err != nil {
|
||||
t.Errorf("New() with XDG_DATA_HOME=/var/lib: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user