diff --git a/app/gopherbook/config.go b/app/gopherbook/config.go new file mode 100644 index 0000000..9b70832 --- /dev/null +++ b/app/gopherbook/config.go @@ -0,0 +1,39 @@ +package main + +import ( + "os" + "path/filepath" +) + +var ( + baseLibraryPath string + baseCachePath string + baseEtcPath string + baseWatchPath string +) + +func init() { + // Store the base paths (before user-specific paths are added) + baseLibraryPath = libraryPath + baseCachePath = cachePath + baseEtcPath = etcPath + baseWatchPath = watchPath + + // Override from environment variables if set + if env := os.Getenv("GOPHERBOOK_LIBRARY"); env != "" { + baseLibraryPath = filepath.Clean(env) + libraryPath = baseLibraryPath + } + if env := os.Getenv("GOPHERBOOK_CACHE"); env != "" { + baseCachePath = filepath.Clean(env) + cachePath = baseCachePath + } + if env := os.Getenv("GOPHERBOOK_ETC"); env != "" { + baseEtcPath = filepath.Clean(env) + etcPath = baseEtcPath + } + if env := os.Getenv("GOPHERBOOK_WATCH"); env != "" { + baseWatchPath = filepath.Clean(env) + watchPath = baseWatchPath + } +} diff --git a/app/gopherbook/main.go b/app/gopherbook/main.go index 87bab00..7ed7bae 100644 --- a/app/gopherbook/main.go +++ b/app/gopherbook/main.go @@ -544,8 +544,8 @@ func handleLogin(w http.ResponseWriter, r *http.Request) { currentUser = req.Username key := deriveKey(req.Password) - libraryPath = filepath.Join("./library", currentUser) - cachePath = filepath.Join("./cache/covers", currentUser) + libraryPath = filepath.Join(baseLibraryPath, currentUser) + cachePath = filepath.Join(baseCachePath, currentUser) os.MkdirAll(filepath.Join(libraryPath, "Unorganized"), 0755) os.MkdirAll(cachePath, 0755) @@ -599,8 +599,8 @@ func handleLogout(w http.ResponseWriter, r *http.Request) { passwordsMutex.Unlock() currentEncryptionKey = nil currentUser = "" - libraryPath = "./library" - cachePath = "./cache/covers" + libraryPath = baseLibraryPath + cachePath = baseCachePath http.SetCookie(w, &http.Cookie{ Name: "session", @@ -2697,9 +2697,16 @@ func authMiddleware(next http.HandlerFunc) http.HandlerFunc { next(w, r) } } +func getUsersPath() string { + return filepath.Join(etcPath, "users.json") +} + +func getAdminPath() string { + return filepath.Join(etcPath, "admin.json") +} func loadUsers() { - data, err := os.ReadFile("etc/users.json") + data, err := os.ReadFile(getUsersPath()) if err != nil { return } @@ -2707,7 +2714,7 @@ func loadUsers() { log.Printf("Error unmarshaling users: %v", err) } - adminData, err := os.ReadFile("etc/admin.json") + adminData, err := os.ReadFile(getAdminPath()) if err == nil && len(adminData) > 0 { var adminConfig struct{ RegistrationEnabled bool } if err := json.Unmarshal(adminData, &adminConfig); err == nil { @@ -2718,13 +2725,13 @@ func loadUsers() { func saveUsers() { data, _ := json.MarshalIndent(users, "", " ") - os.WriteFile("etc/users.json", data, 0644) + os.WriteFile(getUsersPath(), data, 0644) } func saveAdminConfig() { config := struct{ RegistrationEnabled bool }{RegistrationEnabled: registrationEnabled} data, _ := json.MarshalIndent(config, "", " ") - os.WriteFile("etc/admin.json", data, 0644) + os.WriteFile(getAdminPath(), data, 0644) } func loadTags() {