优化部分逻辑
This commit is contained in:
@@ -66,8 +66,14 @@ type User struct {
|
||||
// ============================================================
|
||||
|
||||
func InitDB() error {
|
||||
// 确保 data 目录存在
|
||||
if err := os.MkdirAll("./data", 0755); err != nil {
|
||||
return fmt.Errorf("创建数据目录失败: %w", err)
|
||||
}
|
||||
|
||||
dbPath := "./data/frpc-console.db"
|
||||
var err error
|
||||
DB, err = sql.Open("sqlite", "./frpc-console.db")
|
||||
DB, err = sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -210,13 +216,13 @@ func setSchemaVersion(version string) error {
|
||||
}
|
||||
|
||||
func backupDatabase() (string, error) {
|
||||
src := "./frpc-console.db"
|
||||
src := "./data/frpc-console.db"
|
||||
if _, err := os.Stat(src); os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
timestamp := time.Now().Format("20060102_150405")
|
||||
dst := fmt.Sprintf("./frpc-console.db.pre-%s.%s", SchemaVersion, timestamp)
|
||||
dst := fmt.Sprintf("./data/frpc-console.db.pre-%s.%s", SchemaVersion, timestamp)
|
||||
|
||||
srcFile, err := os.Open(src)
|
||||
if err != nil {
|
||||
@@ -245,7 +251,7 @@ func restoreDatabase(backupPath string) error {
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
dstFile, err := os.Create("./frpc-console.db")
|
||||
dstFile, err := os.Create("./data/frpc-console.db")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user