perf: 配置文件加载优化
This commit is contained in:
@@ -8,18 +8,20 @@ import (
|
||||
"os"
|
||||
"time"
|
||||
|
||||
libConfig "be.ems/src/lib_features/config"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
|
||||
libConfig "be.ems/lib/config"
|
||||
libGlobal "be.ems/lib/global"
|
||||
)
|
||||
|
||||
//go:embed config/*.yaml
|
||||
var configFiles embed.FS
|
||||
var cfg *viper.Viper
|
||||
|
||||
// 初始化程序配置
|
||||
func InitConfig() {
|
||||
func InitConfig(configDir, assetsDir embed.FS) {
|
||||
cfg = viper.New()
|
||||
initFlag()
|
||||
initViper()
|
||||
initViper(configDir, assetsDir)
|
||||
}
|
||||
|
||||
// 指定参数绑定
|
||||
@@ -28,7 +30,7 @@ func initFlag() {
|
||||
pflag.String("env", "prod", "Specify Run Environment Configuration local or prod")
|
||||
// --c /etc/restconf.yaml
|
||||
// -c /etc/restconf.yaml
|
||||
pConfig := pflag.StringP("config", "c", "./etc/restconf.yaml", "Specify Configuration File")
|
||||
pflag.StringP("config", "c", "./etc/restconf.yaml", "Specify Configuration File")
|
||||
// --version
|
||||
// -V
|
||||
pVersion := pflag.BoolP("version", "V", false, "Output program version")
|
||||
@@ -39,7 +41,7 @@ func initFlag() {
|
||||
|
||||
// 参数固定输出
|
||||
if *pVersion {
|
||||
buildInfo := libConfig.BuildInfo()
|
||||
buildInfo := fmt.Sprintf("OMC version: %s\n%s\n%s\n\n", libGlobal.Version, libGlobal.BuildTime, libGlobal.GoVer)
|
||||
fmt.Println(buildInfo)
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -48,113 +50,130 @@ func initFlag() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// 外层lib和features使用的配置
|
||||
libConfig.ConfigRead(*pConfig)
|
||||
|
||||
viper.BindPFlags(pflag.CommandLine)
|
||||
cfg.BindPFlags(pflag.CommandLine)
|
||||
}
|
||||
|
||||
// 配置文件读取
|
||||
func initViper() {
|
||||
// 在当前工作目录中寻找配置
|
||||
// viper.AddConfigPath("config")
|
||||
// viper.AddConfigPath("src/config")
|
||||
func initViper(configDir, assetsDir embed.FS) {
|
||||
// 如果配置文件名中没有扩展名,则需要设置Type
|
||||
viper.SetConfigType("yaml")
|
||||
cfg.SetConfigType("yaml")
|
||||
|
||||
// 从 embed.FS 中读取默认配置文件内容
|
||||
configDefault, err := configFiles.ReadFile("config/config.default.yaml")
|
||||
configDefault, err := configDir.ReadFile("config/config.default.yaml")
|
||||
if err != nil {
|
||||
log.Fatalf("ReadFile config default file: %s", err)
|
||||
return
|
||||
}
|
||||
// 设置默认配置文件内容到 viper
|
||||
err = viper.ReadConfig(bytes.NewReader(configDefault))
|
||||
err = cfg.ReadConfig(bytes.NewReader(configDefault))
|
||||
if err != nil {
|
||||
log.Fatalf("NewReader config default file: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
// // 配置文件的名称(无扩展名)
|
||||
// viper.SetConfigName("config.default")
|
||||
// // 读取默认配置文件
|
||||
// if err := viper.ReadInConfig(); err != nil {
|
||||
// log.Fatalf("fatal error config default file: %s", err)
|
||||
// }
|
||||
|
||||
env := viper.GetString("env")
|
||||
// 加载运行环境配置
|
||||
env := cfg.GetString("env")
|
||||
if env != "local" && env != "prod" {
|
||||
log.Fatalf("fatal error config env for local or prod : %s", env)
|
||||
}
|
||||
log.Printf("Current service environment operation configuration => %s \n", env)
|
||||
|
||||
// 加载运行配置文件合并相同配置
|
||||
if env == "prod" {
|
||||
// viper.SetConfigName("config.prod")
|
||||
// 从 embed.FS 中读取默认配置文件内容
|
||||
configProd, err := configFiles.ReadFile("config/config.prod.yaml")
|
||||
if err != nil {
|
||||
log.Fatalf("ReadFile config prod file: %s", err)
|
||||
return
|
||||
}
|
||||
// 设置默认配置文件内容到 viper
|
||||
err = viper.MergeConfig(bytes.NewReader(configProd))
|
||||
if err != nil {
|
||||
log.Fatalf("NewReader config prod file: %s", err)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// viper.SetConfigName("config.local")
|
||||
// 从 embed.FS 中读取默认配置文件内容
|
||||
configLocal, err := configFiles.ReadFile("config/config.local.yaml")
|
||||
if err != nil {
|
||||
log.Fatalf("ReadFile config local file: %s", err)
|
||||
return
|
||||
}
|
||||
// 设置默认配置文件内容到 viper
|
||||
err = viper.MergeConfig(bytes.NewReader(configLocal))
|
||||
if err != nil {
|
||||
log.Fatalf("NewReader config local file: %s", err)
|
||||
return
|
||||
}
|
||||
envPath := "config/config.prod.yaml"
|
||||
if env == "local" {
|
||||
envPath = "config/config.local.yaml"
|
||||
}
|
||||
// 从 embed.FS 中读取默认配置文件内容
|
||||
configEnv, err := configDir.ReadFile(envPath)
|
||||
if err != nil {
|
||||
log.Fatalf("ReadFile config local file: %s", err)
|
||||
return
|
||||
}
|
||||
// 设置默认配置文件内容到 viper
|
||||
err = cfg.MergeConfig(bytes.NewReader(configEnv))
|
||||
if err != nil {
|
||||
log.Fatalf("NewReader config local file: %s", err)
|
||||
return
|
||||
}
|
||||
// if err := viper.MergeInConfig(); err != nil {
|
||||
// log.Fatalf("fatal error config MergeInConfig: %s", err)
|
||||
// }
|
||||
|
||||
// 合并外层lib和features使用配置
|
||||
libConfig.ConfigInMerge()
|
||||
// 合并外部使用配置
|
||||
configFile := cfg.GetString("config")
|
||||
if configFile != "" {
|
||||
configInMerge(configFile)
|
||||
}
|
||||
|
||||
// 记录程序开始运行的时间点
|
||||
viper.Set("runTime", time.Now())
|
||||
cfg.Set("runTime", time.Now())
|
||||
|
||||
// 设置程序内全局资源访问
|
||||
cfg.Set("AssetsDir", assetsDir)
|
||||
}
|
||||
|
||||
// 配置文件读取进行内部参数合并
|
||||
func configInMerge(configFile string) {
|
||||
// 指定配置文件读取序列化
|
||||
libConfig.ReadConfig(configFile)
|
||||
uriPrefix := libConfig.GetYamlConfig().OMC.UriPrefix
|
||||
if uriPrefix != "" {
|
||||
libConfig.UriPrefix = uriPrefix
|
||||
}
|
||||
if libConfig.GetYamlConfig().TestConfig.Enabled {
|
||||
libConfig.ReadTestConfigYaml(libConfig.GetYamlConfig().TestConfig.File)
|
||||
}
|
||||
|
||||
// 配置文件读取
|
||||
var v = viper.New()
|
||||
// 设置配置文件路径
|
||||
v.SetConfigFile(configFile)
|
||||
v.SetConfigType("yaml")
|
||||
// 读取配置文件
|
||||
if err := v.ReadInConfig(); err != nil {
|
||||
fmt.Printf("failure to read configuration file: %v \n", err)
|
||||
return
|
||||
}
|
||||
// 合并外层lib和features使用配置
|
||||
for key, value := range v.AllSettings() {
|
||||
// 跳过配置
|
||||
if key == "testconfig" || key == "logger" {
|
||||
continue
|
||||
}
|
||||
// 数据库配置
|
||||
if key == "database" {
|
||||
item := value.(map[string]any)
|
||||
defaultItem := cfg.GetStringMap("gorm.datasource.default")
|
||||
defaultItem["type"] = item["type"]
|
||||
defaultItem["host"] = item["host"]
|
||||
defaultItem["port"] = item["port"]
|
||||
defaultItem["username"] = item["user"]
|
||||
defaultItem["password"] = item["password"]
|
||||
defaultItem["database"] = item["name"]
|
||||
continue
|
||||
}
|
||||
cfg.Set(key, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Env 获取运行服务环境
|
||||
// local prod
|
||||
func Env() string {
|
||||
return viper.GetString("env")
|
||||
return cfg.GetString("env")
|
||||
}
|
||||
|
||||
// RunTime 程序开始运行的时间
|
||||
func RunTime() time.Time {
|
||||
return viper.GetTime("runTime")
|
||||
return cfg.GetTime("runTime")
|
||||
}
|
||||
|
||||
// Get 获取配置信息
|
||||
//
|
||||
// Get("server.port")
|
||||
// Get("server.proxy")
|
||||
func Get(key string) any {
|
||||
return viper.Get(key)
|
||||
return cfg.Get(key)
|
||||
}
|
||||
|
||||
// GetAssetsDirFS 访问程序内全局资源访问
|
||||
func GetAssetsDirFS() embed.FS {
|
||||
return viper.Get("AssetsDir").(embed.FS)
|
||||
}
|
||||
|
||||
// SetAssetsDirFS 设置程序内全局资源访问
|
||||
func SetAssetsDirFS(assetsDir embed.FS) {
|
||||
viper.Set("AssetsDir", assetsDir)
|
||||
return cfg.Get("AssetsDir").(embed.FS)
|
||||
}
|
||||
|
||||
// IsAdmin 用户是否为管理员
|
||||
@@ -165,7 +184,7 @@ func IsAdmin(userID string) bool {
|
||||
// 从本地配置获取user信息
|
||||
admins := Get("user.adminList").([]any)
|
||||
for _, s := range admins {
|
||||
if s.(string) == userID {
|
||||
if fmt.Sprint(s) == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,221 +0,0 @@
|
||||
# 应用服务配置
|
||||
server:
|
||||
# 服务端口
|
||||
port: 33030
|
||||
# 是否开启代理
|
||||
proxy: false
|
||||
|
||||
# 日志
|
||||
logger:
|
||||
fileDir: "/var/log"
|
||||
fileName: "omc.log"
|
||||
level: 2 # 日志记录的等级 0:silent<1:info<2:warn<3:error
|
||||
maxDay: 7 # 日志会保留 180 天
|
||||
maxSize: 10 # 调整按 10MB 大小的切割
|
||||
|
||||
# 静态文件配置, 相对项目根路径或填绝对路径
|
||||
staticFile:
|
||||
# 默认资源,dir目录需要预先创建
|
||||
default:
|
||||
prefix: "/static"
|
||||
dir: "/usr/local/omc/static"
|
||||
# 文件上传资源目录映射,与项目目录同级
|
||||
upload:
|
||||
prefix: "/upload"
|
||||
dir: "/usr/local/omc/upload"
|
||||
|
||||
# 文件上传
|
||||
upload:
|
||||
# 最大上传文件大小,默认为 10mb
|
||||
fileSize: 10
|
||||
# 文件扩展名白名单
|
||||
whitelist:
|
||||
# 图片
|
||||
- ".bmp"
|
||||
- ".webp"
|
||||
- ".gif"
|
||||
- ".jpg"
|
||||
- ".jpeg"
|
||||
- ".png"
|
||||
# word excel powerpoint
|
||||
- ".doc"
|
||||
- ".docx"
|
||||
- ".xls"
|
||||
- ".xlsx"
|
||||
- ".ppt"
|
||||
- ".pptx"
|
||||
# 文本文件
|
||||
- ".html"
|
||||
- ".htm"
|
||||
- ".txt"
|
||||
# pdf
|
||||
- ".pdf"
|
||||
# 压缩文件
|
||||
- ".zip"
|
||||
- ".gz"
|
||||
- ".tgz"
|
||||
- ".gzip"
|
||||
# 音视频格式
|
||||
- ".mp3"
|
||||
- ".mp4"
|
||||
- ".avi"
|
||||
- ".rmvb"
|
||||
# 软件包
|
||||
- ".deb"
|
||||
- ".rpm"
|
||||
# 验证文件
|
||||
- ".ini"
|
||||
|
||||
# cors 跨域
|
||||
cors:
|
||||
# 设置 Access-Control-Allow-Origin 的值,【默认值】会获取请求头上的 origin
|
||||
# 例如:http://mask-api.org
|
||||
# 如果请求设置了 credentials,则 origin 不能设置为 *
|
||||
origin: "*"
|
||||
# 设置 Access-Control-Allow-Credentials,【默认值】false
|
||||
credentials: true
|
||||
# 设置 Access-Control-Max-Age
|
||||
maxAge: 31536000
|
||||
# 允许跨域的方法,【默认值】为 GET,HEAD,PUT,POST,DELETE,PATCH
|
||||
allowMethods:
|
||||
- "OPTIONS"
|
||||
- "HEAD"
|
||||
- "GET"
|
||||
- "POST"
|
||||
- "PUT"
|
||||
- "DELETE"
|
||||
- "PATCH"
|
||||
# 设置 Access-Control-Allow-Headers 的值,【默认值】会获取请求头上的 Access-Control-Request-Headers
|
||||
allowHeaders:
|
||||
- "X-App-Code"
|
||||
- "X-App-Version"
|
||||
- "Authorization"
|
||||
- "Origin"
|
||||
- "X-Requested-With"
|
||||
- "Content-Type"
|
||||
- "Content-Language"
|
||||
- "Accept-Language"
|
||||
- "Accept"
|
||||
- "Range"
|
||||
# 非标准请求头
|
||||
- "accessToken"
|
||||
- "Accesstoken"
|
||||
- "Operationtype"
|
||||
# 设置 Access-Control-Expose-Headers 的值
|
||||
exposeHeaders:
|
||||
- "X-RepeatSubmit-Rest"
|
||||
|
||||
# security 安全
|
||||
security:
|
||||
csrf:
|
||||
enable: false
|
||||
type: "referer"
|
||||
# 允许调用的域名地址的,例如:http://<Referer地址>/mask-api
|
||||
refererWhiteList:
|
||||
- "127.0.0.1:3030"
|
||||
xframe:
|
||||
enable: false
|
||||
value: "SAMEORIGIN"
|
||||
csp:
|
||||
enable: true
|
||||
hsts:
|
||||
enable: false
|
||||
maxAge: 31536000
|
||||
includeSubdomains: false
|
||||
noopen:
|
||||
enable: false
|
||||
nosniff:
|
||||
enable: false
|
||||
xssProtection:
|
||||
enable: true
|
||||
value: "1; mode=block"
|
||||
|
||||
# JWT 令牌配置
|
||||
jwt:
|
||||
# 令牌算法 HS256 HS384 HS512
|
||||
algorithm: "HS512"
|
||||
# 令牌密钥
|
||||
secret: "217a0481c7f9cfe1cb547d32ee012b0f"
|
||||
# 令牌有效期(默认120分钟)
|
||||
expiresIn: 120
|
||||
# 验证令牌有效期,相差不足xx分钟,自动刷新缓存
|
||||
refreshIn: 20
|
||||
|
||||
# GORM 数据源
|
||||
gorm:
|
||||
dataSource:
|
||||
# 默认数据库实例
|
||||
default:
|
||||
type: "mysql"
|
||||
host: "127.0.0.1"
|
||||
port: 3306
|
||||
username: "<username>"
|
||||
password: "<password>"
|
||||
database: "<database>"
|
||||
logging: false
|
||||
# 多个数据源时可以用这个指定默认的数据源
|
||||
defaultDataSourceName: "default"
|
||||
|
||||
# Redis 缓存数据
|
||||
redis:
|
||||
dataSource:
|
||||
default:
|
||||
port: 6379 # Redis port
|
||||
host: "127.0.0.1" # Redis host
|
||||
password: "<password>"
|
||||
db: 0 # Redis db_num
|
||||
# 多个数据源时可以用这个指定默认的数据源
|
||||
defaultDataSourceName: "default"
|
||||
|
||||
# AES 加密
|
||||
aes:
|
||||
# 接口密钥
|
||||
apiKey: "T9ox2DCzpLfJIPzkH9pKhsOTMOEMJcFv"
|
||||
# 网元主机密钥
|
||||
hostKey: "AGT66VfY4SMaiT97a7df0aef1704d5c5"
|
||||
|
||||
# 用户配置
|
||||
user:
|
||||
# 登录认证,默认打开
|
||||
loginAuth: true
|
||||
# 接口加密,默认打开
|
||||
cryptoApi: true
|
||||
# 密码
|
||||
password:
|
||||
# 密码最大错误次数
|
||||
maxRetryCount: 5
|
||||
# 密码锁定时间,单位分钟(默认10分钟)
|
||||
lockTime: 10
|
||||
# 管理员列表
|
||||
adminList:
|
||||
- "1"
|
||||
|
||||
# char 字符验证码配置
|
||||
charCaptcha:
|
||||
# 宽度
|
||||
width: 120
|
||||
# 高度
|
||||
height: 40
|
||||
# 干扰线条的数量
|
||||
noise: 4
|
||||
# 验证码的字符是否有颜色,默认没有,如果设定了背景,则默认有
|
||||
color: true
|
||||
# 验证码图片背景颜色
|
||||
background: "#fafafa"
|
||||
# 验证码长度
|
||||
size: 4
|
||||
# 验证码字符
|
||||
chars: "023456789abcdefghjkmnprstuvwxyz"
|
||||
|
||||
# math 数值计算码配置
|
||||
mathCaptcha:
|
||||
# 宽度
|
||||
width: 120
|
||||
# 高度
|
||||
height: 40
|
||||
# 干扰线条的数量
|
||||
noise: 4
|
||||
# 验证码的字符是否有颜色,默认没有,如果设定了背景,则默认有
|
||||
color: true
|
||||
# 验证码图片背景颜色
|
||||
background: "#fafafa"
|
||||
@@ -1,54 +0,0 @@
|
||||
# 应用服务配置
|
||||
server:
|
||||
port: 33040
|
||||
proxy: true
|
||||
|
||||
# 日志
|
||||
logger:
|
||||
fileDir: "C:/var/log"
|
||||
level: 0 # 输出最低等级
|
||||
|
||||
# 静态文件配置, 相对项目根路径或填绝对路径
|
||||
staticFile:
|
||||
default:
|
||||
dir: "C:/usr/local/omc/static"
|
||||
# 文件上传资源目录映射,与项目目录同级
|
||||
upload:
|
||||
dir: "C:/usr/local/omc/upload"
|
||||
|
||||
# security 安全
|
||||
security:
|
||||
csrf:
|
||||
refererWhiteList:
|
||||
- "localhost:3131"
|
||||
- "127.0.0.1:3131"
|
||||
|
||||
# GORM 数据源
|
||||
gorm:
|
||||
dataSource:
|
||||
default:
|
||||
type: "mysql"
|
||||
host: "127.0.0.1"
|
||||
port: 33066
|
||||
username: "root"
|
||||
password: "1000omc@kp!"
|
||||
database: "omc_db"
|
||||
logging: true
|
||||
|
||||
# Redis 缓存数据,数据源声明全小写
|
||||
redis:
|
||||
dataSource:
|
||||
# OMC系统使用库
|
||||
default:
|
||||
port: 6379 # Redis port
|
||||
host: "127.0.0.1" # Redis host
|
||||
password: "helloearth"
|
||||
db: 10 # Redis db_num
|
||||
# UDM网元用户库
|
||||
udmuser:
|
||||
port: 6379 # Redis port
|
||||
host: "127.0.0.1"
|
||||
password: "helloearth"
|
||||
db: 0 # Redis db_num
|
||||
# 多个数据源时可以用这个指定默认的数据源
|
||||
defaultDataSourceName: "default"
|
||||
@@ -1,32 +0,0 @@
|
||||
# 应用服务配置
|
||||
server:
|
||||
port: 33030
|
||||
proxy: true
|
||||
|
||||
# security 安全
|
||||
security:
|
||||
csrf:
|
||||
# 允许调用的域名地址的,例如:http://<Referer>/
|
||||
refererWhiteList:
|
||||
- "127.0.0.1"
|
||||
- "<Referer>"
|
||||
|
||||
# GORM 数据源
|
||||
gorm:
|
||||
dataSource:
|
||||
default:
|
||||
type: "mysql"
|
||||
host: "<mysql host>"
|
||||
port: 3306
|
||||
username: "<mysql username>"
|
||||
password: "<mysql password>"
|
||||
database: "<mysql database>"
|
||||
|
||||
# Redis 缓存数据
|
||||
redis:
|
||||
dataSource:
|
||||
default:
|
||||
port: 6379 # Redis port
|
||||
host: "<redis host>"
|
||||
password: "<redis password>"
|
||||
db: 0 # Redis db_num
|
||||
@@ -3,15 +3,15 @@ package logger
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"be.ems/src/framework/config"
|
||||
)
|
||||
|
||||
var logWriter *Logger
|
||||
|
||||
// 初始程序日志
|
||||
func InitLogger() {
|
||||
env := viper.GetString("env")
|
||||
conf := viper.GetStringMap("logger")
|
||||
env := config.Get("env").(string)
|
||||
conf := config.Get("logger").(map[string]any)
|
||||
fileDir := conf["filedir"].(string)
|
||||
fileName := conf["filename"].(string)
|
||||
level := conf["level"].(int)
|
||||
|
||||
Reference in New Issue
Block a user