mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-09-22 04:43:00 +00:00
+ feature: viper reads the environment variable GIN_MODE that comes with gin, which is used to distinguish the three environments of development, testing and production #1098.
This commit is contained in:
@@ -104,7 +104,8 @@ db-list:
|
||||
|
||||
# local configuration
|
||||
local:
|
||||
path: 'uploads/file'
|
||||
path: 'uploads/file' # 访问路径
|
||||
store-path: 'uploads/file' # 存储路径
|
||||
|
||||
# autocode configuration
|
||||
autocode:
|
||||
|
||||
+2
-1
@@ -102,7 +102,8 @@ db-list:
|
||||
|
||||
# local configuration
|
||||
local:
|
||||
path: 'uploads/file'
|
||||
path: 'uploads/file' # 访问路径
|
||||
store-path: 'uploads/file' # 存储路径
|
||||
|
||||
# autocode configuration
|
||||
autocode:
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package config
|
||||
|
||||
type Local struct {
|
||||
Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径
|
||||
Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件访问路径
|
||||
StorePath string `mapstructure:"store-path" json:"store-path" yaml:"store-path"` // 本地文件存储路径
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package internal
|
||||
|
||||
const (
|
||||
ConfigEnv = "GVA_CONFIG"
|
||||
ConfigDefaultFile = "config.yaml"
|
||||
ConfigTestFile = "config.test.yaml"
|
||||
ConfigDebugFile = "config.debug.yaml"
|
||||
ConfigReleaseFile = "config.release.yaml"
|
||||
)
|
||||
+29
-16
@@ -3,6 +3,8 @@ package core
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/core/internal"
|
||||
"github.com/gin-gonic/gin"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
@@ -11,31 +13,42 @@ import (
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
_ "github.com/flipped-aurora/gin-vue-admin/server/packfile"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
||||
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Viper //
|
||||
// 优先级: 命令行 > 环境变量 > 默认值
|
||||
// Author [SliverHorn](https://github.com/SliverHorn)
|
||||
func Viper(path ...string) *viper.Viper {
|
||||
var config string
|
||||
|
||||
if len(path) == 0 {
|
||||
flag.StringVar(&config, "c", "", "choose config file.")
|
||||
flag.Parse()
|
||||
if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
|
||||
if configEnv := os.Getenv(utils.ConfigEnv); configEnv == "" {
|
||||
config = utils.ConfigFile
|
||||
fmt.Printf("您正在使用config的默认值,config的路径为%v\n", utils.ConfigFile)
|
||||
} else {
|
||||
if config == "" { // 判断命令行参数是否为空
|
||||
if configEnv := os.Getenv(internal.ConfigEnv); configEnv == "" { // 判断 internal.ConfigEnv 常量存储的环境变量是否为空
|
||||
switch gin.Mode() {
|
||||
case gin.DebugMode:
|
||||
config = internal.ConfigDefaultFile
|
||||
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigDefaultFile)
|
||||
case gin.ReleaseMode:
|
||||
config = internal.ConfigReleaseFile
|
||||
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigReleaseFile)
|
||||
case gin.TestMode:
|
||||
config = internal.ConfigTestFile
|
||||
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigTestFile)
|
||||
}
|
||||
} else { // internal.ConfigEnv 常量存储的环境变量不为空 将值赋值于config
|
||||
config = configEnv
|
||||
fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config)
|
||||
fmt.Printf("您正在使用%s环境变量,config的路径为%s\n", internal.ConfigEnv, config)
|
||||
}
|
||||
} else {
|
||||
fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%v\n", config)
|
||||
} else { // 命令行参数不为空 将值赋值于config
|
||||
fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%s\n", config)
|
||||
}
|
||||
} else {
|
||||
} else { // 函数传递的可变参数的第一个值赋值于config
|
||||
config = path[0]
|
||||
fmt.Printf("您正在使用func Viper()传递的值,config的路径为%v\n", config)
|
||||
fmt.Printf("您正在使用func Viper()传递的值,config的路径为%s\n", config)
|
||||
}
|
||||
|
||||
v := viper.New()
|
||||
@@ -49,15 +62,15 @@ func Viper(path ...string) *viper.Viper {
|
||||
|
||||
v.OnConfigChange(func(e fsnotify.Event) {
|
||||
fmt.Println("config file changed:", e.Name)
|
||||
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
|
||||
if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
})
|
||||
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
|
||||
if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
// root 适配性
|
||||
// 根据root位置去找到对应迁移位置,保证root路径有效
|
||||
|
||||
// root 适配性 根据root位置去找到对应迁移位置,保证root路径有效
|
||||
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
|
||||
global.BlackCache = local_cache.NewCache(
|
||||
local_cache.SetDefaultExpire(time.Second * time.Duration(global.GVA_CONFIG.JWT.ExpiresTime)),
|
||||
|
||||
@@ -27,7 +27,7 @@ func Routers() *gin.Engine {
|
||||
// Router.Static("/static", "./dist/assets") // dist里面的静态资源
|
||||
// Router.StaticFile("/", "./dist/index.html") // 前端网页入口页面
|
||||
|
||||
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.Path)) // 为用户头像和文件提供静态地址
|
||||
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.StorePath)) // 为用户头像和文件提供静态地址
|
||||
// Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
|
||||
global.GVA_LOG.Info("use middleware logger")
|
||||
// 跨域,如需跨域可以打开下面的注释
|
||||
@@ -75,7 +75,7 @@ func Routers() *gin.Engine {
|
||||
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
|
||||
|
||||
// Code generated by github.com/flipped-aurora/gin-vue-admin/server Begin; DO NOT EDIT.
|
||||
|
||||
|
||||
// Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT.
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package utils
|
||||
|
||||
const (
|
||||
ConfigEnv = "GVA_CONFIG"
|
||||
ConfigFile = "config.yaml"
|
||||
)
|
||||
@@ -34,13 +34,14 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
|
||||
// 拼接新文件名
|
||||
filename := name + "_" + time.Now().Format("20060102150405") + ext
|
||||
// 尝试创建此路径
|
||||
mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.Path, os.ModePerm)
|
||||
mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.StorePath, os.ModePerm)
|
||||
if mkdirErr != nil {
|
||||
global.GVA_LOG.Error("function os.MkdirAll() Filed", zap.Any("err", mkdirErr.Error()))
|
||||
return "", "", errors.New("function os.MkdirAll() Filed, err:" + mkdirErr.Error())
|
||||
}
|
||||
// 拼接路径和文件名
|
||||
p := global.GVA_CONFIG.Local.Path + "/" + filename
|
||||
p := global.GVA_CONFIG.Local.StorePath + "/" + filename
|
||||
filepath := global.GVA_CONFIG.Local.Path + "/" + filename
|
||||
|
||||
f, openError := file.Open() // 读取文件
|
||||
if openError != nil {
|
||||
@@ -62,7 +63,7 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
|
||||
global.GVA_LOG.Error("function io.Copy() Filed", zap.Any("err", copyErr.Error()))
|
||||
return "", "", errors.New("function io.Copy() Filed, err:" + copyErr.Error())
|
||||
}
|
||||
return p, filename, nil
|
||||
return filepath, filename, nil
|
||||
}
|
||||
|
||||
//@author: [piexlmax](https://github.com/piexlmax)
|
||||
@@ -75,8 +76,8 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
|
||||
//@return: error
|
||||
|
||||
func (*Local) DeleteFile(key string) error {
|
||||
p := global.GVA_CONFIG.Local.Path + "/" + key
|
||||
if strings.Contains(p, global.GVA_CONFIG.Local.Path) {
|
||||
p := global.GVA_CONFIG.Local.StorePath + "/" + key
|
||||
if strings.Contains(p, global.GVA_CONFIG.Local.StorePath) {
|
||||
if err := os.Remove(p); err != nil {
|
||||
return errors.New("本地文件删除失败, err:" + err.Error())
|
||||
}
|
||||
|
||||
@@ -220,7 +220,7 @@ const removeTab = (tab) => {
|
||||
const index = historys.value.findIndex(
|
||||
(item) => getFmtString(item) === tab
|
||||
)
|
||||
if (getFmtString(route) === tab) {
|
||||
if (getFmtString(route) === tab) {
|
||||
if (historys.value.length === 1) {
|
||||
router.push({ name: defaultRouter.value })
|
||||
} else {
|
||||
@@ -264,6 +264,12 @@ watch(route, (to, now) => {
|
||||
activeValue.value = window.sessionStorage.getItem('activeValue')
|
||||
})
|
||||
|
||||
watch(() => historys.value, () => {
|
||||
sessionStorage.setItem('historys', JSON.stringify(historys.value))
|
||||
}, {
|
||||
deep: true
|
||||
})
|
||||
|
||||
const initPage = () => {
|
||||
// 全局监听 关闭当前页面函数
|
||||
emitter.on('closeThisPage', () => {
|
||||
|
||||
Reference in New Issue
Block a user