refactor: standardize constant naming and improve file-based auth handling

- Renamed constants from uppercase to CamelCase for consistency.
- Replaced redundant file-based auth handling logic with the new `util.CountAuthFiles` helper.
- Fixed various error-handling inconsistencies and enhanced robustness in file operations.
- Streamlined auth client reload logic in server and watcher components.
- Applied minor code readability improvements across multiple packages.
This commit is contained in:
Luis Pater
2025-09-22 02:56:45 +08:00
parent 4999fce7f4
commit d9ad65622a
51 changed files with 341 additions and 270 deletions

View File

@@ -289,9 +289,6 @@ func sanitizeTypeFields(jsonStr string) string {
break
} else if typeStr == "number" || typeStr == "integer" {
preferredType = typeStr
if preferredType == "" {
preferredType = typeStr
}
} else if preferredType == "" {
preferredType = typeStr
}
@@ -323,6 +320,8 @@ func walkForTypeFields(value gjson.Result, path string, paths *[]string) {
walkForTypeFields(val, childPath, paths)
return true
})
default:
}
}
@@ -367,5 +366,7 @@ func findNestedSchemaPaths(value gjson.Result, path string, fieldsToFind []strin
findNestedSchemaPaths(val, childPath, fieldsToFind, paths)
return true
})
default:
}
}

View File

@@ -1,6 +1,11 @@
package util
import (
"io/fs"
"os"
"path/filepath"
"strings"
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
log "github.com/sirupsen/logrus"
)
@@ -21,3 +26,38 @@ func SetLogLevel(cfg *config.Config) {
log.Infof("log level changed from %s to %s (debug=%t)", currentLevel, newLevel, cfg.Debug)
}
}
// CountAuthFiles returns the number of JSON auth files located under the provided directory.
// The function resolves leading tildes to the user's home directory and performs a case-insensitive
// match on the ".json" suffix so that files saved with uppercase extensions are also counted.
func CountAuthFiles(authDir string) int {
if authDir == "" {
return 0
}
if strings.HasPrefix(authDir, "~") {
home, err := os.UserHomeDir()
if err != nil {
log.Debugf("countAuthFiles: failed to resolve home directory: %v", err)
return 0
}
authDir = filepath.Join(home, authDir[1:])
}
count := 0
walkErr := filepath.WalkDir(authDir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
log.Debugf("countAuthFiles: error accessing %s: %v", path, err)
return nil
}
if d.IsDir() {
return nil
}
if strings.HasSuffix(strings.ToLower(d.Name()), ".json") {
count++
}
return nil
})
if walkErr != nil {
log.Debugf("countAuthFiles: walk error: %v", walkErr)
}
return count
}