✨ 修改mysql配置读取规则
This commit is contained in:
parent
dd15e85083
commit
42b9a3ac5a
@ -1,6 +1,14 @@
|
||||
# Redis配置
|
||||
REDIS_HOST=192.168.3.10
|
||||
REDIS_PWD=123456
|
||||
REDIS_DB=9
|
||||
REDIS_PORT=1100
|
||||
# MySQL配置
|
||||
MYSQL_HOST=192.168.3.9
|
||||
MYSQL_PORT=3306
|
||||
MYSQL_USER=casbin
|
||||
MYSQL_PWD=casbin123
|
||||
MYSQL_DB=casbin_demo
|
||||
# 阿里云通信配置
|
||||
ALI_SMS_KEY=LTAI5tBExwETRZcVcpogQCp7
|
||||
ALI_SMS_SECRET=afZPFabUs5Vmjr3W6R7reI3dNaGa8o
|
@ -8,6 +8,7 @@ import (
|
||||
var (
|
||||
AliSmsConfig aliSmsConfig
|
||||
RedisConfig redisConfig
|
||||
MySQLConfig mysqlConfig
|
||||
)
|
||||
|
||||
// 从环境变量获取字符串类型值
|
||||
|
@ -1 +1,27 @@
|
||||
package config
|
||||
|
||||
// MySQL配置
|
||||
type mysqlConfig struct {
|
||||
Host string // 主机
|
||||
Port string // 端口
|
||||
Username string // 用户名
|
||||
Password string // 密码
|
||||
DbName string // 数据库名称
|
||||
}
|
||||
|
||||
// InitMySQLConfig 初始化OSS配置
|
||||
func InitMySQLConfig() {
|
||||
host := getEnvVal("MYSQL_HOST", "mysql")
|
||||
port := getEnvVal("MYSQL_PORT", "3306")
|
||||
user := getEnvVal("MYSQL_USER", "root")
|
||||
password := getEnvVal("MYSQL_PWD", "root")
|
||||
dbName := getEnvVal("MYSQL_DB", "go_api")
|
||||
|
||||
MySQLConfig = mysqlConfig{
|
||||
Host: host,
|
||||
Port: port,
|
||||
Username: user,
|
||||
Password: password,
|
||||
DbName: dbName,
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package global
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
. "go_api_tmpl/config"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
@ -12,11 +13,8 @@ import (
|
||||
|
||||
// InitMySQLClient 初始化MySQL连接
|
||||
func InitMySQLClient() {
|
||||
USER := "casbin"
|
||||
PASS := "casbin123"
|
||||
HOST := "192.168.3.9"
|
||||
PORT := "3306"
|
||||
DBNAME := "casbin_demo"
|
||||
// 初始化MySQL配置
|
||||
InitMySQLConfig()
|
||||
|
||||
newLogger := logger.New(
|
||||
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
|
||||
@ -27,7 +25,8 @@ func InitMySQLClient() {
|
||||
},
|
||||
)
|
||||
|
||||
url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", USER, PASS, HOST, PORT, DBNAME)
|
||||
url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
|
||||
MySQLConfig.Username, MySQLConfig.Password, MySQLConfig.Host, MySQLConfig.Port, MySQLConfig.DbName)
|
||||
conn, err := gorm.Open(mysql.Open(url), &gorm.Config{Logger: newLogger})
|
||||
if err != nil {
|
||||
Log.Panicf("初始化MySQL连接失败, 错误信息: %v", err)
|
||||
|
@ -1,8 +1,15 @@
|
||||
package initialization
|
||||
|
||||
import "go_api_tmpl/config"
|
||||
import (
|
||||
"go_api_tmpl/config"
|
||||
"go_api_tmpl/global"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
// 初始化日志工具
|
||||
global.InitLogger()
|
||||
// 初始化数据库连接
|
||||
global.InitMySQLClient()
|
||||
// 初始化数据库表
|
||||
DatabaseTable()
|
||||
// 初始化Casbin
|
||||
|
5
main.go
5
main.go
@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"go_api_tmpl/core"
|
||||
"go_api_tmpl/global"
|
||||
"go_api_tmpl/handle"
|
||||
"go_api_tmpl/initialization"
|
||||
"go_api_tmpl/middleware"
|
||||
@ -11,10 +10,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 初始化日志工具
|
||||
global.InitLogger()
|
||||
// 初始化数据库连接
|
||||
global.InitMySQLClient()
|
||||
// 初始化相关组件
|
||||
initialization.Init()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user