-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zhuwenliang
committed
Jul 22, 2024
1 parent
67fb086
commit 0db346f
Showing
9 changed files
with
144 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ectx | ||
|
||
import "context" | ||
|
||
type appContextType string | ||
|
||
var ( | ||
appCtxKey appContextType = "app" | ||
) | ||
func GetAppIdFromCtx(ctx context.Context) (uint, bool) { | ||
app := ctx.Value(appCtxKey) | ||
if app == nil { | ||
return 0, false | ||
} | ||
v, ok := app.(uint) | ||
return v, ok | ||
} | ||
|
||
func CtxWithAppId(ctx context.Context, appid uint) context.Context { | ||
return context.WithValue(ctx, appCtxKey, appid) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/ecodeclub/webook/internal/pkg/ectx" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/ecodeclub/ginx" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gotomicro/ego/core/elog" | ||
) | ||
|
||
type CheckAppIdBuilder struct { | ||
} | ||
|
||
|
||
const ( | ||
appIDHeader = "app" | ||
) | ||
|
||
func NewCheckAppIdBuilder() *CheckAppIdBuilder { | ||
return &CheckAppIdBuilder{} | ||
} | ||
func (a *CheckAppIdBuilder) Build() gin.HandlerFunc { | ||
return func(ctx *gin.Context) { | ||
gctx := &ginx.Context{Context: ctx} | ||
appid := ctx.GetHeader(appIDHeader) | ||
if appid == "" { | ||
return | ||
} | ||
c := ctx.Request.Context() | ||
app, err := strconv.Atoi(appid) | ||
if err != nil { | ||
gctx.AbortWithStatus(http.StatusBadRequest) | ||
elog.Error("appid设置失败", elog.FieldErr(err)) | ||
return | ||
} | ||
newCtx := ectx.CtxWithAppId(c, uint(app)) | ||
ctx.Request = ctx.Request.WithContext(newCtx) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.