Skip to content

Commit 8ee300f

Browse files
+ 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 flipped-aurora#1098.
2 parents f87eaec + ba98288 commit 8ee300f

File tree

9 files changed

+59
-33
lines changed

9 files changed

+59
-33
lines changed

server/config.docker.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ db-list:
104104

105105
# local configuration
106106
local:
107-
path: 'uploads/file'
107+
path: 'uploads/file' # 访问路径
108+
store-path: 'uploads/file' # 存储路径
108109

109110
# autocode configuration
110111
autocode:

server/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ db-list:
102102

103103
# local configuration
104104
local:
105-
path: 'uploads/file'
105+
path: 'uploads/file' # 访问路径
106+
store-path: 'uploads/file' # 存储路径
106107

107108
# autocode configuration
108109
autocode:

server/config/oss_local.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package config
22

33
type Local struct {
4-
Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件路径
4+
Path string `mapstructure:"path" json:"path" yaml:"path"` // 本地文件访问路径
5+
StorePath string `mapstructure:"store-path" json:"store-path" yaml:"store-path"` // 本地文件存储路径
56
}

server/core/internal/constant.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package internal
2+
3+
const (
4+
ConfigEnv = "GVA_CONFIG"
5+
ConfigDefaultFile = "config.yaml"
6+
ConfigTestFile = "config.test.yaml"
7+
ConfigDebugFile = "config.debug.yaml"
8+
ConfigReleaseFile = "config.release.yaml"
9+
)

server/core/viper.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package core
33
import (
44
"flag"
55
"fmt"
6+
"github.com/flipped-aurora/gin-vue-admin/server/core/internal"
7+
"github.com/gin-gonic/gin"
68
"os"
79
"path/filepath"
810
"time"
@@ -11,31 +13,42 @@ import (
1113

1214
"github.com/flipped-aurora/gin-vue-admin/server/global"
1315
_ "github.com/flipped-aurora/gin-vue-admin/server/packfile"
14-
"github.com/flipped-aurora/gin-vue-admin/server/utils"
15-
1616
"github.com/fsnotify/fsnotify"
1717
"github.com/spf13/viper"
1818
)
1919

20+
// Viper //
21+
// 优先级: 命令行 > 环境变量 > 默认值
22+
// Author [SliverHorn](https://github.com/SliverHorn)
2023
func Viper(path ...string) *viper.Viper {
2124
var config string
25+
2226
if len(path) == 0 {
2327
flag.StringVar(&config, "c", "", "choose config file.")
2428
flag.Parse()
25-
if config == "" { // 优先级: 命令行 > 环境变量 > 默认值
26-
if configEnv := os.Getenv(utils.ConfigEnv); configEnv == "" {
27-
config = utils.ConfigFile
28-
fmt.Printf("您正在使用config的默认值,config的路径为%v\n", utils.ConfigFile)
29-
} else {
29+
if config == "" { // 判断命令行参数是否为空
30+
if configEnv := os.Getenv(internal.ConfigEnv); configEnv == "" { // 判断 internal.ConfigEnv 常量存储的环境变量是否为空
31+
switch gin.Mode() {
32+
case gin.DebugMode:
33+
config = internal.ConfigDefaultFile
34+
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigDefaultFile)
35+
case gin.ReleaseMode:
36+
config = internal.ConfigReleaseFile
37+
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigReleaseFile)
38+
case gin.TestMode:
39+
config = internal.ConfigTestFile
40+
fmt.Printf("您正在使用gin模式的%s环境名称,config的路径为%s\n", gin.EnvGinMode, internal.ConfigTestFile)
41+
}
42+
} else { // internal.ConfigEnv 常量存储的环境变量不为空 将值赋值于config
3043
config = configEnv
31-
fmt.Printf("您正在使用GVA_CONFIG环境变量,config的路径为%v\n", config)
44+
fmt.Printf("您正在使用%s环境变量,config的路径为%s\n", internal.ConfigEnv, config)
3245
}
33-
} else {
34-
fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%v\n", config)
46+
} else { // 命令行参数不为空 将值赋值于config
47+
fmt.Printf("您正在使用命令行的-c参数传递的值,config的路径为%s\n", config)
3548
}
36-
} else {
49+
} else { // 函数传递的可变参数的第一个值赋值于config
3750
config = path[0]
38-
fmt.Printf("您正在使用func Viper()传递的值,config的路径为%v\n", config)
51+
fmt.Printf("您正在使用func Viper()传递的值,config的路径为%s\n", config)
3952
}
4053

4154
v := viper.New()
@@ -49,15 +62,15 @@ func Viper(path ...string) *viper.Viper {
4962

5063
v.OnConfigChange(func(e fsnotify.Event) {
5164
fmt.Println("config file changed:", e.Name)
52-
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
65+
if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
5366
fmt.Println(err)
5467
}
5568
})
56-
if err := v.Unmarshal(&global.GVA_CONFIG); err != nil {
69+
if err = v.Unmarshal(&global.GVA_CONFIG); err != nil {
5770
fmt.Println(err)
5871
}
59-
// root 适配性
60-
// 根据root位置去找到对应迁移位置,保证root路径有效
72+
73+
// root 适配性 根据root位置去找到对应迁移位置,保证root路径有效
6174
global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..")
6275
global.BlackCache = local_cache.NewCache(
6376
local_cache.SetDefaultExpire(time.Second * time.Duration(global.GVA_CONFIG.JWT.ExpiresTime)),

server/initialize/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func Routers() *gin.Engine {
2727
// Router.Static("/static", "./dist/assets") // dist里面的静态资源
2828
// Router.StaticFile("/", "./dist/index.html") // 前端网页入口页面
2929

30-
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.Path)) // 为用户头像和文件提供静态地址
30+
Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.StorePath)) // 为用户头像和文件提供静态地址
3131
// Router.Use(middleware.LoadTls()) // 如果需要使用https 请打开此中间件 然后前往 core/server.go 将启动模式 更变为 Router.RunTLS("端口","你的cre/pem文件","你的key文件")
3232
global.GVA_LOG.Info("use middleware logger")
3333
// 跨域,如需跨域可以打开下面的注释
@@ -75,7 +75,7 @@ func Routers() *gin.Engine {
7575
exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由
7676

7777
// Code generated by github.com/flipped-aurora/gin-vue-admin/server Begin; DO NOT EDIT.
78-
78+
7979
// Code generated by github.com/flipped-aurora/gin-vue-admin/server End; DO NOT EDIT.
8080
}
8181

server/utils/constant.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

server/utils/upload/local.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
3434
// 拼接新文件名
3535
filename := name + "_" + time.Now().Format("20060102150405") + ext
3636
// 尝试创建此路径
37-
mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.Path, os.ModePerm)
37+
mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.StorePath, os.ModePerm)
3838
if mkdirErr != nil {
3939
global.GVA_LOG.Error("function os.MkdirAll() Filed", zap.Any("err", mkdirErr.Error()))
4040
return "", "", errors.New("function os.MkdirAll() Filed, err:" + mkdirErr.Error())
4141
}
4242
// 拼接路径和文件名
43-
p := global.GVA_CONFIG.Local.Path + "/" + filename
43+
p := global.GVA_CONFIG.Local.StorePath + "/" + filename
44+
filepath := global.GVA_CONFIG.Local.Path + "/" + filename
4445

4546
f, openError := file.Open() // 读取文件
4647
if openError != nil {
@@ -62,7 +63,7 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
6263
global.GVA_LOG.Error("function io.Copy() Filed", zap.Any("err", copyErr.Error()))
6364
return "", "", errors.New("function io.Copy() Filed, err:" + copyErr.Error())
6465
}
65-
return p, filename, nil
66+
return filepath, filename, nil
6667
}
6768

6869
//@author: [piexlmax](https://github.com/piexlmax)
@@ -75,8 +76,8 @@ func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
7576
//@return: error
7677

7778
func (*Local) DeleteFile(key string) error {
78-
p := global.GVA_CONFIG.Local.Path + "/" + key
79-
if strings.Contains(p, global.GVA_CONFIG.Local.Path) {
79+
p := global.GVA_CONFIG.Local.StorePath + "/" + key
80+
if strings.Contains(p, global.GVA_CONFIG.Local.StorePath) {
8081
if err := os.Remove(p); err != nil {
8182
return errors.New("本地文件删除失败, err:" + err.Error())
8283
}

web/src/view/layout/aside/historyComponent/history.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ const removeTab = (tab) => {
220220
const index = historys.value.findIndex(
221221
(item) => getFmtString(item) === tab
222222
)
223-
if (getFmtString(route) === tab) {
223+
if (getFmtString(route) === tab) {
224224
if (historys.value.length === 1) {
225225
router.push({ name: defaultRouter.value })
226226
} else {
@@ -264,6 +264,12 @@ watch(route, (to, now) => {
264264
activeValue.value = window.sessionStorage.getItem('activeValue')
265265
})
266266
267+
watch(() => historys.value, () => {
268+
sessionStorage.setItem('historys', JSON.stringify(historys.value))
269+
}, {
270+
deep: true
271+
})
272+
267273
const initPage = () => {
268274
// 全局监听 关闭当前页面函数
269275
emitter.on('closeThisPage', () => {

0 commit comments

Comments
 (0)