feat: app loader

This commit is contained in:
ivamp 2024-06-16 01:03:33 +08:00
parent 87661426c0
commit 5dc1df3e2f
11 changed files with 66 additions and 56 deletions

View File

@ -1,8 +1,6 @@
package config package config
import ( import (
"github.com/joho/godotenv"
"github.com/kos-v/dsnparser"
"os" "os"
"strings" "strings"
) )
@ -41,33 +39,6 @@ type defaultConfig struct {
var Config = defaultConfig{} var Config = defaultConfig{}
func InitConfig() {
envPath := GetEnvFilePath()
_ = godotenv.Load(envPath)
Config.DB.DSN = GetEnv("DB_DSN")
Config.DB.Driver = "postgres"
Config.Redis.Addr = GetEnv("REDIS_ADDR")
Config.Redis.Pass = GetEnv("REDIS_PASS")
Config.GRPC.GrpcListenAddr = GetEnv("LISTEN_ADDR")
Config.HTTP.ListenAddr = GetEnv("HTTP_LISTEN_ADDR")
Config.JWKS.Url = GetEnv("JWKS_URL")
Config.DebugMode.Enable = GetEnv("DEBUG", "false") == "true"
dsn := dsnparser.Parse(Config.DB.DSN)
var dsn2 = ""
dsn2 += "user=" + dsn.GetUser()
dsn2 += " password=" + dsn.GetPassword()
dsn2 += " dbname=" + dsn.GetPath()
if dsn.HasParam("sslmode") {
dsn2 += " sslmode=" + dsn.GetParam("sslmode")
}
Config.DB.DSN2 = dsn2
}
func GetEnv(key string, defaultValue ...string) string { func GetEnv(key string, defaultValue ...string) string {
r := os.Getenv(key) r := os.Getenv(key)
if len(r) == 0 && len(defaultValue) > 0 { if len(r) == 0 && len(defaultValue) > 0 {

View File

@ -0,0 +1,33 @@
package config
import (
"github.com/joho/godotenv"
"github.com/kos-v/dsnparser"
)
func InitConfig() {
envPath := GetEnvFilePath()
_ = godotenv.Load(envPath)
Config.DB.DSN = GetEnv("DB_DSN")
Config.DB.Driver = "postgres"
Config.Redis.Addr = GetEnv("REDIS_ADDR")
Config.Redis.Pass = GetEnv("REDIS_PASS")
Config.GRPC.GrpcListenAddr = GetEnv("LISTEN_ADDR")
Config.HTTP.ListenAddr = GetEnv("HTTP_LISTEN_ADDR")
Config.JWKS.Url = GetEnv("JWKS_URL")
Config.DebugMode.Enable = GetEnv("DEBUG", "false") == "true"
dsn := dsnparser.Parse(Config.DB.DSN)
var dsn2 = ""
dsn2 += "user=" + dsn.GetUser()
dsn2 += " password=" + dsn.GetPassword()
dsn2 += " dbname=" + dsn.GetPath()
if dsn.HasParam("sslmode") {
dsn2 += " sslmode=" + dsn.GetParam("sslmode")
}
Config.DB.DSN2 = dsn2
}

16
internal/app/gin/init.go Normal file
View File

@ -0,0 +1,16 @@
package gin
import (
"framework_v2/internal/app/facades"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin"
"time"
)
func InitGin() {
gin.SetMode(gin.ReleaseMode)
facades.Router = gin.New()
facades.Router.Use(ginzap.Ginzap(facades.Logger, time.RFC3339, true))
}

View File

@ -6,23 +6,11 @@ import (
"framework_v2/internal/app/helpers" "framework_v2/internal/app/helpers"
"framework_v2/internal/app/user" "framework_v2/internal/app/user"
http2 "framework_v2/internal/middleware/http" http2 "framework_v2/internal/middleware/http"
ginzap "github.com/gin-contrib/zap"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"reflect" "reflect"
"time"
) )
func InitGin() {
gin.SetMode(gin.ReleaseMode)
facades.Router = gin.New()
facades.Router.Use(ginzap.Ginzap(facades.Logger, time.RFC3339, true))
//access.Router.Use(gin.Recovery())
//access.Router.Use(ginzap.RecoveryWithZap(access.Logger, true))
}
//func HandleRoute(method httpMethod, relativePath string, controller HandlerFunc, middlewares ...gin.HandlerFunc) { //func HandleRoute(method httpMethod, relativePath string, controller HandlerFunc, middlewares ...gin.HandlerFunc) {
// access.Router.Handle(method.String(), relativePath, func(c *gin.Context) { // access.Router.Handle(method.String(), relativePath, func(c *gin.Context) {
// for _, middleware := range middlewares { // for _, middleware := range middlewares {

13
internal/app/jwks/init.go Normal file
View File

@ -0,0 +1,13 @@
package jwks
import "time"
func InitJwksRefresh() {
// 启动一个定时器
go func() {
for {
RefreshJWKS()
time.Sleep(refreshRate)
}
}()
}

View File

@ -17,16 +17,6 @@ var (
ErrJWKSNotInitialized = errors.New("JWKS is not initialized") ErrJWKSNotInitialized = errors.New("JWKS is not initialized")
) )
func InitJwksRefresh() {
// 启动一个定时器
go func() {
for {
RefreshJWKS()
time.Sleep(refreshRate)
}
}()
}
func RefreshJWKS() { func RefreshJWKS() {
logger.Logger.Info("Refreshing JWKS...") logger.Logger.Info("Refreshing JWKS...")

View File

@ -7,7 +7,7 @@ import (
"framework_v2/internal/app/jobs" "framework_v2/internal/app/jobs"
"framework_v2/internal/app/jwks" "framework_v2/internal/app/jwks"
"framework_v2/internal/app/logger" "framework_v2/internal/app/logger"
"framework_v2/internal/app/server" "framework_v2/internal/routes"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -23,7 +23,7 @@ var httpCommand = &cobra.Command{
jwks.InitJwksRefresh() jwks.InitJwksRefresh()
jobs.InitAsynQClient() jobs.InitAsynQClient()
gin.InitGin() gin.InitGin()
server.InitHttp() routes.InitHttp()
StartHttp() StartHttp()
}, },

View File

@ -1,8 +1,7 @@
package server package routes
import ( import (
"framework_v2/internal/app/facades" "framework_v2/internal/app/facades"
"framework_v2/internal/routes"
) )
func InitHttp() { func InitHttp() {
@ -10,5 +9,5 @@ func InitHttp() {
panic("You must call InitGin() before InitHttp()") panic("You must call InitGin() before InitHttp()")
} }
routes.InitApiRoutes() InitApiRoutes()
} }