From 8c62ee93a17191a6b9b6f6323e801d8115b5800d Mon Sep 17 00:00:00 2001 From: rikugun Date: Sat, 11 Apr 2020 22:57:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0sqlite=20=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 ++ server/config/config.go | 10 ++++++++++ server/initialize/sqlite.go | 19 +++++++++++++++++++ server/main.go | 7 ++++++- server/utils/directory.go | 15 ++++++++++++++- 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 server/initialize/sqlite.go diff --git a/.gitignore b/.gitignore index 9fb30974..41aca02a 100644 --- a/.gitignore +++ b/.gitignore @@ -25,3 +25,5 @@ yarn-error.log* go.sum /server/log/ /server/latest_log + +*.iml diff --git a/server/config/config.go b/server/config/config.go index c109a366..dec3ca96 100644 --- a/server/config/config.go +++ b/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"` +} diff --git a/server/initialize/sqlite.go b/server/initialize/sqlite.go new file mode 100644 index 00000000..68315ffe --- /dev/null +++ b/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) + } +} diff --git a/server/main.go b/server/main.go index c9892a4d..a574a655 100644 --- a/server/main.go +++ b/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() diff --git a/server/utils/directory.go b/server/utils/directory.go index e252cc75..98128bf7 100644 --- a/server/utils/directory.go +++ b/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) +}