✨ 完成nacos和数据库等配置文件
parent
9bb43f4484
commit
af58c03c7c
@ -0,0 +1,23 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.echol.cn/loser/logger/log"
|
||||||
|
"online_code/config"
|
||||||
|
|
||||||
|
"git.echol.cn/loser/logger"
|
||||||
|
"gorm.io/driver/mysql"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var MySQL *gorm.DB
|
||||||
|
|
||||||
|
func InitMySQLClient() {
|
||||||
|
// 创建连接对象
|
||||||
|
conn, err := gorm.Open(mysql.Open(config.Scd.MySQL.GetDSN()), &gorm.Config{Logger: logger.DefaultGormLogger()})
|
||||||
|
if err != nil {
|
||||||
|
log.Panic("初始化MySQL连接失败, 错误信息: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Debug("MySQL连接成功")
|
||||||
|
}
|
||||||
|
MySQL = conn
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package client
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"git.echol.cn/loser/logger/log"
|
||||||
|
"github.com/go-redis/redis/v8"
|
||||||
|
"online_code/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var Redis *redis.Client
|
||||||
|
|
||||||
|
func InitRedisClient() {
|
||||||
|
conf := config.Scd.Redis
|
||||||
|
// 初始化连接
|
||||||
|
conn := redis.NewClient(&redis.Options{
|
||||||
|
Addr: conf.GetDSN(),
|
||||||
|
Password: conf.Password,
|
||||||
|
DB: conf.Db,
|
||||||
|
})
|
||||||
|
if err := conn.Ping(context.Background()).Err(); err != nil {
|
||||||
|
log.Panicf("Redis连接初始化失败: %v", err)
|
||||||
|
} else {
|
||||||
|
log.Debug("Redis连接初始化成功")
|
||||||
|
}
|
||||||
|
Redis = conn
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
type appInfo struct {
|
||||||
|
AppName string `mapstructure:"name" yaml:"name"` // 应用名称
|
||||||
|
Port uint64 `mapstructure:"port" yaml:"port"` // 端口号
|
||||||
|
Prefix string `mapstructure:"prefix" yaml:"prefix"` // 接口前缀
|
||||||
|
Version string `mapstructure:"version" yaml:"version"` // 版本号
|
||||||
|
Monster bool `mapstructure:"monster" yaml:"monster"` // 妖怪模式
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
var Scd systemConfigData
|
||||||
|
var Nacos nacosConfig
|
||||||
|
|
||||||
|
// 配置信息
|
||||||
|
type systemConfigData struct {
|
||||||
|
Admin appInfo `mapstructure:"admin" yaml:"admin"` // 系统配置-Admin
|
||||||
|
Api appInfo `mapstructure:"api" yaml:"api"` // 系统配置-Api
|
||||||
|
MySQL mysqlConfig `mapstructure:"mysql" yaml:"mysql"` // MySQL配置
|
||||||
|
Redis redisConfig `mapstructure:"redis" yaml:"redis"` // Redis配置
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MySQL配置
|
||||||
|
type mysqlConfig struct {
|
||||||
|
Host string `mapstructure:"host" yaml:"host"` // 主机
|
||||||
|
Port int `mapstructure:"port" yaml:"port"` // 端口
|
||||||
|
User string `mapstructure:"user" yaml:"user"` // 用户名
|
||||||
|
Password string `mapstructure:"password" yaml:"password"` // 密码
|
||||||
|
Db string `mapstructure:"db" yaml:"db"` // 数据库名称
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDSN 返回 MySQL 连接字符串
|
||||||
|
func (c mysqlConfig) GetDSN() string {
|
||||||
|
return fmt.Sprintf("%s:%s@tcp(%s:%v)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||||
|
c.User, c.Password, c.Host, c.Port, c.Db)
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
// nacosConfig Nacos配置
|
||||||
|
type nacosConfig struct {
|
||||||
|
Host string `env:"NACOS_HOST"` // 主机
|
||||||
|
Port uint64 `env:"NACOS_PORT" envDefault:"8848"` // 端口
|
||||||
|
NamespaceId string `env:"NACOS_NAMESPACE"` // 命名空间
|
||||||
|
CenterConfigName string `env:"NACOS_CONFIG_NAME" envDefault:"gtest.yml"` // 外部配置文件名,多个以逗号隔开
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// Redis配置
|
||||||
|
type redisConfig struct {
|
||||||
|
Host string `mapstructure:"host" yaml:"host"` // 主机
|
||||||
|
Port int `mapstructure:"port" yaml:"port"` // 端口
|
||||||
|
Password string `mapstructure:"password" yaml:"password"` // 密码
|
||||||
|
Db int `mapstructure:"db" yaml:"db"` // 数据库名称
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r redisConfig) GetDSN() string {
|
||||||
|
return fmt.Sprintf("%s:%v", r.Host, r.Port)
|
||||||
|
}
|
Loading…
Reference in New Issue