提交 ced2646e authored 作者: mooncake's avatar mooncake

modify ctx

上级 67348e31
...@@ -72,8 +72,8 @@ func parseInit() { ...@@ -72,8 +72,8 @@ func parseInit() {
} }
type Application struct { type Application struct {
app *app.App app *app.App
h func() *gin.Engine router func() *gin.Engine
} }
func (x *Application) Run() { func (x *Application) Run() {
...@@ -97,7 +97,7 @@ func (x *Application) Run() { ...@@ -97,7 +97,7 @@ func (x *Application) Run() {
eventbus.Eb.Publish(ctx, eventbus.TopicGinEngineCreated) eventbus.Eb.Publish(ctx, eventbus.TopicGinEngineCreated)
eventbus.Eb.Publish(ctx, eventbus.TopicPrintSwagger) eventbus.Eb.Publish(ctx, eventbus.TopicPrintSwagger)
services := CreateServices(x.h()) services := CreateServices(x.router())
closes := Close(services) closes := Close(services)
x.app = app.New(services, closes) x.app = app.New(services, closes)
x.app.Run() x.app.Run()
...@@ -124,9 +124,13 @@ func (x *Application) Prepare() { ...@@ -124,9 +124,13 @@ func (x *Application) Prepare() {
eventbus.Eb.Publish(ctx, eventbus.TopicPrintSwagger) eventbus.Eb.Publish(ctx, eventbus.TopicPrintSwagger)
} }
func New(h func() *gin.Engine) *Application { func New() *Application {
return &Application{}
}
func NewWithRouter(router func() *gin.Engine) *Application {
return &Application{ return &Application{
h: h, router: router,
} }
} }
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"strings" "strings"
"time" "time"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm/query"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/xsf" "gitlab.wanzhuangkj.com/tush/xpkg/xutils/xsf"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
...@@ -407,3 +408,19 @@ func WrapAsyncCtx(c *gin.Context) context.Context { ...@@ -407,3 +408,19 @@ func WrapAsyncCtx(c *gin.Context) context.Context {
ctx = context.WithValue(ctx, GinContextKey, c) //nolint ctx = context.WithValue(ctx, GinContextKey, c) //nolint
return ctx return ctx
} }
type IPagination interface {
GetPageIndex() int
GetPageSize() int
}
func SetPage(ctx context.Context, in IPagination) {
if ginCtx, err := GetGinCtx(ctx); err == nil && ginCtx != nil {
ginCtx.Set(KeyApiType, "page")
pagination := query.Pagination{
PageIndex: in.GetPageIndex(),
PageSize: in.GetPageSize(),
}
ginCtx.Set(KeyPagination, pagination)
}
}
package goutils package goutils
import ( import (
"context"
"runtime/debug" "runtime/debug"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger" "gitlab.wanzhuangkj.com/tush/xpkg/pkg/logger"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/ctxutils"
) )
func Recover() { func Recover() {
...@@ -12,9 +14,15 @@ func Recover() { ...@@ -12,9 +14,15 @@ func Recover() {
} }
} }
func Safely(f func()) { func RecoverCtx(ctx context.Context) {
if r := recover(); r != nil {
logger.Fatalf("Recovered panic: %v\nStack trace:\n%s", r, string(debug.Stack()), ctxutils.CtxTraceIDField(ctx))
}
}
func Go(ctx context.Context, f func()) {
go func() { go func() {
defer Recover() defer RecoverCtx(ctx)
f() f()
}() }()
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论