Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
61d7db49
提交
61d7db49
authored
9月 09, 2025
作者:
mooncake9527
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
9f4ce0e8
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
31 行增加
和
13 行删除
+31
-13
config.go
config/config.go
+1
-1
init.go
config/init.go
+14
-12
test.yml
config/test.yml
+10
-0
init.go
database/init.go
+6
-0
没有找到文件。
config/config.go
浏览文件 @
61d7db49
...
@@ -94,7 +94,7 @@ type Config struct {
...
@@ -94,7 +94,7 @@ type Config struct {
ConfType
string
`yaml:"confType" json:"confType"`
ConfType
string
`yaml:"confType" json:"confType"`
Local
Local
`yaml:"-" json:"local"`
Local
Local
`yaml:"-" json:"local"`
Consul
Consul
`yaml:"-" json:"consul"`
Consul
Consul
`yaml:"-" json:"consul"`
Nacos
onacos
.
NacosConfig
`yaml:"
-
" json:"nacos"`
Nacos
onacos
.
NacosConfig
`yaml:"
nacos
" json:"nacos"`
Database
[]
Database
`yaml:"database" json:"database"`
Database
[]
Database
`yaml:"database" json:"database"`
Etcd
Etcd
`yaml:"etcd" json:"etcd"`
Etcd
Etcd
`yaml:"etcd" json:"etcd"`
...
...
config/init.go
浏览文件 @
61d7db49
...
@@ -29,10 +29,9 @@ func parse() (err error) {
...
@@ -29,10 +29,9 @@ func parse() (err error) {
if
hostname
:=
os
.
Getenv
(
"HOSTNAME"
);
hostname
!=
""
{
if
hostname
:=
os
.
Getenv
(
"HOSTNAME"
);
hostname
!=
""
{
Cfg
.
App
.
PodName
=
hostname
Cfg
.
App
.
PodName
=
hostname
}
}
confType
:=
loadEnv
WithDefault
(
"CONF_TYPE"
,
"local"
)
confType
:=
readEnvVar
WithDefault
(
"CONF_TYPE"
,
"local"
)
logger
.
Infof
(
"[conf]conf type:%s"
,
confType
)
logger
.
Infof
(
"[conf]conf type:%s"
,
confType
)
Cfg
.
ConfType
=
confType
Cfg
.
ConfType
=
confType
if
confType
==
"local"
{
if
confType
==
"local"
{
localConf
()
localConf
()
logger
.
Infof
(
"[conf]conf:%s"
,
Cfg
.
Local
.
Conf
)
logger
.
Infof
(
"[conf]conf:%s"
,
Cfg
.
Local
.
Conf
)
...
@@ -60,6 +59,7 @@ func parse() (err error) {
...
@@ -60,6 +59,7 @@ func parse() (err error) {
if
err
:=
v
.
Unmarshal
(
&
Cfg
);
err
!=
nil
{
if
err
:=
v
.
Unmarshal
(
&
Cfg
);
err
!=
nil
{
return
err
return
err
}
}
logger
.
Debug
(
"[conf]parse conf"
,
logger
.
Any
(
"conf"
,
Cfg
))
return
ParseExtend
()
return
ParseExtend
()
},
logger
.
Get
())
},
logger
.
Get
())
if
err
:=
nc
.
Connect
()
.
Watch
()
.
Fetch
()
.
Parse
()
.
Err
();
err
!=
nil
{
if
err
:=
nc
.
Connect
()
.
Watch
()
.
Fetch
()
.
Parse
()
.
Err
();
err
!=
nil
{
...
@@ -78,7 +78,6 @@ func ParseExtend() error {
...
@@ -78,7 +78,6 @@ func ParseExtend() error {
v
:=
viper
.
New
()
v
:=
viper
.
New
()
v
.
SetConfigType
(
"yaml"
)
v
.
SetConfigType
(
"yaml"
)
content
:=
string
(
Cfg
.
GetContent
())
content
:=
string
(
Cfg
.
GetContent
())
// if err := v.ReadConfig(bytes.NewReader(Cfg.GetContent())); err != nil {
if
err
:=
v
.
ReadConfig
(
strings
.
NewReader
(
content
));
err
!=
nil
{
if
err
:=
v
.
ReadConfig
(
strings
.
NewReader
(
content
));
err
!=
nil
{
return
err
return
err
}
}
...
@@ -89,26 +88,26 @@ func ParseExtend() error {
...
@@ -89,26 +88,26 @@ func ParseExtend() error {
if
err
:=
subv
.
Unmarshal
(
extend
);
err
!=
nil
{
if
err
:=
subv
.
Unmarshal
(
extend
);
err
!=
nil
{
return
err
return
err
}
}
logger
.
Info
(
"[conf]
"
,
logger
.
Any
(
"extend"
,
extend
))
logger
.
Debug
(
"[conf]parse extend
"
,
logger
.
Any
(
"extend"
,
extend
))
}
}
}
}
return
nil
return
nil
}
}
func
localConf
()
{
func
localConf
()
{
confFile
:=
loadEnv
WithDefault
(
"CONF_FILES"
,
"./conf/conf.yaml"
)
confFile
:=
readEnvVar
WithDefault
(
"CONF_FILES"
,
"./conf/conf.yaml"
)
Write
(
func
(
c
*
Config
)
{
Write
(
func
(
c
*
Config
)
{
c
.
Local
.
Conf
=
confFile
c
.
Local
.
Conf
=
confFile
})
})
}
}
func
nacosConf
()
{
func
nacosConf
()
{
host
:=
loadEnv
WithDefault
(
"NACOS_HOST"
,
"localhost"
)
host
:=
readEnvVar
WithDefault
(
"NACOS_HOST"
,
"localhost"
)
port
:=
loadEnv
WithDefault
(
"NACOS_PORT"
,
"8848"
)
port
:=
readEnvVar
WithDefault
(
"NACOS_PORT"
,
"8848"
)
contextPath
:=
loadEnv
WithDefault
(
"NACOS_CONTEXT"
,
"/nacos"
)
contextPath
:=
readEnvVar
WithDefault
(
"NACOS_CONTEXT"
,
"/nacos"
)
namespace
:=
loadEnv
WithDefault
(
"NACOS_NAMESPACE"
,
""
)
namespace
:=
readEnvVar
WithDefault
(
"NACOS_NAMESPACE"
,
""
)
dataID
:=
loadEnvWithDefault
(
"NACOS_DATA_ID"
,
"operator-admin
.yaml"
)
dataID
:=
readEnvVarWithDefault
(
"NACOS_DATA_ID"
,
"conf
.yaml"
)
group
:=
loadEnvWithDefault
(
"NACOS_GROUP"
,
"qitu
"
)
group
:=
readEnvVarWithDefault
(
"NACOS_GROUP"
,
"DEFAULT_GROUP
"
)
logger
.
Infof
(
"[conf]conf host:%s namespace:%s dataId:%s group:%s"
,
host
,
namespace
,
dataID
,
group
)
logger
.
Infof
(
"[conf]conf host:%s namespace:%s dataId:%s group:%s"
,
host
,
namespace
,
dataID
,
group
)
Cfg
.
Nacos
.
IPs
=
strings
.
Split
(
host
,
","
)
Cfg
.
Nacos
.
IPs
=
strings
.
Split
(
host
,
","
)
Cfg
.
Nacos
.
Port
=
cast
.
ToInt
(
port
)
Cfg
.
Nacos
.
Port
=
cast
.
ToInt
(
port
)
...
@@ -116,6 +115,9 @@ func nacosConf() {
...
@@ -116,6 +115,9 @@ func nacosConf() {
Cfg
.
Nacos
.
Conf
.
DataID
=
dataID
Cfg
.
Nacos
.
Conf
.
DataID
=
dataID
Cfg
.
Nacos
.
Conf
.
Group
=
group
Cfg
.
Nacos
.
Conf
.
Group
=
group
Cfg
.
Nacos
.
Conf
.
ContextPath
=
contextPath
Cfg
.
Nacos
.
Conf
.
ContextPath
=
contextPath
Cfg
.
Nacos
.
Rd
.
GroupName
=
group
}
}
func
consulConf
()
{
func
consulConf
()
{
...
@@ -136,7 +138,7 @@ func consulConf() {
...
@@ -136,7 +138,7 @@ func consulConf() {
}
}
}
}
func
loadEnv
WithDefault
(
key
,
defaultValue
string
)
string
{
func
readEnvVar
WithDefault
(
key
,
defaultValue
string
)
string
{
if
value
,
exists
:=
os
.
LookupEnv
(
key
);
exists
{
if
value
,
exists
:=
os
.
LookupEnv
(
key
);
exists
{
return
value
return
value
}
}
...
...
config/test.yml
浏览文件 @
61d7db49
...
@@ -44,6 +44,7 @@ logger:
...
@@ -44,6 +44,7 @@ logger:
database
:
database
:
-
name
:
"
wz_qitu"
-
name
:
"
wz_qitu"
driver
:
"
mysql"
driver
:
"
mysql"
default
:
true
mysql
:
mysql
:
dsn
:
"
root:123456@tcp(localhost:30006)/wz_qitu?parseTime=true&loc=Local&charset=utf8,utf8mb4"
dsn
:
"
root:123456@tcp(localhost:30006)/wz_qitu?parseTime=true&loc=Local&charset=utf8,utf8mb4"
enableLog
:
true
enableLog
:
true
...
@@ -57,6 +58,15 @@ redis:
...
@@ -57,6 +58,15 @@ redis:
readTimeout
:
2
readTimeout
:
2
writeTimeout
:
2
writeTimeout
:
2
oss
:
endpoint
:
https://oss-cn-hangzhou.aliyuncs.com
# 外网
access-key-id
:
LTAI5tD3VA4pi1pcz48yYgXL
access-key-secret
:
sus17wfcbjB6J9hDeJXXFtJf9LPN0S
bucket-name
:
prod-qitu
base-path
:
baofu
region
:
oss-cn-hangzhou
bucket-url
:
https://prod-qitu.oss-cn-hangzhou.aliyuncs.com
auth
:
auth
:
enable
:
true
enable
:
true
expire
:
6h
expire
:
6h
...
...
database/init.go
浏览文件 @
61d7db49
...
@@ -23,10 +23,16 @@ func InitDB() (err error) {
...
@@ -23,10 +23,16 @@ func InitDB() (err error) {
config
.
Read
(
func
(
c
*
config
.
Config
)
{
config
.
Read
(
func
(
c
*
config
.
Config
)
{
databases
=
c
.
Database
databases
=
c
.
Database
})
})
if
len
(
databases
)
==
1
{
consts
.
DefaultSchema
=
databases
[
0
]
.
Name
}
else
{
for
_
,
db
:=
range
databases
{
for
_
,
db
:=
range
databases
{
if
db
.
Default
{
if
db
.
Default
{
consts
.
DefaultSchema
=
db
.
Name
consts
.
DefaultSchema
=
db
.
Name
}
}
}
}
for
_
,
db
:=
range
databases
{
switch
strings
.
ToLower
(
db
.
Driver
)
{
switch
strings
.
ToLower
(
db
.
Driver
)
{
case
sgorm
.
DBDriverMysql
,
sgorm
.
DBDriverTidb
:
case
sgorm
.
DBDriverMysql
,
sgorm
.
DBDriverTidb
:
gdb
,
err
:=
InitMysql
(
&
db
)
gdb
,
err
:=
InitMysql
(
&
db
)
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论