From 376976b8afd46e2829d8ad7c29c63f785ce3b452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 13 Nov 2024 11:24:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96requestContext=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- define/context.go | 135 +++++++++++++++++++++++++--------------------- 1 file changed, 75 insertions(+), 60 deletions(-) diff --git a/define/context.go b/define/context.go index 34095d4..525abd7 100644 --- a/define/context.go +++ b/define/context.go @@ -23,34 +23,50 @@ import ( type RequestContext struct { ctx context.Context `json:"-"` // gin 上下文 runtimeInstance *trace.Runtime `json:"-"` // 链路统一追踪实例 + lock *sync.RWMutex `json:"-"` // 数据锁 GatewayUrlConfig *GatewayApiConfig `json:"gateway_url_config"` // 网关接口的配置 GatewayApiInfo *GatewayApi `json:"gateway_api_info"` // 网关接口信息 GatewayRequest *GatewayRequestInfo `json:"gateway_request"` // 网关请求信息 BackendApiResultTable map[string]*httpclientDefine.Response `json:"backend_api_result_table"` // 后端接口返回数据详细信息: 接口别名 => 请求结果 - AppCode string `json:"app_code"` // 传入的APP Code - Sign string `json:"sign"` // 网关接口的签名 AppApiPermission *AppApiPermission `json:"app_api_permission"` // 应用接口权限 AppApiResultPermissionList []*AppApiResultPermission `json:"app_api_result_permission_list"` // 应用接口返回值权限 - Lock *sync.RWMutex `json:"-"` // 数据锁 AppRateBehavior RateBehavior `json:"app_rate_behavior"` // 应用没有配置流控限制的行为 ProjectRateLimit map[string]*RateLimitConfig `json:"project_rate_behavior"` // 项目流控行为 ResponseData *ResponseData `json:"response_data"` // 网关响应数据 } +func (rc *RequestContext) Lock() { + rc.lock.Lock() +} + +func (rc *RequestContext) Unlock() { + rc.lock.Unlock() +} + +func (rc *RequestContext) RLock() { + rc.lock.RLock() +} + +func (rc *RequestContext) RUnlock() { + rc.lock.RUnlock() +} + +// GatewayApiConfig 网关接口配置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:41 2024/11/13 type GatewayApiConfig struct { - RateLimit *RateLimit `json:"rate_limit"` // 网关接口流控规则 - GatewayApiID int64 `json:"gateway_api_id"` // 网关接口ID - Uri string `json:"uri"` // 网关接口ID - ContentType string `json:"content_type"` // 请求类型 - Method string `json:"method"` // 请求方法 - Version string `json:"version"` // 接口版本 - Name string `json:"name"` // 接口名称 - Status string `json:"status"` // 状态 - ParamList []*GatewayApiDetailParamItem `json:"param_list"` // 参数列表 - ResultList []*GatewayApiDetailResultItem `json:"result_list"` // 返回值列表 - ProjectApiTable map[int64]*GatewayApiDetailProjectApiItem `json:"project_api_table"` // 项目接口列表 - RequestGroup [][]*GatewayApiDetailRequestGroupItem `json:"request_group"` // 请求分组 - RequestRewriteTable map[string][]*GatewayApiDetailRequestRewriteItem `json:"request_rewrite_table"` // 请求重写规则, 项目接口别名ID => 重写规则列表 + RateLimit *RateLimit `json:"rate_limit"` // 网关接口流控规则 + Uri string `json:"uri"` // 网关接口ID + ContentType string `json:"content_type"` // 请求类型 + Method string `json:"method"` // 请求方法 + Version string `json:"version"` // 接口版本 + ParamList []*Param `json:"param_list"` // 参数列表 + ResultList []*Result `json:"result_list"` // 返回值列表 + ServiceApiTable map[string]*ServiceApiConfig `json:"service_api_table"` // 服务接口列表 + RequestGroup [][]*RequestGroupItem `json:"request_group"` // 请求分组 + RequestRewriteTable map[string][]*RequestRewriteItem `json:"request_rewrite_table"` // 请求重写规则, 项目接口别名ID => 重写规则列表 } type RateLimit struct { @@ -84,42 +100,38 @@ type GatewayApiDetailProjectApiResultItem struct { ResultType string `json:"result_type"` // 返回值类型 } -// GatewayApiDetailRequestGroupItem ... +// RequestGroupItem 请求接口配置 // // Author : go_developer@163.com<白茶清欢> // -// Date : 17:58 2024/5/13 -type GatewayApiDetailRequestGroupItem struct { - ProjectID int64 `json:"project_id"` // 项目ID - ProjectFlag string `json:"project_flag"` // 项目标识 - ProjectApiID int64 `json:"project_api_id"` // 项目接口ID - ProjectApiAlias string `json:"project_api_alias"` // 项目接口别名 - ProjectApiAliasID int64 `json:"project_api_alias_id"` // 项目接口别名ID +// Date : 11:10 2024/11/13 +type RequestGroupItem struct { + ServiceFlag string `json:"service_flag"` // 服务标识 + ServiceApiFlag string `json:"service_api_flag"` // 服务接口标识 + ServiceApiAlias string `json:"service_api_alias"` // 服务接口别名 + ServiceApiConfig *ServiceApiConfig `json:"service_api_config"` // 服务接口配置 } -// GatewayApiDetailRequestRewriteItem 请求重写参数信息表 +// RequestRewriteItem 请求转换规则配置 // // Author : go_developer@163.com<白茶清欢> // -// Date : 18:02 2024/5/13 -type GatewayApiDetailRequestRewriteItem struct { - SourceProjectApiAlias string `json:"source_project_api_alias"` // 数据源项目接口别名 __GATEWAY__ 代表从网关读取 - SourceProjectID int64 `json:"source_project_id"` // 数据源项目ID - SourceProjectApiID int64 `json:"source_project_api_id"` // 数据源项目接口ID - SourceProjectApiResultID int64 `json:"source_project_api_result_id"` // 数据源接口返回值ID, SourceProjectApiAlias = __GATEWAY__ 代表从网关读取 - TargetProjectApiAlias string `json:"target_project_api_alias"` // 目标接口接口别名 - TargetProjectID int64 `json:"target_project_id"` // 目标项目ID - TargetProjectApiID int64 `json:"target_project_api_id"` // 目标项目接口ID - TargetProjectApiParamID int64 `json:"target_project_api_param_id"` // 目标项目接口参数ID +// Date : 11:16 2024/11/13 +type RequestRewriteItem struct { + SourceServiceApiAlias string `json:"source_service_api_alias"` // 数据源项目接口别名 __GATEWAY__ 代表从网关读取 + SourceServiceApiResultLocation string `json:"source_service_api_result_location"` // 数据源接口返回值位置 + SourceServiceApiResultPath string `json:"source_service_api_result_path"` // 数据源接口返回值路径, SourceServiceApiAlias = __GATEWAY__ 代表从网关读取 + TargetServiceApiAlias string `json:"target_service_api_alias"` // 目标接口接口别名 + TargetServiceApiParamLocation string `json:"target_service_api_param_location"` // 目标接口的参数位置 + TargetServiceApiParamPath string `json:"target_service_api_param_path"` // 目标接口的参数路径 } -// GatewayApiDetailResultItem 网关响应信息表 +// Result 网关响应信息表 // // Author : go_developer@163.com<白茶清欢> // -// Date : 18:03 2024/5/13 -type GatewayApiDetailResultItem struct { - ID int64 `json:"id"` // 网关接口返回值ID +// Date : 11:21 2024/11/13 +type Result struct { SourceProjectApiAlias string `json:"source_project_api_alias"` // 数据源项目接口别名 __GATEWAY__ 代表从网关读取 SourceDataLocation string `json:"source_data_location"` // 数据源数据位置 SourceDataPath string `json:"source_data_path"` // 数据源数据路径 @@ -129,27 +141,31 @@ type GatewayApiDetailResultItem struct { OutputDataType string `json:"output_data_type"` // 网关输出数据类型 } -type GatewayApiDetailParamItem struct { - ID int64 `json:"id"` // 参数ID +// Param 网关接口参数数据结构 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 11:20 2024/11/13 +type Param struct { Location string `json:"location"` // 参数位置 - Path string `json:"path"` // 参数名称 + Path string `json:"path"` // 参数路径 ParamType string `json:"param_type"` // 参数类型 DefaultValue string `json:"default_value"` // 默认值 IsRequired bool `json:"is_required"` // 是否必传 AllowEmpty bool `json:"allow_empty"` // 空字符串是否为有效参数 AllowZero bool `json:"allow_zero"` // 数字类型, 0 是否为有效参数 AllowNil bool `json:"allow_nil"` // nil 是否为有效参数 + ValidateRule any `json:"validate_rule"` // 校验规则 : TODO : 预留 } -// GatewayApiDetailProjectApiItem 项目接口规则 +// ServiceApiConfig 服务接口配置 // // Author : go_developer@163.com<白茶清欢> // // Date : 17:44 2024/5/13 -type GatewayApiDetailProjectApiItem struct { - ProjectFlag string `json:"project_flag"` // 项目标识 - ProjectID int64 `json:"project_id"` // 项目ID - ProjectApiID int64 `json:"project_api_id"` // 项目接口ID +type ServiceApiConfig struct { + ServiceFlag string `json:"service_flag"` // 服务标识标识 + ServiceApiFlag string `json:"service_api_flag"` // 服务接口标识 CacheEnable bool `json:"cache_enable"` // 缓存是否可用 CacheInstanceID int64 `json:"cache_instance_id"` // 缓存可用的情况下, 缓存实例ID CacheConfig *ApiCacheConfig `json:"cache_config"` // 缓存配置 @@ -180,19 +196,18 @@ type ApiCacheConfig struct { } type GatewayApiDetail struct { - RateLimit *RateLimit `json:"rate_limit"` // 网关接口流控规则 - GatewayApiID int64 `json:"gateway_api_id"` // 网关接口ID - Uri string `json:"uri"` // 网关接口ID - ContentType string `json:"content_type"` // 请求类型 - Method string `json:"method"` // 请求方法 - Version string `json:"version"` // 接口版本 - Name string `json:"name"` // 接口名称 - Status string `json:"status"` // 状态 - ParamList []*GatewayApiDetailParamItem `json:"param_list"` // 参数列表 - ResultList []*GatewayApiDetailResultItem `json:"result_list"` // 返回值列表 - ProjectApiTable map[int64]*GatewayApiDetailProjectApiItem `json:"project_api_table"` // 项目接口列表 - RequestGroup [][]*GatewayApiDetailRequestGroupItem `json:"request_group"` // 请求分组 - RequestRewriteTable map[string][]*GatewayApiDetailRequestRewriteItem `json:"request_rewrite_table"` // 请求重写规则, 项目接口别名ID => 重写规则列表 + RateLimit *RateLimit `json:"rate_limit"` // 网关接口流控规则 + GatewayApiID int64 `json:"gateway_api_id"` // 网关接口ID + Uri string `json:"uri"` // 网关接口ID + ContentType string `json:"content_type"` // 请求类型 + Method string `json:"method"` // 请求方法 + Version string `json:"version"` // 接口版本 + Name string `json:"name"` // 接口名称 + Status string `json:"status"` // 状态 + ParamList []*Param `json:"param_list"` // 参数列表 + ResultList []*Result `json:"result_list"` // 返回值列表 + ProjectApiTable map[int64]*ServiceApiConfig `json:"project_api_table"` // 项目接口列表 + RequestRewriteTable map[string][]*RequestRewriteItem `json:"request_rewrite_table"` // 请求重写规则, 项目接口别名ID => 重写规则列表 } type GatewayApi struct {