认证授权主要流程实现
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user