diff --git a/.env b/.env new file mode 100644 index 0000000..b12a3cd --- /dev/null +++ b/.env @@ -0,0 +1,9 @@ +# Nacos配置 +NACOS_HOST=127.0.0.1 +NACOS_PORT=8848 +# 命名空间,用默认的public的话这儿就不写 +NACOS_NAMESPACE=41f4f415-970b-4839-8f71-d4fa4c6d6c30 +# 除本文件以外的其他托管到Nacos的配置文件 Ps. 后面可以把NACOS_*和系统启动相关的留着,其他的全部托管到Nacos +NACOS_CONFIG_NAME=mysql.yaml +# 是否关闭gorm自动更新表结构 +DISABLE_SYNC_MODELS=false \ No newline at end of file diff --git a/client/mysql.go b/client/mysql.go new file mode 100644 index 0000000..2dfae2f --- /dev/null +++ b/client/mysql.go @@ -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 +} diff --git a/client/redis.go b/client/redis.go new file mode 100644 index 0000000..7150a9d --- /dev/null +++ b/client/redis.go @@ -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 +} diff --git a/config/app.go b/config/app.go new file mode 100644 index 0000000..55fbee4 --- /dev/null +++ b/config/app.go @@ -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"` // 妖怪模式 +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..d5581cf --- /dev/null +++ b/config/config.go @@ -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配置 +} diff --git a/config/mysql.go b/config/mysql.go new file mode 100644 index 0000000..97ee610 --- /dev/null +++ b/config/mysql.go @@ -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) +} diff --git a/config/nacos.go b/config/nacos.go new file mode 100644 index 0000000..3389f66 --- /dev/null +++ b/config/nacos.go @@ -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"` // 外部配置文件名,多个以逗号隔开 +} diff --git a/config/redis.go b/config/redis.go new file mode 100644 index 0000000..a6150ba --- /dev/null +++ b/config/redis.go @@ -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) +} diff --git a/go.mod b/go.mod index 029e83e..d0d3ad3 100644 --- a/go.mod +++ b/go.mod @@ -5,14 +5,18 @@ go 1.18 require ( git.echol.cn/loser/logger v1.0.14 github.com/gin-gonic/gin v1.7.7 + github.com/go-redis/redis/v8 v8.11.5 github.com/google/uuid v1.1.2 + gorm.io/driver/mysql v1.3.2 gorm.io/gorm v1.23.5 ) require ( + gitee.ltd/lxh/logger v1.0.14 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/caarlos0/env/v6 v6.9.2 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-kit/kit v0.12.0 // indirect github.com/go-kit/log v0.2.1 // indirect @@ -20,6 +24,7 @@ require ( github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/validator/v10 v10.4.1 // indirect + github.com/go-sql-driver/mysql v1.6.0 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect github.com/golang/snappy v0.0.4 // indirect