|
|
@@ -11,6 +11,12 @@ import (
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ bodyKey = "body-context"
|
|
|
+ queryParamsKey = "query-params-context"
|
|
|
+ pathParamsKey = "path-params-context"
|
|
|
+)
|
|
|
+
|
|
|
type Context struct {
|
|
|
*gin.Context
|
|
|
}
|
|
|
@@ -48,61 +54,179 @@ func (c *Context) GetHeaders() map[string]string {
|
|
|
return headers
|
|
|
}
|
|
|
|
|
|
-func (c *Context) ReadBody() ([]byte, error) {
|
|
|
- if c.Request.Body == nil {
|
|
|
- return make([]byte, 0), nil
|
|
|
+type CacheBody struct {
|
|
|
+ c *Context
|
|
|
+ bytesBody []byte
|
|
|
+}
|
|
|
+
|
|
|
+func (cacheBody *CacheBody) Set(bytesBody []byte) {
|
|
|
+ cacheBody.bytesBody = bytesBody
|
|
|
+ cacheBody.c.Set(bodyKey, cacheBody.bytesBody)
|
|
|
+}
|
|
|
+
|
|
|
+func (cacheBody *CacheBody) Bytes() []byte {
|
|
|
+ return cacheBody.bytesBody
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Context) GetBytesBody() (*CacheBody, error) {
|
|
|
+ body, exist := c.Get(bodyKey)
|
|
|
+ if !exist {
|
|
|
+ bytesBody, err := c.readOriginBody()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return &CacheBody{
|
|
|
+ c: c,
|
|
|
+ bytesBody: bytesBody,
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
- body, err := io.ReadAll(c.Request.Body)
|
|
|
+ return &CacheBody{
|
|
|
+ c: c,
|
|
|
+ bytesBody: body.([]byte),
|
|
|
+ }, nil
|
|
|
+}
|
|
|
+
|
|
|
+type JsonBody struct {
|
|
|
+ c *Context
|
|
|
+ jsonBodyMap map[string]any
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Set(key string, value any) {
|
|
|
+ jsonBody.jsonBodyMap[key] = value
|
|
|
+ jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Delete(key string) {
|
|
|
+ delete(jsonBody.jsonBodyMap, key)
|
|
|
+ jsonBody.c.Set(bodyKey, jsonBody.jsonBodyMap)
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Get(key string) any {
|
|
|
+ return jsonBody.jsonBodyMap[key]
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Map() map[string]any {
|
|
|
+ return jsonBody.jsonBodyMap
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Bytes() ([]byte, error) {
|
|
|
+ return json.Marshal(jsonBody.jsonBodyMap)
|
|
|
+}
|
|
|
+
|
|
|
+func (jsonBody *JsonBody) Unmarshal(output any) error {
|
|
|
+ jsonBytes, err := jsonBody.Bytes()
|
|
|
if err != nil {
|
|
|
- return nil, errors.New(err.Error())
|
|
|
+ return err
|
|
|
}
|
|
|
|
|
|
- defer func(Body io.ReadCloser) {
|
|
|
- err := Body.Close()
|
|
|
+ return json.Unmarshal(jsonBytes, output)
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Context) GetJsonBody() (*JsonBody, error) {
|
|
|
+ body, exist := c.Get(bodyKey)
|
|
|
+ if !exist {
|
|
|
+ bytesBody, err := c.readOriginBody()
|
|
|
if err != nil {
|
|
|
- logger.GetInstance().Error(errors.New(err.Error()))
|
|
|
- return
|
|
|
+ return nil, err
|
|
|
}
|
|
|
- }(c.Request.Body)
|
|
|
|
|
|
- c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
|
|
+ jsonBodyMap := make(map[string]any)
|
|
|
+ err = json.Unmarshal(bytesBody, &jsonBodyMap)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
|
|
|
- return body, nil
|
|
|
+ return &JsonBody{
|
|
|
+ c: c,
|
|
|
+ jsonBodyMap: jsonBodyMap,
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ return &JsonBody{
|
|
|
+ c: c,
|
|
|
+ jsonBodyMap: body.(map[string]any),
|
|
|
+ }, nil
|
|
|
}
|
|
|
|
|
|
-func (c *Context) ReplaceBody(body []byte) error {
|
|
|
- if c.Request.Body != nil {
|
|
|
- err := c.Request.Body.Close()
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
+type QueryPrams struct {
|
|
|
+ c *Context
|
|
|
+ queryParams map[string]string
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *QueryPrams) Set(key string, value string) {
|
|
|
+ queryParams.queryParams[key] = value
|
|
|
+ queryParams.c.Set(queryParamsKey, queryParams.queryParams)
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *QueryPrams) Delete(key string) {
|
|
|
+ delete(queryParams.queryParams, key)
|
|
|
+ queryParams.c.Set(queryParamsKey, queryParams.queryParams)
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *QueryPrams) Get(key string) string {
|
|
|
+ return queryParams.queryParams[key]
|
|
|
+}
|
|
|
+
|
|
|
+func (queryParams *QueryPrams) Map() map[string]string {
|
|
|
+ return queryParams.queryParams
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Context) GetQueryParams() *QueryPrams {
|
|
|
+ queryParams, exist := c.Get(queryParamsKey)
|
|
|
+ if !exist {
|
|
|
+ return &QueryPrams{
|
|
|
+ c: c,
|
|
|
+ queryParams: c.getAllQueryParams(),
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
|
|
|
+ return &QueryPrams{
|
|
|
+ c: c,
|
|
|
+ queryParams: queryParams.(map[string]string),
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- return nil
|
|
|
+type PathPrams struct {
|
|
|
+ c *Context
|
|
|
+ pathParams map[string]string
|
|
|
}
|
|
|
|
|
|
-func (c *Context) ReadJsonBody(output any) error {
|
|
|
- body, err := c.ReadBody()
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
+func (pathParams *PathPrams) Set(key string, value string) {
|
|
|
+ pathParams.pathParams[key] = value
|
|
|
+ pathParams.c.Set(pathParamsKey, pathParams.pathParams)
|
|
|
+}
|
|
|
|
|
|
- return json.Unmarshal(body, output)
|
|
|
+func (pathParams *PathPrams) Delete(key string) {
|
|
|
+ delete(pathParams.pathParams, key)
|
|
|
+ pathParams.c.Set(pathParamsKey, pathParams.pathParams)
|
|
|
}
|
|
|
|
|
|
-func (c *Context) ReplaceJsonBody(body any) error {
|
|
|
- bodyBytes, err := json.Marshal(body)
|
|
|
- if err != nil {
|
|
|
- return errors.New(err.Error())
|
|
|
+func (pathParams *PathPrams) Get(key string) string {
|
|
|
+ return pathParams.pathParams[key]
|
|
|
+}
|
|
|
+
|
|
|
+func (pathParams *PathPrams) Map() map[string]string {
|
|
|
+ return pathParams.pathParams
|
|
|
+}
|
|
|
+
|
|
|
+func (c *Context) GetPathParams() *PathPrams {
|
|
|
+ pathParams, exist := c.Get(pathParamsKey)
|
|
|
+ if !exist {
|
|
|
+ return &PathPrams{
|
|
|
+ c: c,
|
|
|
+ pathParams: c.getAllPathParams(),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- return c.ReplaceBody(bodyBytes)
|
|
|
+ return &PathPrams{
|
|
|
+ c: c,
|
|
|
+ pathParams: pathParams.(map[string]string),
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-func (c *Context) GetAllQueryParams() map[string]string {
|
|
|
+func (c *Context) getAllQueryParams() map[string]string {
|
|
|
queryParams := make(map[string]string, 0)
|
|
|
|
|
|
for key, values := range c.Request.URL.Query() {
|
|
|
@@ -112,7 +236,7 @@ func (c *Context) GetAllQueryParams() map[string]string {
|
|
|
return queryParams
|
|
|
}
|
|
|
|
|
|
-func (c *Context) GetAllPathParams() map[string]string {
|
|
|
+func (c *Context) getAllPathParams() map[string]string {
|
|
|
pathParams := make(map[string]string, 0)
|
|
|
|
|
|
for _, params := range c.Params {
|
|
|
@@ -122,6 +246,31 @@ func (c *Context) GetAllPathParams() map[string]string {
|
|
|
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"
|