From 0ec1b163ed54bdf61d4e953e9fca894e8bd9b71a Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期四, 30 七月 2020 10:51:42 +0800
Subject: [PATCH] dbPath use rootPath

---
 conf/app.conf       |    2 +-
 routers/router.go   |   21 +++------------------
 extend/util/util.go |   16 ++++++++++++++++
 models/db.go        |    6 ++++--
 4 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/conf/app.conf b/conf/app.conf
index 15218ef..58fb34c 100644
--- a/conf/app.conf
+++ b/conf/app.conf
@@ -16,5 +16,5 @@
 pushMasterSecret = vFiErCTBqqAcufPzB0Tl45
 pushPackageName = uni.UNIEDF0B5C
 pushBaseUrl = https://restapi.getui.com/v2/
-dbUrl = ./conf/app.db
+dbUrl = conf/app.db
 hikUrl = https://111.205.161.131:1443
\ No newline at end of file
diff --git a/extend/util/util.go b/extend/util/util.go
index 5e2b118..3887458 100644
--- a/extend/util/util.go
+++ b/extend/util/util.go
@@ -8,6 +8,9 @@
 	"encoding/hex"
 	"fmt"
 	"math/rand"
+	"os"
+	"os/exec"
+	"path/filepath"
 	"strconv"
 	"strings"
 	"time"
@@ -82,4 +85,17 @@
 		return max
 	}
 	return rand.Intn(max-min) + min
+}
+
+
+func GetAppRootPath() string {
+	file,err:= exec.LookPath(os.Args[0])
+	if err != nil {
+		return ""
+	}
+	p, err := filepath.Abs(file)
+	if err != nil {
+		return ""
+	}
+	return filepath.Dir(p)
 }
\ No newline at end of file
diff --git a/models/db.go b/models/db.go
index 48345c6..0ceaf78 100644
--- a/models/db.go
+++ b/models/db.go
@@ -1,6 +1,7 @@
 package models
 
 import (
+	"car-service/extend/util"
 	"github.com/astaxie/beego"
 	"github.com/astaxie/beego/orm"
 	_ "github.com/mattn/go-sqlite3"
@@ -8,9 +9,10 @@
 
 func InitDb() {
 	dbUrl := beego.AppConfig.String("dbUrl")
-
+	rootPath := util.GetAppRootPath()
+	dbPath := rootPath + dbUrl
 	orm.RegisterDriver("sqlite", orm.DRSqlite)
-	_ = orm.RegisterDataBase("default", "sqlite3", dbUrl)
+	_ = orm.RegisterDataBase("default", "sqlite3", dbPath)
 	orm.RegisterModel(new(User), new(UserCar), new(SysUpgrade))
 	_ = orm.RunSyncdb("default", false, true)
 }
diff --git a/routers/router.go b/routers/router.go
index 0a88743..172f308 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -9,13 +9,10 @@
 
 import (
 	"car-service/controllers"
+	"car-service/extend/util"
 	"fmt"
-	"github.com/astaxie/beego/plugins/cors"
-	"os"
-	"os/exec"
-	"path/filepath"
-
 	"github.com/astaxie/beego"
+	"github.com/astaxie/beego/plugins/cors"
 )
 
 func init() {
@@ -43,7 +40,7 @@
 	//)
 	//beego.AddNamespace(ns)
 	//beego.Router("/basic/api/car/app/user", &controllers.UserController{})
-	rootPath := getAppRootPath()
+	rootPath := util.GetAppRootPath()
 	fmt.Println("rootPath:", rootPath)
 	beego.SetStaticPath("/download", rootPath+"/download")
 	preApi := "/basic/api"
@@ -60,15 +57,3 @@
 
 	beego.Router(preApi+"/user/all", &controllers.UserController{}, "*:GetUserAll")
 }
-
-func getAppRootPath() string {
-	file,err:= exec.LookPath(os.Args[0])
-	if err != nil {
-		return ""
-	}
-	p, err := filepath.Abs(file)
-	if err != nil {
-		return ""
-	}
-	return filepath.Dir(p)
-}
\ No newline at end of file

--
Gitblit v1.8.0