fix(build): 独立二进制输出到根目录并修复 local 模式 SQLite 路径

- SQLite/redka 数据目录相对可执行文件解析,自动创建 data/ 父目录
- ./run.sh build 产物改为 ./aiops(与 run.sh 同级),数据/日志在 ./data ./logs
- 解决从项目根执行时 data/ 不存在导致的 SQLite CANTOPEN 报错
This commit is contained in:
Logan.Li committed 2026-06-26 16:16:02 +08:00
1 parent fd7b04b9d6
commit e7a479ccf5
5 files changed
+88 -31

No files matched your search

+9 -1
View File
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
@@ -14,6 +15,7 @@ import (
"ezops/router"
"ezops/config"
"ezops/seed"
"ezops/pkg/appdir"
"ezops/pkg/database"
"ezops/pkg/logger"
ezredis "ezops/pkg/redis"
@@ -136,7 +138,7 @@ func main() {
Port: cfg.Redis.Port,
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
DataDir: "data/redka",
DataDir: appdir.Resolve("data/redka"),
})
if err != nil {
if cfg.Mode == "local" {
@@ -237,6 +239,7 @@ func loadConfig() *config.Config {
viper.SetConfigType("yaml")
viper.AddConfigPath("./configs")
viper.AddConfigPath(".")
viper.AddConfigPath(appdir.Root())
// 如果指定了配置文件路径,使用它 // Logan.Li
if *flagConfig != "" {
@@ -295,6 +298,11 @@ func loadConfig() *config.Config {
os.Exit(1)
}
// SQLite / redka 相对路径统一解析到可执行文件目录 // Logan.Li
if cfg.Database.Driver == "sqlite" && cfg.Database.DBName != "" && !filepath.IsAbs(cfg.Database.DBName) {
cfg.Database.DBName = appdir.Resolve(cfg.Database.DBName)
}
return &cfg
}
+36
View File
@@ -0,0 +1,36 @@
// appdir — 独立二进制运行目录(数据/配置相对可执行文件,不依赖 cwd)// Logan.Li
package appdir
import (
"os"
"path/filepath"
)
// Root 可执行文件所在目录(解析软链)// Logan.Li
func Root() string {
exe, err := os.Executable()
if err != nil {
return "."
}
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
exe = resolved
}
return filepath.Dir(exe)
}
// Resolve 相对路径 → 基于可执行文件目录的绝对路径 // Logan.Li
func Resolve(path string) string {
if path == "" || filepath.IsAbs(path) {
return path
}
return filepath.Join(Root(), path)
}
// EnsureParentDir 确保文件父目录存在 // Logan.Li
func EnsureParentDir(filePath string) error {
dir := filepath.Dir(filePath)
if dir == "" || dir == "." {
return nil
}
return os.MkdirAll(dir, 0o755)
}
+5 -1
View File
@@ -7,6 +7,7 @@ import (
"ezops/config"
"ezops/model"
"ezops/pkg/appdir"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
@@ -29,7 +30,10 @@ func New(cfg config.DatabaseConfig) (*gorm.DB, error) {
cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName)
dialector = postgres.Open(dsn)
case "sqlite":
dsn := cfg.DBName
dsn := appdir.Resolve(cfg.DBName)
if err := appdir.EnsureParentDir(dsn); err != nil {
return nil, fmt.Errorf("创建数据库目录失败: %w", err)
}
if !strings.Contains(dsn, "?") {
// WAL 提升并发读,busy_timeout 降低锁冲突 // Logan.Li
dsn += "?_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"