feat: 结构整理
parent
28fdd5a348
commit
e321686d4b
|
@ -10,4 +10,11 @@ const (
|
||||||
Etcd = "etcd"
|
Etcd = "etcd"
|
||||||
EtcdEnd = "endpoints"
|
EtcdEnd = "endpoints"
|
||||||
EtcdEndDefault = "127.0.0.1:2379"
|
EtcdEndDefault = "127.0.0.1:2379"
|
||||||
|
Port
|
||||||
|
PortDefault = "8080"
|
||||||
|
)
|
||||||
|
|
||||||
|
//mysql 数据库实例名称
|
||||||
|
const (
|
||||||
|
TestDB = "test"
|
||||||
)
|
)
|
||||||
|
|
|
@ -3,4 +3,6 @@ package consts
|
||||||
const (
|
const (
|
||||||
EtcConnError = "etcConnectError"
|
EtcConnError = "etcConnectError"
|
||||||
ErrorReason = "reason"
|
ErrorReason = "reason"
|
||||||
|
PanicError = "panicError"
|
||||||
|
GormError = "gormErr"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,23 +1,20 @@
|
||||||
package handler
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bgm/consts"
|
||||||
|
bgm "bgm/helper"
|
||||||
"bgm/model"
|
"bgm/model"
|
||||||
"bgm/service"
|
"bgm/service"
|
||||||
"errors"
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/rs/xid"
|
"github.com/rs/xid"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateCategoryH(ctx *fiber.Ctx) error {
|
func CreateCategoryH(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error {
|
||||||
i := ctx.UserContext().Value("local")
|
|
||||||
category := &model.Category{
|
category := &model.Category{
|
||||||
Key: 1,
|
Key: 1,
|
||||||
Value: "默认",
|
Value: "默认",
|
||||||
Gid: xid.New().String(),
|
Gid: xid.New().String(),
|
||||||
}
|
}
|
||||||
if k, ok := i.(*gorm.DB); ok {
|
return service.CreateCategory(category, mysqlMap[consts.TestDB])
|
||||||
return service.CreateCategory(category, k)
|
|
||||||
}
|
|
||||||
return errors.New("")
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,16 @@
|
||||||
package bgm
|
package bgm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bgm/consts"
|
||||||
"fmt"
|
"fmt"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GormMap(dsnPre string) (map[string]*gorm.DB, error) {
|
type MysqlMap map[string]*gorm.DB
|
||||||
dsn := fmt.Sprintf("%v/test?charset=utf8mb4&parseTime=True&loc=Local", dsnPre)
|
|
||||||
|
func GormMap(dsnPre string) (MysqlMap, error) {
|
||||||
|
dsn := fmt.Sprintf("%v/%v?charset=utf8mb4&parseTime=True&loc=Local", dsnPre, consts.TestDB)
|
||||||
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||||
return map[string]*gorm.DB{"local": db}, err
|
return map[string]*gorm.DB{consts.TestDB: db}, err
|
||||||
}
|
}
|
||||||
|
|
40
main.go
40
main.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"bgm/handler"
|
"bgm/handler"
|
||||||
"bgm/helper"
|
"bgm/helper"
|
||||||
"bgm/model"
|
"bgm/model"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||||
|
@ -13,7 +12,6 @@ import (
|
||||||
"github.com/gofiber/fiber/v2/middleware/requestid"
|
"github.com/gofiber/fiber/v2/middleware/requestid"
|
||||||
ctxLogger "github.com/luizsuper/ctxLoggers"
|
ctxLogger "github.com/luizsuper/ctxLoggers"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gorm.io/gorm"
|
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
)
|
)
|
||||||
|
@ -22,10 +20,12 @@ const (
|
||||||
createCategory = "/sts"
|
createCategory = "/sts"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type handleM map[string]func(ctx *fiber.Ctx, mysqlMap bgm.MysqlMap) error
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gormMap = map[string]*gorm.DB{}
|
mysqlMap = make(bgm.MysqlMap, 0)
|
||||||
err error
|
err error
|
||||||
handlerMap = make(map[string]func(ctx *fiber.Ctx) error, 0)
|
handlerMap = make(handleM, 0)
|
||||||
app = fiber.New(fiber.Config{
|
app = fiber.New(fiber.Config{
|
||||||
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
||||||
return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"})
|
return ctx.Status(fiber.StatusInternalServerError).JSON(model.Response{Message: "服务器内部错误"})
|
||||||
|
@ -49,29 +49,33 @@ func init() {
|
||||||
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
|
StackTraceHandler: func(c *fiber.Ctx, e interface{}) {
|
||||||
buf := make([]byte, 1024*1024)
|
buf := make([]byte, 1024*1024)
|
||||||
buf = buf[:runtime.Stack(buf, false)]
|
buf = buf[:runtime.Stack(buf, false)]
|
||||||
ctxLogger.FError(c, "panic err", zap.String("stack", fmt.Sprintf("panic: %v\n%s\n", e, buf)))
|
ctxLogger.FError(c, consts.PanicError, zap.String(consts.ErrorReason, fmt.Sprintf("panic: %v\n%s\n", e, buf)))
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
gormMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)])
|
mysqlMap, err = bgm.GormMap(bgm.ConfigMap()[bgm.GetConfigKey(consts.DsnPre)])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxLogger.FError(nil, "gorm err", zap.String("gorm", err.Error()))
|
ctxLogger.FError(nil, consts.GormError, zap.String(consts.ErrorReason, err.Error()))
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
app.Use(func(ctx *fiber.Ctx) error {
|
routerInit()
|
||||||
background := context.Background()
|
|
||||||
ctx.SetUserContext(context.WithValue(background, "local", gormMap["local"]))
|
|
||||||
return ctx.Next()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func routerInit() {
|
||||||
app.Get("/*", func(ctx *fiber.Ctx) error {
|
handlerMap[createCategory] = handler.CreateCategoryH
|
||||||
defer ctx.UserContext().Done()
|
}
|
||||||
return handlerMap[ctx.Path()](ctx)
|
|
||||||
})
|
func serverStart() {
|
||||||
err := app.Listen(":8080")
|
port := bgm.GetEnvDefault(consts.Port, consts.PortDefault)
|
||||||
|
err = app.Listen(fmt.Sprintf(":%v", port))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctxLogger.Error(nil, "", zap.String("err", err.Error()))
|
ctxLogger.Error(nil, "", zap.String("err", err.Error()))
|
||||||
os.Exit(-1)
|
os.Exit(-1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
serverStart()
|
||||||
|
app.Get("/*", func(ctx *fiber.Ctx) error {
|
||||||
|
return handlerMap[ctx.Path()](ctx, mysqlMap)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue