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

View File

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

4
pkg/env/env.go vendored
View File

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