Files
gitea-mobile/internal/config/config.go
T
agent-company 703b2fafb0 feat: add env-based configuration and token-in-cookie auth
Implement 12-factor configuration via environment variables and
token-in-cookie authentication for Gitea API access.

- internal/config/config.go: reads GITEA_URL, GITEA_TOKEN, LISTEN_ADDR,
  SESSION_SECRET from environment with validation
- internal/auth/cookie.go: HMAC-signed HTTP-only cookie for storing
  Gitea API tokens (Secure, SameSite=Strict)
- internal/middleware/auth.go: extracts token from cookie, injects into
  request context, redirects unauthenticated users to /settings
- internal/middleware/logging.go: structured JSON request logging
- internal/handlers/settings.go: settings page for entering/removing
  Gitea API token with mobile-first dark UI
- cmd/server/main.go: integrated config, auth middleware, and settings

Includes unit tests for config loading, cookie signing/verification,
and auth middleware bypass/redirect logic.

Closes leeworks-agents/gitea-mobile#2

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 04:05:31 +00:00

51 lines
1.2 KiB
Go

package config
import (
"fmt"
"os"
)
// Config holds application configuration loaded from environment variables.
type Config struct {
// GiteaURL is the base URL of the Gitea instance.
GiteaURL string
// GiteaToken is the default API token (optional; users can set their own via cookie).
GiteaToken string
// ListenAddr is the server listen address.
ListenAddr string
// SessionSecret is the HMAC key for signing session cookies.
SessionSecret string
}
// Load reads configuration from environment variables.
// Returns an error if required variables are missing.
func Load() (*Config, error) {
cfg := &Config{
GiteaURL: os.Getenv("GITEA_URL"),
GiteaToken: os.Getenv("GITEA_TOKEN"),
ListenAddr: os.Getenv("LISTEN_ADDR"),
SessionSecret: os.Getenv("SESSION_SECRET"),
}
if cfg.ListenAddr == "" {
cfg.ListenAddr = ":8080"
}
if cfg.GiteaURL == "" {
return nil, fmt.Errorf("GITEA_URL environment variable is required")
}
if cfg.SessionSecret == "" {
return nil, fmt.Errorf("SESSION_SECRET environment variable is required")
}
if len(cfg.SessionSecret) < 32 {
return nil, fmt.Errorf("SESSION_SECRET must be at least 32 characters")
}
return cfg, nil
}