Compare commits
2 Commits
9bb43f4484
...
761c24efbf
Author | SHA1 | Date | |
---|---|---|---|
761c24efbf | |||
af58c03c7c |
9
.env
Normal file
9
.env
Normal file
@ -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
|
3
.gitignore
vendored
3
.gitignore
vendored
@ -16,7 +16,8 @@
|
||||
*.out
|
||||
|
||||
# Dependency directories (remove the comment below to include it)
|
||||
# vendor/
|
||||
vendor/
|
||||
logs/
|
||||
|
||||
# Go workspace file
|
||||
go.work
|
||||
|
23
client/mysql.go
Normal file
23
client/mysql.go
Normal file
@ -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
|
||||
}
|
26
client/redis.go
Normal file
26
client/redis.go
Normal file
@ -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
config.yaml
Normal file
0
config.yaml
Normal file
9
config/app.go
Normal file
9
config/app.go
Normal file
@ -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"` // 妖怪模式
|
||||
}
|
12
config/config.go
Normal file
12
config/config.go
Normal file
@ -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配置
|
||||
}
|
20
config/mysql.go
Normal file
20
config/mysql.go
Normal file
@ -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)
|
||||
}
|
9
config/nacos.go
Normal file
9
config/nacos.go
Normal file
@ -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"` // 外部配置文件名,多个以逗号隔开
|
||||
}
|
15
config/redis.go
Normal file
15
config/redis.go
Normal file
@ -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)
|
||||
}
|
56
go.mod
56
go.mod
@ -4,45 +4,95 @@ go 1.18
|
||||
|
||||
require (
|
||||
git.echol.cn/loser/logger v1.0.14
|
||||
git.echol.cn/loser/nacos-viper-remote v0.4.1-0.20220525104600-e38430672884
|
||||
gitee.ltd/lxh/logger v1.0.14
|
||||
github.com/caarlos0/env/v6 v6.9.2
|
||||
github.com/fsnotify/fsnotify v1.5.1
|
||||
github.com/gin-gonic/gin v1.7.7
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/google/uuid v1.1.2
|
||||
github.com/spf13/viper v1.10.1
|
||||
gorm.io/driver/mysql v1.3.2
|
||||
gorm.io/gorm v1.23.5
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.99.0 // indirect
|
||||
cloud.google.com/go/firestore v1.6.1 // indirect
|
||||
github.com/aliyun/alibaba-cloud-sdk-go v1.61.18 // indirect
|
||||
github.com/armon/go-metrics v0.3.10 // indirect
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/caarlos0/env/v6 v6.9.2 // indirect
|
||||
github.com/buger/jsonparser v1.1.1 // indirect
|
||||
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.1.2 // indirect
|
||||
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4 // indirect
|
||||
github.com/cncf/xds/go v0.0.0-20211130200136-a8f946100490 // indirect
|
||||
github.com/coreos/go-semver v0.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1 // indirect
|
||||
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
|
||||
github.com/fatih/color v1.13.0 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-errors/errors v1.0.1 // indirect
|
||||
github.com/go-kit/kit v0.12.0 // indirect
|
||||
github.com/go-kit/log v0.2.1 // indirect
|
||||
github.com/go-logfmt/logfmt v0.5.1 // indirect
|
||||
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/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
|
||||
github.com/golang/mock v1.6.0 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/go-cmp v0.5.6 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.1.1 // indirect
|
||||
github.com/hashicorp/consul/api v1.12.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/hashicorp/go-hclog v1.0.0 // indirect
|
||||
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
|
||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||
github.com/hashicorp/hcl v1.0.0 // indirect
|
||||
github.com/hashicorp/serf v0.9.6 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.4 // indirect
|
||||
github.com/jmespath/go-jmespath v0.4.0 // indirect
|
||||
github.com/jpillora/backoff v1.0.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/leodido/go-urn v1.2.0 // indirect
|
||||
github.com/lixh00/loki-client-go v1.0.1 // indirect
|
||||
github.com/magiconair/properties v1.8.5 // indirect
|
||||
github.com/mattn/go-colorable v0.1.12 // indirect
|
||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
|
||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.5.0 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
|
||||
github.com/nacos-group/nacos-sdk-go/v2 v2.0.0 // indirect
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible // indirect
|
||||
github.com/pelletier/go-toml v1.9.4 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_golang v1.12.2 // indirect
|
||||
github.com/prometheus/client_model v0.2.0 // indirect
|
||||
github.com/prometheus/common v0.34.0 // indirect
|
||||
github.com/prometheus/procfs v0.7.3 // indirect
|
||||
github.com/prometheus/prometheus v1.8.2-0.20201028100903-3245b3267b24 // indirect
|
||||
github.com/sagikazarmark/crypt v0.4.0 // indirect
|
||||
github.com/spf13/afero v1.6.0 // indirect
|
||||
github.com/spf13/cast v1.4.1 // indirect
|
||||
github.com/spf13/jwalterweatherman v1.1.0 // indirect
|
||||
github.com/spf13/pflag v1.0.5 // indirect
|
||||
github.com/subosito/gotenv v1.2.0 // indirect
|
||||
github.com/toolkits/concurrent v0.0.0-20150624120057-a4371d70e3e3 // indirect
|
||||
github.com/ugorji/go/codec v1.1.7 // indirect
|
||||
go.etcd.io/etcd/api/v3 v3.5.1 // indirect
|
||||
go.etcd.io/etcd/client/pkg/v3 v3.5.1 // indirect
|
||||
go.etcd.io/etcd/client/v2 v2.305.1 // indirect
|
||||
go.opencensus.io v0.23.0 // indirect
|
||||
go.uber.org/atomic v1.9.0 // indirect
|
||||
go.uber.org/multierr v1.8.0 // indirect
|
||||
go.uber.org/zap v1.21.0 // indirect
|
||||
@ -51,9 +101,13 @@ require (
|
||||
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
|
||||
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
google.golang.org/api v0.63.0 // indirect
|
||||
google.golang.org/appengine v1.6.7 // indirect
|
||||
google.golang.org/genproto v0.0.0-20220505152158-f39f71e6c8f3 // indirect
|
||||
google.golang.org/grpc v1.46.2 // indirect
|
||||
google.golang.org/protobuf v1.28.0 // indirect
|
||||
gopkg.in/ini.v1 v1.66.2 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
)
|
||||
|
11
initialize/clients.go
Normal file
11
initialize/clients.go
Normal file
@ -0,0 +1,11 @@
|
||||
package initialize
|
||||
|
||||
import "online_code/client"
|
||||
|
||||
// 初始化数据库客户端
|
||||
func initClient() {
|
||||
// 初始化MySQL
|
||||
client.InitMySQLClient()
|
||||
// 初始化Redis
|
||||
client.InitRedisClient()
|
||||
}
|
68
initialize/config.go
Normal file
68
initialize/config.go
Normal file
@ -0,0 +1,68 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/config"
|
||||
|
||||
nr "git.echol.cn/loser/nacos-viper-remote"
|
||||
"github.com/caarlos0/env/v6"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/spf13/viper"
|
||||
_ "github.com/spf13/viper/remote"
|
||||
)
|
||||
|
||||
var vp *viper.Viper
|
||||
|
||||
// 初始化Nacos
|
||||
func initNacos() {
|
||||
// 初始化Nacos配置
|
||||
if err := env.Parse(&config.Nacos); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if config.Nacos.Host == "" {
|
||||
log.Panic("Nacos配置错误")
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化配置文件
|
||||
func initConfig() {
|
||||
vp = viper.New()
|
||||
|
||||
// 配置 Viper for Nacos 的远程仓库参数
|
||||
nr.SetOptions(&nr.Option{
|
||||
Url: config.Nacos.Host, // nacos server 多地址需要地址用;号隔开,如 Url: "loc1;loc2;loc3"
|
||||
Port: config.Nacos.Port, // nacos server端口号
|
||||
NamespaceId: config.Nacos.NamespaceId, // nacos namespace
|
||||
GroupName: "DEFAULT_GROUP", // nacos group
|
||||
Config: nr.Config{DataId: config.Nacos.CenterConfigName}, // nacos DataID
|
||||
Auth: nil, // 如果需要验证登录,需要此参数
|
||||
})
|
||||
err := vp.AddRemoteProvider("nacos", config.Nacos.Host, "")
|
||||
if err != nil {
|
||||
log.Panicf("%s", err)
|
||||
}
|
||||
vp.SetConfigType("yaml")
|
||||
//尝试进行配置读取
|
||||
if err = vp.ReadRemoteConfig(); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
//异步监听Nacos中的配置变化,如发生配置更改,会直接同步到 viper实例中。
|
||||
err = vp.WatchRemoteConfigOnChannel()
|
||||
if err != nil {
|
||||
log.Errorf("监听远程配置变动失败: %v", err)
|
||||
}
|
||||
|
||||
// 解析配置文件为结构体
|
||||
if err = vp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
vp.WatchConfig() // 监听配置文件变化
|
||||
vp.OnConfigChange(func(e fsnotify.Event) {
|
||||
log.Info("配置文件发生变动:", e.Name)
|
||||
if err = vp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
// 配置文件发生变动,重新初始化一下连接信息
|
||||
initClient()
|
||||
})
|
||||
}
|
19
initialize/db_table.go
Normal file
19
initialize/db_table.go
Normal file
@ -0,0 +1,19 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/client"
|
||||
"online_code/models/entity"
|
||||
)
|
||||
|
||||
// 初始化数据库表
|
||||
func databaseTable() {
|
||||
dbs := []any{
|
||||
new(entity.User), // 管理员用户
|
||||
}
|
||||
|
||||
if err := client.MySQL.AutoMigrate(dbs...); err != nil {
|
||||
log.Panicf("数据库表预初始化处理:%s", err.Error())
|
||||
}
|
||||
log.Debugf("数据库表预初始化处理完成")
|
||||
}
|
10
initialize/init.go
Normal file
10
initialize/init.go
Normal file
@ -0,0 +1,10 @@
|
||||
package initialize
|
||||
|
||||
// InitSystem 初始化系统
|
||||
func InitSystem() {
|
||||
//initNacos() // 初始化nacos
|
||||
//initConfig() // 初始化配置
|
||||
initLocaConfig() // 初始化本地配置
|
||||
initClient() // 初始化连接池等信息
|
||||
databaseTable() // 初始化数据库表信息
|
||||
}
|
33
initialize/locaConfig.go
Normal file
33
initialize/locaConfig.go
Normal file
@ -0,0 +1,33 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.echol.cn/loser/logger/log"
|
||||
"online_code/config"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var localVp *viper.Viper
|
||||
|
||||
func initLocaConfig() {
|
||||
localVp = viper.New()
|
||||
|
||||
localVp.SetConfigName("config") // 文件名-无需文件后缀
|
||||
localVp.SetConfigType("yaml") // 文件扩展名
|
||||
localVp.AddConfigPath(".")
|
||||
|
||||
// 处理读取配置文件的错误
|
||||
if err := localVp.ReadInConfig(); err != nil {
|
||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
||||
panic(fmt.Errorf("未找到配置文件,请检查路径: %s \n", err))
|
||||
} else {
|
||||
panic(fmt.Errorf("文件已找到,但读取文件时出错: %s \n", err))
|
||||
}
|
||||
}
|
||||
|
||||
// 解析配置文件为结构体
|
||||
if err := localVp.Unmarshal(&config.Scd); err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user