1 Star 0 Fork 0

linxing/youye-core

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
application.go 2.66 KB
一键复制 编辑 原始数据 按行查看 历史
Mark 提交于 2024-05-28 09:46 . strip env prefix
package sdk
import (
"log"
"gitee.com/linxing_3/youye-core/config/source"
"gitee.com/linxing_3/youye-core/config/source/env"
"gitee.com/linxing_3/youye-core/config/source/file"
"gitee.com/linxing_3/youye-core/sdk/application"
"gitee.com/linxing_3/youye-core/sdk/config"
"gitee.com/linxing_3/youye-core/sdk/pkg/captcha"
"gitee.com/linxing_3/youye-core/sdk/pkg/cronjob"
"gitee.com/linxing_3/youye-core/sdk/pkg/database"
"gitee.com/linxing_3/youye-core/sdk/pkg/logger"
"github.com/gin-gonic/gin"
)
var App application.IApplication = application.NewConfig()
func NewApp(configPath string, envprefix string) (application.IApplication, error) {
err := config.Setup([]source.Source{
file.NewSource(file.WithPath(configPath)),
env.NewSource(
env.WithPrefix(envprefix),
env.WithStrippedPrefix(envprefix),
),
})
if err != nil {
return nil, err
}
globalConf := config.GetConfig()
App.SetConfig(globalConf)
// if logger.DefaultLogger == nil {
initLogger(globalConf)
// }
initDB(globalConf)
if err := initCache(globalConf); err != nil {
return nil, err
}
App.SetCrontab("*", cronjob.NewWithSeconds())
if !config.LockerConfig.Empty() {
lockerAdapter, err := config.LockerConfig.Setup()
if err != nil {
log.Fatalf("locker setup error, %s\n", err.Error())
}
App.SetLockerAdapter(lockerAdapter)
}
ginEngine := gin.New()
if config.ApplicationConfig.Mode == gin.DebugMode {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
ginEngine.Use(gin.Recovery())
App.SetEngine(ginEngine)
return App, nil
}
func initLogger(conf *config.Config) {
l := logger.SetupLogger(
logger.WithType(conf.Logger.Type),
logger.WithPath(conf.Logger.Path),
logger.WithLevel(conf.Logger.Level),
logger.WithStdout(conf.Logger.Stdout),
logger.WithCap(conf.Logger.Cap),
logger.WithCallSkip(conf.Logger.SkipCount),
)
App.SetLogger(l)
}
func initDB(conf *config.Config) {
dblog := App.GetLogger()
if conf.Database != nil && conf.Database.Logger != nil {
dblog = logger.SetupLogger(
logger.WithType(conf.Database.Logger.Type),
logger.WithPath(conf.Database.Logger.Path),
logger.WithLevel(conf.Database.Logger.Level),
logger.WithStdout(conf.Database.Logger.Stdout),
logger.WithCap(conf.Database.Logger.Cap),
logger.WithCallSkip(conf.Database.Logger.SkipCount),
)
}
db, err := database.NewDatabase("*", conf.Database, dblog)
if err != nil {
log.Fatalf("database setup error, %s\n", err.Error())
}
App.SetDb("*", db)
}
func initCache(*config.Config) error {
cache, err := config.CacheConfig.Setup()
if err != nil {
return err
}
captcha.SetStore(captcha.NewCacheStore(cache, 600))
App.SetCacheAdapter(cache)
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/linxing_3/youye-core.git
git@gitee.com:linxing_3/youye-core.git
linxing_3
youye-core
youye-core
v0.0.1-202405280946

搜索帮助