Like job-tracker, the tracker forwards the visitor's cookies to the site backend's /auth/validate-admin instead of asking for an API key. Signed in, the dashboard shows every device's data. The new /setup page has instructions for each collector and creates and revokes API keys. The userscript is served publicly at /tracker.user.js. Co-Authored-By: Claude Opus 5.5 <[email protected]>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
// Package config loads server configuration from environment variables.
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type Config struct {
|
|
DatabaseURL string
|
|
ListenAddr string
|
|
AutoMigrate bool
|
|
Location *time.Location // timezone used for day boundaries in summaries
|
|
|
|
// AuthMode decides who may open the dashboard: "forward" asks the main
|
|
// site's AuthValidateURL with the visitor's cookies, "dev" admits anyone
|
|
// and needs DEV_MODE=true.
|
|
AuthMode string
|
|
AuthValidateURL string
|
|
}
|
|
|
|
func Load() (Config, error) {
|
|
c := Config{
|
|
DatabaseURL: os.Getenv("DATABASE_URL"),
|
|
ListenAddr: envOr("LISTEN_ADDR", ":8080"),
|
|
}
|
|
if c.DatabaseURL == "" {
|
|
return c, errors.New("DATABASE_URL is required")
|
|
}
|
|
|
|
var err error
|
|
if c.AutoMigrate, err = strconv.ParseBool(envOr("AUTO_MIGRATE", "true")); err != nil {
|
|
return c, fmt.Errorf("AUTO_MIGRATE: %w", err)
|
|
}
|
|
if c.Location, err = time.LoadLocation(envOr("TRACKER_TZ", "UTC")); err != nil {
|
|
return c, fmt.Errorf("TRACKER_TZ: %w", err)
|
|
}
|
|
|
|
c.AuthMode = envOr("AUTH_MODE", "forward")
|
|
c.AuthValidateURL = envOr("AUTH_VALIDATE_URL", "http://backend:8080/auth/validate-admin")
|
|
switch c.AuthMode {
|
|
case "forward":
|
|
case "dev":
|
|
if dev, _ := strconv.ParseBool(os.Getenv("DEV_MODE")); !dev {
|
|
return c, errors.New("AUTH_MODE=dev is only allowed with DEV_MODE=true")
|
|
}
|
|
default:
|
|
return c, fmt.Errorf("unknown AUTH_MODE %q (want forward or dev)", c.AuthMode)
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func envOr(key, def string) string {
|
|
if v := os.Getenv(key); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|