认证授权主要流程实现
This commit is contained in:
105
init/env/env.go
vendored
105
init/env/env.go
vendored
@@ -3,40 +3,20 @@ package env
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gofiber/fiber/v2/log"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
// region app
|
||||
|
||||
var (
|
||||
AppName = "platform"
|
||||
AppPort = "8080"
|
||||
)
|
||||
|
||||
var (
|
||||
DbHost = "localhost"
|
||||
DbPort = "3306"
|
||||
DbName string
|
||||
DbUserName string
|
||||
DbPassword string
|
||||
)
|
||||
|
||||
var (
|
||||
LogLevel = slog.LevelDebug
|
||||
)
|
||||
|
||||
func Init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Debug("❓ 没有本地环境变量")
|
||||
} else {
|
||||
log.Debug("✔ 加载本地环境变量")
|
||||
}
|
||||
|
||||
check()
|
||||
}
|
||||
|
||||
func check() {
|
||||
func loadApp() {
|
||||
_AppName := os.Getenv("APP_NAME")
|
||||
if _AppName != "" {
|
||||
AppName = _AppName
|
||||
@@ -46,7 +26,21 @@ func check() {
|
||||
if _AppPort != "" {
|
||||
AppPort = _AppPort
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region db
|
||||
|
||||
var (
|
||||
DbHost = "localhost"
|
||||
DbPort = "5432"
|
||||
DbName string
|
||||
DbUserName string
|
||||
DbPassword string
|
||||
)
|
||||
|
||||
func loadDb() {
|
||||
_DbHost := os.Getenv("DB_HOST")
|
||||
if _DbHost != "" {
|
||||
DbHost = _DbHost
|
||||
@@ -77,7 +71,54 @@ func check() {
|
||||
} else {
|
||||
panic("环境变量 DB_PASSWORD 的值为空")
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region redis
|
||||
|
||||
var (
|
||||
RedisHost = "localhost"
|
||||
RedisPort = "6379"
|
||||
RedisDb = 0
|
||||
RedisPass = ""
|
||||
)
|
||||
|
||||
func loadRedis() {
|
||||
_RedisHost := os.Getenv("REDIS_HOST")
|
||||
if _RedisHost != "" {
|
||||
RedisHost = _RedisHost
|
||||
}
|
||||
|
||||
_RedisPort := os.Getenv("REDIS_PORT")
|
||||
if _RedisPort != "" {
|
||||
RedisPort = _RedisPort
|
||||
}
|
||||
|
||||
_RedisDb := os.Getenv("REDIS_DB")
|
||||
if _RedisDb != "" {
|
||||
atoi, err := strconv.Atoi(_RedisDb)
|
||||
if err != nil {
|
||||
panic("环境变量 REDIS_DB 的值不是数字")
|
||||
}
|
||||
RedisDb = atoi
|
||||
}
|
||||
|
||||
_RedisPass := os.Getenv("REDIS_PASS")
|
||||
if _RedisPass != "" {
|
||||
RedisPass = _RedisPass
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region log
|
||||
|
||||
var (
|
||||
LogLevel = slog.LevelDebug
|
||||
)
|
||||
|
||||
func loadLog() {
|
||||
_LogLevel := os.Getenv("LOG_LEVEL")
|
||||
switch _LogLevel {
|
||||
case "debug":
|
||||
@@ -90,3 +131,19 @@ func check() {
|
||||
LogLevel = slog.LevelError
|
||||
}
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
func Init() {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
log.Debug("❓ 没有本地环境变量")
|
||||
} else {
|
||||
log.Debug("✔ 加载本地环境变量")
|
||||
}
|
||||
|
||||
loadApp()
|
||||
loadDb()
|
||||
loadRedis()
|
||||
loadLog()
|
||||
}
|
||||
|
||||
@@ -9,14 +9,18 @@ import (
|
||||
"github.com/lmittmann/tint"
|
||||
)
|
||||
|
||||
var Default *slog.Logger
|
||||
|
||||
func Init() {
|
||||
Default = slog.New(
|
||||
slog.SetDefault(slog.New(
|
||||
tint.NewHandler(os.Stdout, &tint.Options{
|
||||
Level: env.LogLevel,
|
||||
TimeFormat: time.Kitchen,
|
||||
ReplaceAttr: func(_ []string, attr slog.Attr) slog.Attr {
|
||||
err, ok := attr.Value.Any().(error)
|
||||
if ok {
|
||||
return tint.Err(err)
|
||||
}
|
||||
return attr
|
||||
},
|
||||
}),
|
||||
)
|
||||
slog.SetDefault(Default)
|
||||
))
|
||||
}
|
||||
|
||||
@@ -4,40 +4,39 @@ import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"platform/init/env"
|
||||
"platform/init/logs"
|
||||
"platform/web/queries"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
import "gorm.io/driver/postgres"
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func Init() {
|
||||
logger := logs.Default
|
||||
|
||||
// 连接数据库
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai",
|
||||
env.DbName, env.DbUserName, env.DbPassword, env.DbName, env.DbPort,
|
||||
env.DbHost, env.DbUserName, env.DbPassword, env.DbName, env.DbPort,
|
||||
)
|
||||
|
||||
open, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
SingularTable: true,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error("gorm 打开数据库失败", slog.Any("err", err))
|
||||
slog.Error("gorm 初始化数据库失败:", slog.Any("err", err))
|
||||
panic(err)
|
||||
}
|
||||
|
||||
sql, err := open.DB()
|
||||
// 连接池
|
||||
conn, err := db.DB()
|
||||
if err != nil {
|
||||
logger.Error("gorm open db error: ", slog.Any("err", err))
|
||||
slog.Error("gorm 初始化数据库失败:", slog.Any("err", err))
|
||||
panic(err)
|
||||
}
|
||||
sql.SetMaxIdleConns(10)
|
||||
sql.SetMaxOpenConns(100)
|
||||
conn.SetMaxIdleConns(10)
|
||||
conn.SetMaxOpenConns(100)
|
||||
|
||||
DB = open
|
||||
// 初始化查询工具
|
||||
queries.SetDefault(db)
|
||||
}
|
||||
|
||||
18
init/rds/rds.go
Normal file
18
init/rds/rds.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package rds
|
||||
|
||||
import (
|
||||
"net"
|
||||
"platform/init/env"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var Client *redis.Client
|
||||
|
||||
func Init() {
|
||||
Client = redis.NewClient(&redis.Options{
|
||||
Addr: net.JoinHostPort(env.RedisHost, env.RedisPort),
|
||||
DB: env.RedisDb,
|
||||
Password: env.RedisPass,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user