Files
spa-server/config.go
T

37 lines
911 B
Go
Raw Normal View History

package spaserver
import (
"os"
"strconv"
)
// Config holds the server configuration.
// All fields are populated from environment variables by [DefaultConfig].
type Config struct {
// Port is the TCP port the server listens on.
Port int `env:"EINHERJAR_SPA_PORT" envDefault:"8080"`
// StaticDir is the filesystem path that contains index.html and all assets.
StaticDir string `env:"EINHERJAR_SPA_STATIC_DIR" envDefault:"/srv/www"`
}
// DefaultConfig returns a Config populated from environment variables,
// falling back to safe defaults when variables are unset.
func DefaultConfig() Config {
port := 8080
if v := os.Getenv("EINHERJAR_SPA_PORT"); v != "" {
if p, err := strconv.Atoi(v); err == nil && p > 0 {
port = p
}
}
staticDir := "/srv/www"
if v := os.Getenv("EINHERJAR_SPA_STATIC_DIR"); v != "" {
staticDir = v
}
return Config{
Port: port,
StaticDir: staticDir,
}
}