回调
This commit is contained in:
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user