Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c3cdb38
config: change default config dir from ~/.free-llm-proxy-router to ~/…
testusernamegithub Nov 22, 2026
7b3f9e5
cmd: update config paths in all CLI tools to use ~/.config/free-llm-p…
testusernamegithub Nov 22, 2026
767eed2
configs: update example config files to use ~/.config/free-llm-proxy-…
testusernamegithub Nov 22, 2026
0f6a39a
docs: update README and .env.example to reference ~/.config/free-llm-…
testusernamegithub Nov 22, 2026
4e6e137
scripts: update setup.sh and cron-refresh.sh to use ~/.config/free-ll…
testusernamegithub Nov 22, 2026
7f2ce53
Dockerfile: update comment to reference ~/.config/free-llm-proxy-router
testusernamegithub Nov 22, 2026
714cea9
Dockerfile: update comment to reference ~/.config/free-llm-proxy-router
testusernamegithub Nov 22, 2026
61783d5
Merge branch 'kaiser-data:main' into moved-config-to-.config
BenediktChlopik Nov 28, 2026
d3746b7
config: migrate from ~/.free-llm-proxy-router to ~/.config/free-llm-p…
testusernamegithub Nov 28, 2026
b138f75
config: respect XDG_CONFIG_HOME for config directory
testusernamegithub Nov 28, 2026
17fcf8b
config: resolve merge conflicts and apply XDG_CONFIG_HOME review feed…
testusernamegithub Nov 30, 2026
00824f8
test(config): cover XDG config legacy fallback
testusernamegithub Nov 30, 2026
6df7b18
config: resolve remaining merge conflicts and commit fallback test
testusernamegithub Nov 30, 2026
1aa4569
feat(proxy): use XDG state file paths in commands
testusernamegithub Nov 30, 2026
ccebe7c
refactor(config): add XDG config directory with legacy fallback
testusernamegithub Nov 30, 2026
8c757a3
fix(config): resolve merge conflicts from XDG config refactoring
testusernamegithub Nov 30, 2026
7c7b956
config: add StateReadPath with legacy fallback
testusernamegithub Dec 1, 2026
a60415d
proxy: use config read paths for catalog and state files
testusernamegithub Dec 1, 2026
e1450b9
scan: remove inline legacy catalog fallback
testusernamegithub Dec 1, 2026
b2f54ab
docs: update README scp command for XDG_CONFIG_HOME fallback
testusernamegithub Dec 1, 2026
01f77de
bench: remove extra blank lines
testusernamegithub Dec 1, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Keys are loaded in this order (first match wins):
# 1. Real environment variable (export KEY=...)
# 2. .env in the project directory ← this file
# 3. ~/.free-llm-proxy-router/.secrets
# 3. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (default: ~/.config/free-llm-proxy-router/.secrets)
#
# .env is gitignored — never commit real keys.
# Leave unused providers blank; they will be skipped.
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ WORKDIR /app
COPY free-llm-proxy free-llm-scan /app/
COPY configs /app/configs

# Usage/reliability state and scanned catalogs go to $HOME/.free-llm-proxy-router
# Usage/reliability state and scanned catalogs go to $XDG_CONFIG_HOME/free-llm-proxy-router
# (or ~/.config/free-llm-proxy-router if XDG_CONFIG_HOME is not set).
# (HOME=/home/nonroot, writable). Mount a volume there to keep them.
USER nonroot:nonroot
EXPOSE 8080
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ The [install script](#install) picks the right archive on the board itself. For

```bash
CGO_ENABLED=0 GOOS=linux GOARCH=mipsle go build -ldflags="-s -w" -o free-llm-proxy ./cmd/free-llm-proxy
scp -r free-llm-proxy configs/ user@board.local:~/free-llm-proxy-router/ # keep configs/ next to the binary
scp -r free-llm-proxy configs/ user@board.local:${XDG_CONFIG_HOME:-~/.config}/free-llm-proxy-router/ # keep configs/ next to the binary
```

On an SD card, `proxy.log_level: "warn"` drops the per-request log lines.
Expand Down Expand Up @@ -282,9 +282,9 @@ Keys are read from, first match wins:

1. environment variables (`export GROQ_API_KEY=...`)
2. `.env` in the working directory (see [`.env.example`](.env.example) for every supported key)
3. `~/.free-llm-proxy-router/.env` or `.secrets`, which work from any directory
3. `$XDG_CONFIG_HOME/free-llm-proxy-router/.env` or `.secrets`, which work from any directory

Config is read from `-c path`, `./config.yaml` or `~/.free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `~/.free-llm-proxy-router/`.
Config is read from `-c path`, `./config.yaml` or `$XDG_CONFIG_HOME/free-llm-proxy-router/config.yaml`, and falls back to the bundled [`configs/config.example.yaml`](configs/config.example.yaml). Start it with `-c path/to/config.yaml` and most edits apply without a restart (`auth_token` and `host` need one). `bash scripts/setup.sh` writes a starter config to `$XDG_CONFIG_HOME/free-llm-proxy-router/`.

```yaml
proxy:
Expand Down
4 changes: 2 additions & 2 deletions cmd/free-llm-bench/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func runBench(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("loading config: %w", err)
}
cat, err := catalog.Load(cfg.Catalog.Path)
cat, err := catalog.Load(config.CatalogReadPath(cfg.Catalog.Path, cfgFile))
if err != nil {
return fmt.Errorf("loading catalog: %w", err)
}

// Build all strategies
relTracker := reliability.New()
reliability.Load(relTracker, "~/.free-llm-proxy-router/reliability.json")
reliability.Load(relTracker, config.StateFilePath("reliability.json"))
rateLimiter := ratelimit.NewGlobalTracker()
stratReg := strategy.NewRegistry(relTracker, rateLimiter, "", "gemini-2.0-flash-lite", 3, 5, strategy.LongContextOptions{})

Expand Down
21 changes: 13 additions & 8 deletions cmd/free-llm-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
Short: "OpenAI-compatible proxy for free LLM providers",
RunE: runProxy,
}
root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and ~/.free-llm-proxy-router/)")
root.Flags().StringVarP(&cfgFile, "config", "c", "", "config file path (default: search in . and $XDG_CONFIG_HOME/free-llm-proxy-router/)")
root.Flags().StringVar(&host, "host", "", "override listen address (default from config: 127.0.0.1; use 0.0.0.0 to expose)")
root.Flags().IntVarP(&port, "port", "p", 0, "override proxy port (default from config: 8080)")
root.Flags().BoolVar(&allowNoAuth, "allow-no-auth", false, "allow a non-loopback --host without proxy.auth_token (access must be restricted some other way)")
Expand Down Expand Up @@ -68,7 +68,8 @@ func runProxy(cmd *cobra.Command, args []string) error {
cfg.Proxy.AllowNoAuth = true
}

// Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path)
// Load model catalog (bundled catalog.json until the first scan writes cfg.Catalog.Path).
// CatalogReadPath handles legacy fallback on the read side.
catPath := config.CatalogReadPath(cfg.Catalog.Path, cfgFile)
cat, err := catalog.Load(catPath)
if err != nil {
Expand Down Expand Up @@ -117,18 +118,18 @@ func runProxy(cmd *cobra.Command, args []string) error {
rateLimiter := ratelimit.NewGlobalTracker()
geminiTracker := ratelimit.NewGeminiTracker()

// Load persisted rate-limit usage
usagePath := "~/.free-llm-proxy-router/usage.json"
// Load persisted rate-limit usage (new location with legacy fallback)
usagePath := config.StateReadPath("usage.json")
if usage, err := ratelimit.LoadUsage(usagePath); err != nil {
log.Printf("usage.json unreadable (starting fresh): %v", err)
} else {
rateLimiter.Restore(usage)
geminiTracker.Restore(usage.Gemini)
}

// Set up reliability tracker
// Set up reliability tracker (new location with legacy fallback)
reliabilityTracker := reliability.New()
relPath := "~/.free-llm-proxy-router/reliability.json"
relPath := config.StateReadPath("reliability.json")
if err := reliability.Load(reliabilityTracker, relPath); err != nil {
log.Printf("reliability.json not found (starting fresh): %v", err)
}
Expand Down Expand Up @@ -209,13 +210,16 @@ func runProxy(cmd *cobra.Command, args []string) error {

// Persist daily usage and reliability stats periodically and on shutdown,
// so a crash or kill -9 loses at most a few minutes of quota tracking.
// Always write to the new location ($XDG_CONFIG_HOME/free-llm-proxy-router/).
newUsagePath := config.StateFilePath("usage.json")
newRelPath := config.StateFilePath("reliability.json")
saveState := func() {
usage := rateLimiter.Snapshot()
usage.Gemini = geminiTracker.Snapshot()
if err := ratelimit.SaveUsage(usage, usagePath); err != nil {
if err := ratelimit.SaveUsage(usage, newUsagePath); err != nil {
log.Printf("saving usage: %v", err)
}
if err := reliability.Save(reliabilityTracker, relPath); err != nil {
if err := reliability.Save(reliabilityTracker, newRelPath); err != nil {
log.Printf("saving reliability: %v", err)
}
}
Expand Down Expand Up @@ -267,3 +271,4 @@ func rescanCatalog(ctx context.Context, providers []config.ProviderConfig, path
}
return catalog.Save(newCat, path)
}

17 changes: 13 additions & 4 deletions cmd/free-llm-scan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ var (
)

// loadConfig loads the config and applies the --catalog override.
// Writers always write to the configured path; CatalogReadPath handles
// legacy fallback on the read side so users migrate naturally.
func loadConfig() (*config.Config, error) {
cfg, err := config.Load(cfgFile)
if err != nil {
Expand All @@ -33,6 +35,13 @@ func loadConfig() (*config.Config, error) {
return cfg, nil
}

// loadCatalogForRead loads the catalog for read operations (probe, refresh, export),
// applying the legacy fallback only when the configured path is the default one.
func loadCatalogForRead(cfg *config.Config) (*catalog.Catalog, error) {
readPath := config.CatalogReadPath(cfg.Catalog.Path, cfgFile)
return catalog.Load(readPath)
}

func main() {
root := &cobra.Command{
Use: "free-llm-scan",
Expand Down Expand Up @@ -133,7 +142,7 @@ func updateCmd() *cobra.Command {
func checkScanTargets(providers []config.ProviderConfig) error {
if len(providers) == 0 {
return fmt.Errorf("no providers configured: no config.yaml was found in ., " +
"~/.free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " +
"$XDG_CONFIG_HOME/free-llm-proxy-router or configs/, and no bundled config.example.yaml next to the binary. " +
"Run `bash scripts/setup.sh`, or pass `-c path/to/config.yaml`")
}
for _, p := range providers {
Expand All @@ -155,7 +164,7 @@ func probeCmd() *cobra.Command {
if err != nil {
return err
}
cat, err := catalog.Load(cfg.Catalog.Path)
cat, err := loadCatalogForRead(cfg)
if err != nil {
return fmt.Errorf("loading catalog: %w", err)
}
Expand Down Expand Up @@ -219,7 +228,7 @@ func refreshLLMCmd() *cobra.Command {
if err != nil {
return err
}
cat, err := catalog.Load(cfg.Catalog.Path)
cat, err := loadCatalogForRead(cfg)
if err != nil {
return fmt.Errorf("loading catalog: %w", err)
}
Expand Down Expand Up @@ -268,7 +277,7 @@ func exportMDCmd() *cobra.Command {
if err != nil {
return err
}
cat, err := catalog.Load(cfg.Catalog.Path)
cat, err := loadCatalogForRead(cfg)
if err != nil {
return fmt.Errorf("loading catalog: %w", err)
}
Expand Down
8 changes: 5 additions & 3 deletions configs/.secrets.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# free-llm-proxy-router API keys
# Copy this file to ~/.free-llm-proxy-router/.secrets (or .env in the project root)
# Copy this file to $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or ~/.config/free-llm-proxy-router/.secrets)
# or the legacy ~/.free-llm-proxy-router/.secrets (or .env in the project root)
# and fill in your keys. Lines starting with # are ignored.
#
# Key lookup order (first match wins):
# 1. Real environment variable (export KEY=...)
# 2. ~/.free-llm-proxy-router/.secrets (or .env)
# 3. .secrets / .env in the project directory
# 2. $XDG_CONFIG_HOME/free-llm-proxy-router/.secrets (or ~/.config/free-llm-proxy-router/.secrets)
# 3. ~/.free-llm-proxy-router/.secrets (legacy)
# 4. .secrets / .env in the project directory
#
# NEVER commit this file to git.

Expand Down
4 changes: 2 additions & 2 deletions configs/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ refresh:
output_merge: "conservative"

catalog:
path: "~/.free-llm-proxy-router/catalog.json"
enriched_path: "~/.free-llm-proxy-router/enriched.json"
path: "~/.config/free-llm-proxy-router/catalog.json"
enriched_path: "~/.config/free-llm-proxy-router/enriched.json"
max_age_hours: 24
auto_scan_on_start: false

Expand Down
97 changes: 92 additions & 5 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -130,8 +131,9 @@ var mu sync.RWMutex
var current *Config

// Load reads the configuration from the given file path.
// If path is empty, it searches for config.yaml in the current directory
// and in ~/.free-llm-proxy-router/.
// If path is empty, it searches for config.yaml in the current directory,
// the XDG config directory ($XDG_CONFIG_HOME/free-llm-proxy-router or ~/.config/free-llm-proxy-router),
// and the legacy directory (~/.free-llm-proxy-router/).
//
// Before returning, Load looks for .env and .secrets files in standard
// locations and populates them into the dotenv map so that ResolvedAuth()
Expand All @@ -149,10 +151,14 @@ func Load(path string) (*Config, error) {
v.SetConfigName("config")
v.SetConfigType("yaml")
v.AddConfigPath(".")
v.AddConfigPath(expandHome("~/.free-llm-proxy-router"))
v.AddConfigPath(NewConfigDir())
v.AddConfigPath(OldConfigDir())
v.AddConfigPath("configs")
}

// Log a hint if only the legacy config directory exists.
checkLegacyConfig(configDir)

// Load .env / .secrets files before AutomaticEnv so real env vars win.
loadDotenv(defaultEnvPaths(configDir)...)

Expand All @@ -172,6 +178,15 @@ func Load(path string) (*Config, error) {
}
log.Printf("config: no config.yaml found — using bundled %s", bundled)
}
} else if path == "" {
// Config was found via search paths. If it came from the legacy dir
// and the new dir doesn't have a config yet, log a one-line hint.
used := v.ConfigFileUsed()
legacyDir := OldConfigDir()
newDir := NewConfigDir()
if strings.HasPrefix(used, legacyDir) && !fileExists(filepath.Join(newDir, "config.yaml")) {
log.Printf("config: found config in legacy location %s — consider moving it to %s", legacyDir, newDir)
}
}

var cfg Config
Expand Down Expand Up @@ -262,8 +277,8 @@ func setDefaults(v *viper.Viper) {
v.SetDefault("proxy.cache_ttl", 300)
v.SetDefault("proxy.log_level", "info")
v.SetDefault("proxy.strategy", "adaptive")
v.SetDefault("catalog.path", expandHome("~/.free-llm-proxy-router/catalog.json"))
v.SetDefault("catalog.enriched_path", expandHome("~/.free-llm-proxy-router/enriched.json"))
v.SetDefault("catalog.path", filepath.Join(NewConfigDir(), "catalog.json"))
v.SetDefault("catalog.enriched_path", filepath.Join(NewConfigDir(), "enriched.json"))
v.SetDefault("catalog.max_age_hours", 24)
v.SetDefault("fallback.retry_on_429", true)
v.SetDefault("fallback.retry_on_5xx", true)
Expand All @@ -286,6 +301,78 @@ func expandHome(path string) string {
return path
}

// ExpandHome is the public version of expandHome for use by other packages.
func ExpandHome(path string) string {
return expandHome(path)
}

// FileExists reports whether the given path exists and is not a directory.
func FileExists(path string) bool {
return fileExists(path)
}

// OldConfigDir returns the legacy config directory (~/.free-llm-proxy-router).
func OldConfigDir() string {
return expandHome("~/.free-llm-proxy-router")
}

// NewConfigDir returns the current config directory, respecting XDG_CONFIG_HOME.
// On all platforms: $XDG_CONFIG_HOME/free-llm-proxy-router if set, otherwise ~/.config/free-llm-proxy-router.
// This matches the setup.sh and cron-refresh.sh scripts.
func NewConfigDir() string {
if xdg := os.Getenv("XDG_CONFIG_HOME"); xdg != "" {
return filepath.Join(xdg, "free-llm-proxy-router")
}
return expandHome("~/.config/free-llm-proxy-router")
}

// StateFilePath returns the path to a state file (usage.json, reliability.json, etc.)
// in the new config directory. It does NOT fall back to the legacy location;
// writers always write to the new location.
func StateFilePath(filename string) string {
return filepath.Join(NewConfigDir(), filename)
}

// StateReadPath returns the path to a state file for reading, with legacy fallback.
// It checks the new location first, then the legacy location, and falls back to
// the new location if neither exists (so writers create the file in the new dir).
func StateReadPath(filename string) string {
newPath := StateFilePath(filename)
if fileExists(newPath) {
return newPath
}
legacyPath := filepath.Join(OldConfigDir(), filename)
if fileExists(legacyPath) {
return legacyPath
}
return newPath
}

// DefaultCatalogPath returns the default catalog path used by setDefaults.
// This is useful for checking whether a configured path is the default one.
func DefaultCatalogPath() string {
return filepath.Join(NewConfigDir(), "catalog.json")
}

// DefaultEnrichedPath returns the default enriched catalog path used by setDefaults.
func DefaultEnrichedPath() string {
return filepath.Join(NewConfigDir(), "enriched.json")
}

// checkLegacyConfig logs a one-time hint if only the legacy config directory exists.
func checkLegacyConfig(configDir string) {
if configDir != "" {
return // explicit config file given, no hint needed
}
oldDir := OldConfigDir()
newDir := NewConfigDir()
oldExists := fileExists(filepath.Join(oldDir, "config.yaml"))
newExists := fileExists(filepath.Join(newDir, "config.yaml"))
if oldExists && !newExists {
log.Printf("config: found legacy %s/config.yaml — consider moving it to %s/", oldDir, newDir)
}
}

// lookupEnv returns the value for key, checking real env vars first,
// then falling back to any value loaded from .env / .secrets files.
func lookupEnv(key string) string {
Expand Down
Loading
Loading