Skip to content

Commit d16599f

Browse files
committed
feat: prefer util.WritablePath() for logs and local storage
1 parent 674393e commit d16599f

File tree

6 files changed

+57
-8
lines changed

6 files changed

+57
-8
lines changed

cmd/server/main.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func main() {
147147
}
148148
return "", false
149149
}
150+
writableBase := util.WritablePath()
150151
if value, ok := lookupEnv("PGSTORE_DSN", "pgstore_dsn"); ok {
151152
usePostgresStore = true
152153
pgStoreDSN = value
@@ -158,6 +159,13 @@ func main() {
158159
if value, ok := lookupEnv("PGSTORE_LOCAL_PATH", "pgstore_local_path"); ok {
159160
pgStoreLocalPath = value
160161
}
162+
if pgStoreLocalPath == "" {
163+
if writableBase != "" {
164+
pgStoreLocalPath = writableBase
165+
} else {
166+
pgStoreLocalPath = wd
167+
}
168+
}
161169
useGitStore = false
162170
}
163171
if value, ok := lookupEnv("GITSTORE_GIT_URL", "gitstore_git_url"); ok {
@@ -229,11 +237,14 @@ func main() {
229237
log.Infof("postgres-backed token store enabled, workspace path: %s", pgStoreInst.WorkDir())
230238
}
231239
} else if useObjectStore {
232-
objectStoreRoot := objectStoreLocalPath
233-
if objectStoreRoot == "" {
234-
objectStoreRoot = wd
240+
if objectStoreLocalPath == "" {
241+
if writableBase != "" {
242+
objectStoreLocalPath = writableBase
243+
} else {
244+
objectStoreLocalPath = wd
245+
}
235246
}
236-
objectStoreRoot = filepath.Join(objectStoreRoot, "objectstore")
247+
objectStoreRoot := filepath.Join(objectStoreLocalPath, "objectstore")
237248
resolvedEndpoint := strings.TrimSpace(objectStoreEndpoint)
238249
useSSL := true
239250
if strings.Contains(resolvedEndpoint, "://") {
@@ -289,7 +300,11 @@ func main() {
289300
}
290301
} else if useGitStore {
291302
if gitStoreLocalPath == "" {
292-
gitStoreLocalPath = wd
303+
if writableBase != "" {
304+
gitStoreLocalPath = writableBase
305+
} else {
306+
gitStoreLocalPath = wd
307+
}
293308
}
294309
gitStoreRoot = filepath.Join(gitStoreLocalPath, "gitstore")
295310
authDir := filepath.Join(gitStoreRoot, "auths")

internal/api/handlers/management/logs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"time"
1414

1515
"github.com/gin-gonic/gin"
16+
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
1617
)
1718

1819
const (
@@ -145,6 +146,9 @@ func (h *Handler) logDirectory() string {
145146
if h.logDir != "" {
146147
return h.logDir
147148
}
149+
if base := util.WritablePath(); base != "" {
150+
return filepath.Join(base, "logs")
151+
}
148152
if h.configFilePath != "" {
149153
dir := filepath.Dir(h.configFilePath)
150154
if dir != "" && dir != "." {

internal/api/server.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ type serverOptionConfig struct {
5252
type ServerOption func(*serverOptionConfig)
5353

5454
func defaultRequestLoggerFactory(cfg *config.Config, configPath string) logging.RequestLogger {
55-
return logging.NewFileRequestLogger(cfg.RequestLog, "logs", filepath.Dir(configPath))
55+
configDir := filepath.Dir(configPath)
56+
if base := util.WritablePath(); base != "" {
57+
return logging.NewFileRequestLogger(cfg.RequestLog, filepath.Join(base, "logs"), configDir)
58+
}
59+
return logging.NewFileRequestLogger(cfg.RequestLog, "logs", configDir)
5660
}
5761

5862
// WithMiddleware appends additional Gin middleware during server construction.
@@ -233,7 +237,11 @@ func NewServer(cfg *config.Config, authManager *auth.Manager, accessManager *sdk
233237
if optionState.localPassword != "" {
234238
s.mgmt.SetLocalPassword(optionState.localPassword)
235239
}
236-
s.mgmt.SetLogDirectory(filepath.Join(s.currentPath, "logs"))
240+
logDir := filepath.Join(s.currentPath, "logs")
241+
if base := util.WritablePath(); base != "" {
242+
logDir = filepath.Join(base, "logs")
243+
}
244+
s.mgmt.SetLogDirectory(logDir)
237245
s.localPassword = optionState.localPassword
238246

239247
// Setup routes

internal/logging/global_logger.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sync"
1111

1212
"github.com/gin-gonic/gin"
13+
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
1314
log "github.com/sirupsen/logrus"
1415
"gopkg.in/natefinch/lumberjack.v2"
1516
)
@@ -72,7 +73,10 @@ func ConfigureLogOutput(loggingToFile bool) error {
7273
defer writerMu.Unlock()
7374

7475
if loggingToFile {
75-
const logDir = "logs"
76+
logDir := "logs"
77+
if base := util.WritablePath(); base != "" {
78+
logDir = filepath.Join(base, "logs")
79+
}
7680
if err := os.MkdirAll(logDir, 0o755); err != nil {
7781
return fmt.Errorf("logging: failed to create log directory: %w", err)
7882
}

internal/managementasset/updater.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func StaticDir(configFilePath string) string {
6464
return cleaned
6565
}
6666

67+
if writable := util.WritablePath(); writable != "" {
68+
return filepath.Join(writable, "static")
69+
}
70+
6771
configFilePath = strings.TrimSpace(configFilePath)
6872
if configFilePath == "" {
6973
return ""

internal/util/util.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,17 @@ func CountAuthFiles(authDir string) int {
8484
}
8585
return count
8686
}
87+
88+
// WritablePath returns the cleaned WRITABLE_PATH environment variable when it is set.
89+
// It accepts both uppercase and lowercase variants for compatibility with existing conventions.
90+
func WritablePath() string {
91+
for _, key := range []string{"WRITABLE_PATH", "writable_path"} {
92+
if value, ok := os.LookupEnv(key); ok {
93+
trimmed := strings.TrimSpace(value)
94+
if trimmed != "" {
95+
return filepath.Clean(trimmed)
96+
}
97+
}
98+
}
99+
return ""
100+
}

0 commit comments

Comments
 (0)