|
|
@@ -16,6 +16,8 @@ const (
|
|
|
queryParamsKey = "query-params-context"
|
|
|
pathParamsKey = "path-params-context"
|
|
|
headerKey = "header-context"
|
|
|
+ tenantInfoKey = "context-tenant-info"
|
|
|
+ userInfoKey = "context-user-info"
|
|
|
)
|
|
|
|
|
|
type Context struct {
|
|
|
@@ -86,7 +88,7 @@ func (c *Context) GetHeader() *Header {
|
|
|
func (c *Context) GetBytesBody() (*BytesBody, error) {
|
|
|
body, exist := c.Get(bodyKey)
|
|
|
if !exist {
|
|
|
- bytesBody, err := c.readOriginBody()
|
|
|
+ bytesBody, err := c.ReadOriginBody()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -128,7 +130,7 @@ func (c *Context) GetBytesBody() (*BytesBody, error) {
|
|
|
func (c *Context) GetJsonBody() (*JsonBody, error) {
|
|
|
body, exist := c.Get(bodyKey)
|
|
|
if !exist {
|
|
|
- bytesBody, err := c.readOriginBody()
|
|
|
+ bytesBody, err := c.ReadOriginBody()
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
@@ -179,7 +181,7 @@ func (c *Context) GetQueryParams() *QueryPrams {
|
|
|
if !exist {
|
|
|
return &QueryPrams{
|
|
|
c: c,
|
|
|
- queryParams: c.getAllQueryParams(),
|
|
|
+ queryParams: c.GetOriginQueryParams(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -199,7 +201,7 @@ func (c *Context) GetPathParams() *PathPrams {
|
|
|
if !exist {
|
|
|
return &PathPrams{
|
|
|
c: c,
|
|
|
- pathParams: c.getAllPathParams(),
|
|
|
+ pathParams: c.GetOriginPathParams(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -209,6 +211,64 @@ func (c *Context) GetPathParams() *PathPrams {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// ReadOriginBody 获取上下文中的原始字节Body
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的原始字节Body
|
|
|
+// - 错误
|
|
|
+func (c *Context) ReadOriginBody() ([]byte, error) {
|
|
|
+ if c.Request.Body == nil {
|
|
|
+ return make([]byte, 0), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ body, err := io.ReadAll(c.Request.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, errors.New(err.Error())
|
|
|
+ }
|
|
|
+
|
|
|
+ defer func(Body io.ReadCloser) {
|
|
|
+ err := Body.Close()
|
|
|
+ if err != nil {
|
|
|
+ logger.GetInstance().Error(errors.New(err.Error()))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }(c.Request.Body)
|
|
|
+
|
|
|
+ c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
|
|
+
|
|
|
+ c.Set(bodyKey, body)
|
|
|
+
|
|
|
+ return body, nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetOriginQueryParams 获取上下文中的原始查询参数
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的原始查询参数
|
|
|
+func (c *Context) GetOriginQueryParams() map[string]string {
|
|
|
+ queryParams := make(map[string]string)
|
|
|
+
|
|
|
+ for key, values := range c.Request.URL.Query() {
|
|
|
+ queryParams[key] = strings.Join(values, ",")
|
|
|
+ }
|
|
|
+
|
|
|
+ return queryParams
|
|
|
+}
|
|
|
+
|
|
|
+// GetOriginPathParams 获取上下文中的原始路径参数
|
|
|
+// 参数: 无
|
|
|
+// 返回值:
|
|
|
+// - 上下文中的原始路径参数
|
|
|
+func (c *Context) GetOriginPathParams() map[string]string {
|
|
|
+ pathParams := make(map[string]string)
|
|
|
+
|
|
|
+ for _, params := range c.Params {
|
|
|
+ pathParams[params.Key] = params.Value
|
|
|
+ }
|
|
|
+
|
|
|
+ return pathParams
|
|
|
+}
|
|
|
+
|
|
|
type Header struct {
|
|
|
c *Context
|
|
|
header map[string]string
|
|
|
@@ -448,56 +508,6 @@ func (pathParams *PathPrams) Map() map[string]string {
|
|
|
return pathParams.pathParams
|
|
|
}
|
|
|
|
|
|
-func (c *Context) getAllQueryParams() map[string]string {
|
|
|
- queryParams := make(map[string]string)
|
|
|
-
|
|
|
- for key, values := range c.Request.URL.Query() {
|
|
|
- queryParams[key] = strings.Join(values, ",")
|
|
|
- }
|
|
|
-
|
|
|
- return queryParams
|
|
|
-}
|
|
|
-
|
|
|
-func (c *Context) getAllPathParams() map[string]string {
|
|
|
- pathParams := make(map[string]string)
|
|
|
-
|
|
|
- for _, params := range c.Params {
|
|
|
- pathParams[params.Key] = params.Value
|
|
|
- }
|
|
|
-
|
|
|
- return pathParams
|
|
|
-}
|
|
|
-
|
|
|
-func (c *Context) readOriginBody() ([]byte, error) {
|
|
|
- if c.Request.Body == nil {
|
|
|
- return make([]byte, 0), nil
|
|
|
- }
|
|
|
-
|
|
|
- body, err := io.ReadAll(c.Request.Body)
|
|
|
- if err != nil {
|
|
|
- return nil, errors.New(err.Error())
|
|
|
- }
|
|
|
-
|
|
|
- defer func(Body io.ReadCloser) {
|
|
|
- err := Body.Close()
|
|
|
- if err != nil {
|
|
|
- logger.GetInstance().Error(errors.New(err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
- }(c.Request.Body)
|
|
|
-
|
|
|
- c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
|
|
-
|
|
|
- c.Set(bodyKey, body)
|
|
|
-
|
|
|
- return body, nil
|
|
|
-}
|
|
|
-
|
|
|
-const (
|
|
|
- tenantInfoKey = "context-tenant-info"
|
|
|
- userInfoKey = "context-user-info"
|
|
|
-)
|
|
|
-
|
|
|
type TenantInfo interface {
|
|
|
GetID() string
|
|
|
GetName() string
|