feat : brahma 适配新环境变量方法

master
liuhaotian 2022-01-14 19:54:40 +08:00
parent ea827d8b54
commit fa25d64ef0
3 changed files with 17 additions and 12 deletions

View File

@ -1,7 +1,6 @@
package gorm_helper
import (
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/config"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
"github.com/gofiber/fiber/v2"
@ -14,8 +13,8 @@ type MysqlMapType map[string]*gorm.DB
var mysqlMap = make(MysqlMapType, 0)
func GormMap(dsnPre string) error {
dsn := fmt.Sprintf("%v/%v?charset=utf8mb4&parseTime=True&loc=Local", dsnPre, config.TestDB)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
//dsn := fmt.Sprintf("%v/%v?charset=utf8mb4&parseTime=True&loc=Local", dsnPre, config.TestDB)
db, err := gorm.Open(mysql.Open(dsnPre), &gorm.Config{})
if err != nil {
return err
}

View File

@ -2,11 +2,11 @@ package main
import (
"fmt"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/config"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/error_process"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/gorm_helper"
"git.icechen.cn/monorepo/backend/app/brahma/api/murder/internal/handler"
"git.icechen.cn/monorepo/backend/pkg/api"
"git.icechen.cn/monorepo/backend/pkg/config"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/gofiber/fiber/v2/middleware/requestid"
@ -19,6 +19,7 @@ const (
tag = "/tag"
category = "/category"
scripts = "/scripts"
hello = "/hello"
)
var (
@ -36,10 +37,12 @@ func init() {
// ResourceInit 读取配置初始化资源
func ResourceInit() {
config.GetConfig()
err = gorm_helper.GormMap(config.GetConfigMap()[config.GetConfigKey(config.DsnPre)])
mysql, err := config.GetMysql()
if err != nil {
ctxLogger.FError(nil, "etcd读取mysql出错", zap.String("", err.Error()))
os.Exit(-1)
}
err = gorm_helper.GormMap(mysql)
if err != nil {
ctxLogger.FError(nil, error_process.GormError, zap.String("", err.Error()))
os.Exit(-1)
@ -107,12 +110,15 @@ func routerInit() {
scriptsGroup.Delete("", func(ctx *fiber.Ctx) error {
return handler.DeleteScriptsH(ctx)
})
helloGroup := app.Group(hello)
helloGroup.Get("", func(ctx *fiber.Ctx) error {
return handler.HelloWorld()
})
}
func serverStart() {
port := config.GetEnvDefault(config.Port, config.PortDefault)
err = app.Listen(fmt.Sprintf(":%v", port))
err = app.Listen(fmt.Sprintf(":%v", 8080))
if err != nil {
ctxLogger.Error(nil, "", zap.String("", err.Error()))
os.Exit(-1)

4
pkg/env/env.go vendored
View File

@ -6,8 +6,8 @@ import (
)
func init() {
AppName = GetEnvDefault("APP_NAME", "default")
Namespace = GetEnvDefault("NAMESPACE", "default")
AppName = GetEnvDefault("APP_NAME", "murder")
Namespace = GetEnvDefault("NAMESPACE", "brahma")
AppType = SAppType(GetEnvDefault("APP_TYPE", string(AppTypeAPI)))
}