Эх сурвалжийг харах

feat: 增加sqlite 支持

rikugun 4 жил өмнө
parent
commit
8c62ee93a1

+ 2 - 0
.gitignore

@@ -25,3 +25,5 @@ yarn-error.log*
 go.sum
 /server/log/
 /server/latest_log
+
+*.iml

+ 10 - 0
server/config/config.go

@@ -2,6 +2,7 @@ package config
 
 type Server struct {
 	Mysql   Mysql   `mapstructure:"mysql" json:"mysql"`
+	Sqlite   Sqlite   `mapstructure:"sqlite" json:"sqlite"`
 	Qiniu   Qiniu   `mapstructure:"qiniu" json:"qiniu"`
 	Casbin  Casbin  `mapstructure:"casbin" json:"casbin"`
 	Redis   Redis   `mapstructure:"redis" json:"redis"`
@@ -15,6 +16,7 @@ type System struct {
 	UseMultipoint bool   `mapstructure:"use-multipoint" json:"useMultipoint"`
 	Env           string `mapstructure:"env" json:"env"`
 	Addr          int    `mapstructure:"addr" json:"addr"`
+	Db            string    `mapstructure:"db" json:"db"`
 }
 
 type JWT struct {
@@ -58,3 +60,11 @@ type Log struct {
 	Stdout  string `mapstructure:"stdout" json:"stdout"`
 	File    string `mapstructure:"file" json:"file"`
 }
+
+type Sqlite struct {
+	Username     string `mapstructure:"username" json:"username"`
+	Password     string `mapstructure:"password" json:"password"`
+	Path         string `mapstructure:"path" json:"path"`
+	Config       string `mapstructure:"config" json:"config"`
+	LogMode      bool   `mapstructure:"log-mode" json:"logMode"`
+}

+ 19 - 0
server/initialize/sqlite.go

@@ -0,0 +1,19 @@
+package initialize
+
+import (
+	"fmt"
+	"gin-vue-admin/global"
+	"github.com/jinzhu/gorm"
+	_ "github.com/jinzhu/gorm/dialects/sqlite"
+)
+
+//初始化数据库并产生数据库全局变量
+func Sqlite() {
+	admin := global.GVA_CONFIG.Sqlite
+	if db, err := gorm.Open("sqlite3", fmt.Sprintf("%s?%s", admin.Path,admin.Config)); err != nil {
+		global.GVA_LOG.Error("DEFAULTDB数据库启动异常", err)
+	} else {
+		global.GVA_DB = db
+		global.GVA_DB.LogMode(admin.LogMode)
+	}
+}

+ 6 - 1
server/main.go

@@ -8,7 +8,12 @@ import (
 )
 
 func main() {
-	initialize.Mysql()
+	switch global.GVA_CONFIG.System.Db  {
+	case "mysql":
+		initialize.Mysql()
+	case "sqlite":
+		initialize.Sqlite()
+	}
 	initialize.DBTables()
 	// 程序结束前关闭数据库链接
 	defer global.GVA_DB.Close()

+ 14 - 1
server/utils/directory.go

@@ -1,6 +1,9 @@
 package utils
 
-import "os"
+import (
+	"os"
+	"path/filepath"
+)
 
 // @title    PathExists
 // @description   文件目录是否存在
@@ -45,3 +48,13 @@ func CreateDir(dirs ...string) (err error) {
 	}
 	return err
 }
+// @title cwd
+// @description 获取当前工作目录
+// @return string
+func CWD() string {
+	path, err := os.Executable()
+	if err != nil {
+		return ""
+	}
+	return filepath.Dir(path)
+}