This commit is contained in:
2026-09-04 17:51:30 +08:00
parent 04402a8bdf
commit e615be6ad7
18 changed files with 449 additions and 124 deletions
+23
View File
@@ -2,6 +2,7 @@ package utils
import (
"context"
"net/http"
"net/url"
"strconv"
@@ -9,6 +10,12 @@ import (
"google.golang.org/grpc/metadata"
)
const (
CtxCallbackRawBody string = "callback_raw_body"
CtxCallbackHeaders string = "callback_headers"
CtxCallbackErr string = "callback_read_err"
)
type UserInfo struct {
ID int64
Name string
@@ -31,6 +38,11 @@ type UserInfo struct {
Valid bool
}
type HeaderAndBody struct {
Header http.Header
Body []byte
}
func firstMD(md metadata.MD, keys ...string) string {
for _, key := range keys {
vals := md.Get(key)
@@ -109,3 +121,14 @@ func GetUserFromCtx(ctx context.Context) UserInfo {
return userInfo
}
// GetHeaderAndBody 只从ctx拿数据,不写http响应,Err交给handler处理
func GetHeaderAndBody(ctx context.Context) HeaderAndBody {
rawBody, _ := ctx.Value(CtxCallbackRawBody).([]byte)
headers, _ := ctx.Value(CtxCallbackHeaders).(http.Header)
return HeaderAndBody{
Header: headers,
Body: rawBody,
}
}