Files
siujamo 442a1e2f9f refactor: move auth token to HttpOnly cookie, fix CORS and artefacts
Backend:
- Set JWT as HttpOnly cookie on login, add cookie fallback in auth middleware
- Add /auth/logout endpoint to clear cookie
- Fix CORS to echo origin with Allow-Credentials for withCredentials requests
- Make SPA static serving optional via SERVE_SPA config flag

Frontend:
- Remove manual token management; rely on HttpOnly cookie + withCredentials
- Simplify auth-slice to authenticated boolean flag
- Fix Artefact interface to match backend fields (sha256Hash, fileName, fileSize)
- Fix artefact upload endpoint and send version in FormData
- Use KiB/MiB for file size display
- Move Redux hooks from store/hooks to hooks/store
2026-07-07 15:57:26 +08:00

66 lines
1.5 KiB
Go

package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type Config struct {
ServerPort string
JWTSecret string
JWTExpiryHrs int
DatabaseURL string
ArtefactDir string
MaxConcurrent int
BandwidthBPS int
RateLimitRPS int
ServeSPA bool
}
func Load() *Config {
// Load .env file if present (silently ignore if not found)
if err := godotenv.Load(); err != nil {
log.Println("[config] No .env file found, using system environment variables")
}
return &Config{
ServerPort: envOrDefault("SERVER_PORT", "8080"),
JWTSecret: envOrDefault("JWT_SECRET", "change-me-in-production"),
JWTExpiryHrs: envOrDefaultInt("JWT_EXPIRY_HRS", 24),
DatabaseURL: envOrDefault("DATABASE_URL", "postgres://postgres:postgres@localhost:5432/manifest?sslmode=disable"),
ArtefactDir: envOrDefault("ARTEFACT_DIR", "/var/ota/artefacts"),
MaxConcurrent: envOrDefaultInt("MAX_CONCURRENT_DOWNLOADS", 50),
BandwidthBPS: envOrDefaultInt("BANDWIDTH_BPS", 10*1024*1024/50),
RateLimitRPS: envOrDefaultInt("RATE_LIMIT_RPS", 100),
ServeSPA: envOrDefaultBool("SERVE_SPA", true),
}
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func envOrDefaultInt(key string, def int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return def
}
func envOrDefaultBool(key string, def bool) bool {
if v := os.Getenv(key); v != "" {
if b, err := strconv.ParseBool(v); err == nil {
return b
}
}
return def
}