refactor: extract path logic to path.go and run go fmt on codebase

This commit is contained in:
duorameng
2026-05-22 14:52:51 +08:00
parent b6fd889661
commit 4c283a60d5
55 changed files with 409 additions and 420 deletions
+1 -76
View File
@@ -1,81 +1,6 @@
package constant package constant
import ( import "time"
"os"
"path/filepath"
"time"
)
var (
// ConfigPath 配置文件路径
ConfigPath string
// DataDir 数据目录
DataDir string
// DefaultDBPath 默认数据库路径
DefaultDBPath string
// WebDistDir 前端构建目录
WebDistDir string
// ScriptsWorkDir 脚本工作目录
ScriptsWorkDir string
)
func init() {
rootDir := ResolveAppRootDir()
ConfigPath = filepath.Clean(filepath.Join(rootDir, "configs", "config.ini"))
DataDir = filepath.Clean(filepath.Join(rootDir, "data"))
DefaultDBPath = filepath.Clean(filepath.Join(rootDir, "data", "baihu.db"))
WebDistDir = filepath.Clean(filepath.Join(rootDir, "web", "dist"))
ScriptsWorkDir = filepath.Clean(filepath.Join(rootDir, "data", "scripts"))
}
// ResolveAppRootDir 获取应用程序的绝对根目录路径。
func ResolveAppRootDir() string {
// 1. 检查当前工作目录(CWD)及其上级目录
if cwd, err := os.Getwd(); err == nil {
dir := cwd
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 2. 检查当前可执行文件路径及其上级目录
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 3. 兜底回退到当前工作目录
if cwd, err := os.Getwd(); err == nil {
return cwd
}
return "."
}
const ( const (
+77
View File
@@ -0,0 +1,77 @@
package constant
import (
"os"
"path/filepath"
)
var (
// ConfigPath 配置文件路径
ConfigPath string
// DataDir 数据目录
DataDir string
// DefaultDBPath 默认数据库路径
DefaultDBPath string
// WebDistDir 前端构建目录
WebDistDir string
// ScriptsWorkDir 脚本工作目录
ScriptsWorkDir string
)
func init() {
rootDir := ResolveAppRootDir()
ConfigPath = filepath.Clean(filepath.Join(rootDir, "configs", "config.ini"))
DataDir = filepath.Clean(filepath.Join(rootDir, "data"))
DefaultDBPath = filepath.Clean(filepath.Join(rootDir, "data", "baihu.db"))
WebDistDir = filepath.Clean(filepath.Join(rootDir, "web", "dist"))
ScriptsWorkDir = filepath.Clean(filepath.Join(rootDir, "data", "scripts"))
}
// ResolveAppRootDir 获取应用程序的绝对根目录路径。
func ResolveAppRootDir() string {
// 1. 检查当前工作目录(CWD)及其上级目录
if cwd, err := os.Getwd(); err == nil {
dir := cwd
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 2. 检查当前可执行文件路径及其上级目录
if exe, err := os.Executable(); err == nil {
dir := filepath.Dir(exe)
for {
if _, err := os.Stat(filepath.Join(dir, "configs", "config.ini")); err == nil {
return dir
}
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return dir
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
}
// 3. 兜底回退到当前工作目录
if cwd, err := os.Getwd(); err == nil {
return cwd
}
return "."
}
-1
View File
@@ -1,7 +1,6 @@
package controllers package controllers
import ( import (
"strconv" "strconv"
"sync" "sync"
"time" "time"
-1
View File
@@ -1,7 +1,6 @@
package controllers package controllers
import ( import (
"github.com/engigu/baihu-panel/internal/database" "github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/models" "github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/models/vo" "github.com/engigu/baihu-panel/internal/models/vo"
+1
View File
@@ -76,6 +76,7 @@ func (c *MiseController) VerifyCommand(ctx *gin.Context) {
} }
utils.Success(ctx, gin.H{"command": cmd}) utils.Success(ctx, gin.H{"command": cmd})
} }
// UseGlobal 设置全局默认版本 // UseGlobal 设置全局默认版本
func (c *MiseController) UseGlobal(ctx *gin.Context) { func (c *MiseController) UseGlobal(ctx *gin.Context) {
var req struct { var req struct {
@@ -1,7 +1,6 @@
package controllers package controllers
import ( import (
"github.com/engigu/baihu-panel/internal/models/vo" "github.com/engigu/baihu-panel/internal/models/vo"
"github.com/engigu/baihu-panel/internal/services" "github.com/engigu/baihu-panel/internal/services"
"github.com/engigu/baihu-panel/internal/utils" "github.com/engigu/baihu-panel/internal/utils"
+1 -2
View File
@@ -6,12 +6,12 @@ import (
"strings" "strings"
"github.com/engigu/baihu-panel/internal/constant" "github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models" "github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/models/vo" "github.com/engigu/baihu-panel/internal/models/vo"
"github.com/engigu/baihu-panel/internal/services" "github.com/engigu/baihu-panel/internal/services"
"github.com/engigu/baihu-panel/internal/services/tasks" "github.com/engigu/baihu-panel/internal/services/tasks"
"github.com/engigu/baihu-panel/internal/utils" "github.com/engigu/baihu-panel/internal/utils"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"os" "os"
@@ -695,4 +695,3 @@ func (tc *TaskController) ToggleTask(c *gin.Context) {
utils.Success(c, vo.ToTaskVO(updatedTask)) utils.Success(c, vo.ToTaskVO(updatedTask))
} }
+1 -2
View File
@@ -7,9 +7,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"sync" "sync"
"time" "time"
"strings"
"unicode/utf8" "unicode/utf8"
"github.com/engigu/baihu-panel/internal/constant" "github.com/engigu/baihu-panel/internal/constant"
@@ -23,7 +23,6 @@ import (
"golang.org/x/text/transform" "golang.org/x/text/transform"
) )
type TerminalController struct { type TerminalController struct {
envService *services.EnvService envService *services.EnvService
} }
-1
View File
@@ -95,7 +95,6 @@ func getModelSignature(models []interface{}) string {
return hex.EncodeToString(hash[:]) return hex.EncodeToString(hash[:])
} }
// customMigrations 自定义迁移(处理 AutoMigrate 无法自动完成的变更) // customMigrations 自定义迁移(处理 AutoMigrate 无法自动完成的变更)
func customMigrations() error { func customMigrations() error {
// 检查 ql_tokens 表是否存在 // 检查 ql_tokens 表是否存在
+1 -2
View File
@@ -1,9 +1,9 @@
package executor package executor
import ( import (
"fmt"
"math/rand" "math/rand"
"strings" "strings"
"fmt"
"sync" "sync"
"time" "time"
@@ -71,7 +71,6 @@ func (m *CronManager) Stop() {
m.logger.Infof("[CronManager] 调度管理服务已停止") m.logger.Infof("[CronManager] 调度管理服务已停止")
} }
// AddTask 添加或更新计划任务 // AddTask 添加或更新计划任务
func (m *CronManager) AddTask(task CronTask) error { func (m *CronManager) AddTask(task CronTask) error {
m.mu.Lock() m.mu.Lock()
-1
View File
@@ -16,7 +16,6 @@ type EnvironmentVariable struct {
UserID string `json:"user_id" gorm:"size:20;index"` UserID string `json:"user_id" gorm:"size:20;index"`
CreatedAt LocalTime `json:"created_at"` CreatedAt LocalTime `json:"created_at"`
UpdatedAt LocalTime `json:"updated_at"` UpdatedAt LocalTime `json:"updated_at"`
} }
func (EnvironmentVariable) TableName() string { func (EnvironmentVariable) TableName() string {
-1
View File
@@ -20,7 +20,6 @@ func setupEventHandlers(subscribers ...eventbus.Subscriber) {
} }
} }
func startAppLogCleanup(appLogSvc *services.AppLogService) { func startAppLogCleanup(appLogSvc *services.AppLogService) {
// 初始化时执行一次清理 // 初始化时执行一次清理
appLogSvc.CleanUp() appLogSvc.CleanUp()
+1 -1
View File
@@ -1,8 +1,8 @@
package router package router
import ( import (
"strings"
"os" "os"
"strings"
"github.com/engigu/baihu-panel/internal/controllers" "github.com/engigu/baihu-panel/internal/controllers"
"github.com/engigu/baihu-panel/internal/middleware" "github.com/engigu/baihu-panel/internal/middleware"
-1
View File
@@ -132,7 +132,6 @@ func initPWARoutes(root *gin.RouterGroup) {
}) })
} }
func handleManifest(ctx *gin.Context) { func handleManifest(ctx *gin.Context) {
staticFS := static.GetFS() staticFS := static.GetFS()
if staticFS == nil { if staticFS == nil {
+1 -2
View File
@@ -12,8 +12,8 @@ import (
"github.com/engigu/baihu-panel/internal/constant" "github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/database" "github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/executor" "github.com/engigu/baihu-panel/internal/executor"
"github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models" "github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/services/tasks" "github.com/engigu/baihu-panel/internal/services/tasks"
"github.com/engigu/baihu-panel/internal/utils" "github.com/engigu/baihu-panel/internal/utils"
@@ -378,7 +378,6 @@ func (s *AgentService) GetTasks(agentID string) []models.AgentTask {
return result return result
} }
// ReportResult Agent 上报执行结果 // ReportResult Agent 上报执行结果
func (s *AgentService) ReportResult(result *models.AgentTaskResult) error { func (s *AgentService) ReportResult(result *models.AgentTaskResult) error {
// 获取依赖的服务 // 获取依赖的服务
-1
View File
@@ -372,7 +372,6 @@ func (s *BackupService) restoreScriptsDir(r *zip.ReadCloser) {
} }
} }
func (s *BackupService) addDirToZip(zipWriter *zip.Writer, srcDir, prefix string) error { func (s *BackupService) addDirToZip(zipWriter *zip.Writer, srcDir, prefix string) error {
return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error { return filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
-2
View File
@@ -160,8 +160,6 @@ func LoadConfig(path string) (*AppConfig, error) {
return Config, nil return Config, nil
} }
// applyEnvOverrides 从环境变量加载配置 // applyEnvOverrides 从环境变量加载配置
func applyEnvOverrides() { func applyEnvOverrides() {
// Server // Server
+2 -1
View File
@@ -109,7 +109,6 @@ func (s *MiseService) fetchLiveLanguages() ([]MiseLanguage, error) {
return s.listFallback() return s.listFallback()
} }
func (s *MiseService) listFallback() ([]MiseLanguage, error) { func (s *MiseService) listFallback() ([]MiseLanguage, error) {
cmd := exec.Command("mise", "ls") cmd := exec.Command("mise", "ls")
cmd.Env = os.Environ() cmd.Env = os.Environ()
@@ -329,6 +328,7 @@ func (s *MiseService) GetVerifyCommand(plugin, version string) (string, error) {
} }
return m.GetVerifyCommand(version) return m.GetVerifyCommand(version)
} }
// UseGlobal 设置全局默认版本 // UseGlobal 设置全局默认版本
func (s *MiseService) UseGlobal(plugin, version string) error { func (s *MiseService) UseGlobal(plugin, version string) error {
cmd := exec.Command("mise", "use", "-g", fmt.Sprintf("%s@%s", plugin, version)) cmd := exec.Command("mise", "use", "-g", fmt.Sprintf("%s@%s", plugin, version))
@@ -355,6 +355,7 @@ func (s *MiseService) UnsetGlobal(plugin, version string) error {
} }
return nil return nil
} }
// Envs 获取全局环境变量 // Envs 获取全局环境变量
func (s *MiseService) Envs() (map[string]string, error) { func (s *MiseService) Envs() (map[string]string, error) {
cmd := exec.Command("mise", "set") cmd := exec.Command("mise", "set")
+1 -1
View File
@@ -1,9 +1,9 @@
package repo package repo
import ( import (
"github.com/engigu/baihu-panel/internal/models"
"regexp" "regexp"
"strings" "strings"
"github.com/engigu/baihu-panel/internal/models"
) )
// QinglongStrategy 实现与青龙兼容的解析逻辑 // QinglongStrategy 实现与青龙兼容的解析逻辑
+2 -2
View File
@@ -3,12 +3,12 @@ package repo
import ( import (
"bufio" "bufio"
"fmt" "fmt"
"github.com/engigu/baihu-panel/internal/utils"
"github.com/robfig/cron/v3"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"github.com/engigu/baihu-panel/internal/utils"
"github.com/robfig/cron/v3"
) )
var ( var (
+1 -1
View File
@@ -4,11 +4,11 @@ import (
"encoding/json" "encoding/json"
"time" "time"
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/database" "github.com/engigu/baihu-panel/internal/database"
"github.com/engigu/baihu-panel/internal/logger" "github.com/engigu/baihu-panel/internal/logger"
"github.com/engigu/baihu-panel/internal/models" "github.com/engigu/baihu-panel/internal/models"
"github.com/engigu/baihu-panel/internal/systime" "github.com/engigu/baihu-panel/internal/systime"
"github.com/engigu/baihu-panel/internal/constant"
"github.com/engigu/baihu-panel/internal/utils" "github.com/engigu/baihu-panel/internal/utils"
) )
-1
View File
@@ -92,7 +92,6 @@ func (ts *TaskService) CreateTask(p *TaskParam) *models.Task {
} }
database.DB.Select("*").Create(task) database.DB.Select("*").Create(task)
return task return task
} }
+1
View File
@@ -8,6 +8,7 @@ import (
func GenerateID() string { func GenerateID() string {
return xid.New().String() return xid.New().String()
} }
// IsNumeric 检查字符串是否全为数字 // IsNumeric 检查字符串是否全为数字
func IsNumeric(s string) bool { func IsNumeric(s string) bool {
for _, c := range s { for _, c := range s {
-1
View File
@@ -14,7 +14,6 @@ var (
shellOnce sync.Once shellOnce sync.Once
) )
// GetShell 返回当前操作系统的 shell 和参数 // GetShell 返回当前操作系统的 shell 和参数
func GetShell() (shell string, args []string) { func GetShell() (shell string, args []string) {
shellOnce.Do(func() { shellOnce.Do(func() {