Add dynamic log level adjustment and "type" field to auth files response

- Introduced `SetLogLevel` utility function for unified log level management.
- Updated dynamic log level handling across server and watcher components.
- Extended auth files response by extracting and including the `type` field from file content.
- Updated management API documentation with the new `type` field in auth files response.
This commit is contained in:
Luis Pater
2025-09-08 01:09:39 +08:00
parent 46fa32f087
commit c875088be2
8 changed files with 49 additions and 14 deletions

View File

@@ -466,7 +466,7 @@ Manage JSON token files under `auth-dir`: list, download, upload, delete.
``` ```
- Response: - Response:
```json ```json
{ "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z" } ] } { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z", "type": "google" } ] }
``` ```
- GET `/auth-files/download?name=<file.json>` — Download a single file - GET `/auth-files/download?name=<file.json>` — Download a single file

View File

@@ -466,7 +466,7 @@
``` ```
- 响应: - 响应:
```json ```json
{ "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z" } ] } { "files": [ { "name": "acc1.json", "size": 1234, "modtime": "2025-08-30T12:34:56Z", "type": "google" } ] }
``` ```
- GET `/auth-files/download?name=<file.json>` — 下载单个文件 - GET `/auth-files/download?name=<file.json>` — 下载单个文件

View File

@@ -14,6 +14,7 @@ import (
"github.com/luispater/CLIProxyAPI/internal/cmd" "github.com/luispater/CLIProxyAPI/internal/cmd"
"github.com/luispater/CLIProxyAPI/internal/config" "github.com/luispater/CLIProxyAPI/internal/config"
_ "github.com/luispater/CLIProxyAPI/internal/translator" _ "github.com/luispater/CLIProxyAPI/internal/translator"
"github.com/luispater/CLIProxyAPI/internal/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -112,11 +113,7 @@ func main() {
} }
// Set the log level based on the configuration. // Set the log level based on the configuration.
if cfg.Debug { util.SetLogLevel(cfg)
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
// Expand the tilde (~) in the auth directory path to the user's home directory. // Expand the tilde (~) in the auth directory path to the user's home directory.
if strings.HasPrefix(cfg.AuthDir, "~") { if strings.HasPrefix(cfg.AuthDir, "~") {

View File

@@ -8,6 +8,7 @@ import (
"strings" "strings"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/tidwall/gjson"
) )
// List auth files // List auth files
@@ -27,7 +28,16 @@ func (h *Handler) ListAuthFiles(c *gin.Context) {
continue continue
} }
if info, errInfo := e.Info(); errInfo == nil { if info, errInfo := e.Info(); errInfo == nil {
files = append(files, gin.H{"name": name, "size": info.Size(), "modtime": info.ModTime()}) fileData := gin.H{"name": name, "size": info.Size(), "modtime": info.ModTime()}
// Read file to get type field
full := filepath.Join(h.cfg.AuthDir, name)
if data, errRead := os.ReadFile(full); errRead == nil {
typeValue := gjson.GetBytes(data, "type").String()
fileData["type"] = typeValue
}
files = append(files, fileData)
} }
} }
c.JSON(200, gin.H{"files": files}) c.JSON(200, gin.H{"files": files})

View File

@@ -22,6 +22,7 @@ import (
"github.com/luispater/CLIProxyAPI/internal/config" "github.com/luispater/CLIProxyAPI/internal/config"
"github.com/luispater/CLIProxyAPI/internal/interfaces" "github.com/luispater/CLIProxyAPI/internal/interfaces"
"github.com/luispater/CLIProxyAPI/internal/logging" "github.com/luispater/CLIProxyAPI/internal/logging"
"github.com/luispater/CLIProxyAPI/internal/util"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
@@ -305,11 +306,7 @@ func (s *Server) UpdateClients(clients map[string]interfaces.Client, cfg *config
// Update log level dynamically when debug flag changes // Update log level dynamically when debug flag changes
if s.cfg.Debug != cfg.Debug { if s.cfg.Debug != cfg.Debug {
if cfg.Debug { util.SetLogLevel(cfg)
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Debugf("debug mode updated from %t to %t", s.cfg.Debug, cfg.Debug) log.Debugf("debug mode updated from %t to %t", s.cfg.Debug, cfg.Debug)
} }

View File

@@ -1,6 +1,6 @@
// Package util provides utility functions for the CLI Proxy API server. // Package util provides utility functions for the CLI Proxy API server.
// It includes helper functions for proxy configuration, HTTP client setup, // It includes helper functions for proxy configuration, HTTP client setup,
// and other common operations used across the application. // log level management, and other common operations used across the application.
package util package util
import ( import (

23
internal/util/util.go Normal file
View File

@@ -0,0 +1,23 @@
package util
import (
"github.com/luispater/CLIProxyAPI/internal/config"
log "github.com/sirupsen/logrus"
)
// SetLogLevel configures the logrus log level based on the configuration.
// It sets the log level to DebugLevel if debug mode is enabled, otherwise to InfoLevel.
func SetLogLevel(cfg *config.Config) {
currentLevel := log.GetLevel()
var newLevel log.Level
if cfg.Debug {
newLevel = log.DebugLevel
} else {
newLevel = log.InfoLevel
}
if currentLevel != newLevel {
log.SetLevel(newLevel)
log.Infof("log level changed from %s to %s (debug=%t)", currentLevel, newLevel, cfg.Debug)
}
}

View File

@@ -168,6 +168,14 @@ func (w *Watcher) reloadConfig() {
w.config = newConfig w.config = newConfig
w.clientsMutex.Unlock() w.clientsMutex.Unlock()
// Always apply the current log level based on the latest config.
// This ensures logrus reflects the desired level even if change detection misses.
util.SetLogLevel(newConfig)
// Additional debug for visibility when the flag actually changes.
if oldConfig != nil && oldConfig.Debug != newConfig.Debug {
log.Debugf("log level updated - debug mode changed from %t to %t", oldConfig.Debug, newConfig.Debug)
}
// Log configuration changes in debug mode // Log configuration changes in debug mode
if oldConfig != nil { if oldConfig != nil {
log.Debugf("config changes detected:") log.Debugf("config changes detected:")