feat: add support

This commit is contained in:
zzw
2026-09-24 10:38:00 +08:00
parent 9b876701c3
commit cb16071b20
19 changed files with 901 additions and 1 deletions
+80
View File
@@ -0,0 +1,80 @@
package ccc
import (
"encoding/json"
"fmt"
"strings"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
openapiutil "github.com/alibabacloud-go/openapi-util/service"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
)
const defaultEndpoint = "ccc.cn-shanghai.aliyuncs.com"
func createClient(accessKeyId, accessKeySecret, endpoint string) (*openapi.Client, error) {
if endpoint == "" {
endpoint = defaultEndpoint
}
cfg := &openapi.Config{
AccessKeyId: tea.String(accessKeyId),
AccessKeySecret: tea.String(accessKeySecret),
Endpoint: tea.String(endpoint),
}
return openapi.NewClient(cfg)
}
func createApiInfo(action, version string) *openapi.Params {
return &openapi.Params{
Action: tea.String(action),
Version: tea.String(version),
Protocol: tea.String("HTTPS"),
Method: tea.String("POST"),
AuthType: tea.String("AK"),
Style: tea.String("RPC"),
Pathname: tea.String("/"),
ReqBodyType: tea.String("json"),
BodyType: tea.String("json"),
}
}
func capitalizeKey(key string) string {
if key == "" {
return key
}
return strings.ToUpper(key[:1]) + key[1:]
}
func Call(action, version, requestJSON, accessKeyId, accessKeySecret, endpoint string) (any, error) {
client, err := createClient(accessKeyId, accessKeySecret, endpoint)
if err != nil {
return nil, err
}
requestStr := strings.TrimSpace(requestJSON)
if requestStr == "" {
requestStr = "{}"
}
var requestStruct map[string]any
if err = json.NewDecoder(strings.NewReader(requestStr)).Decode(&requestStruct); err != nil {
return nil, fmt.Errorf("request json: %w", err)
}
queries := make(map[string]any, len(requestStruct))
for key, value := range requestStruct {
queries[capitalizeKey(key)] = value
}
res, err := client.CallApi(createApiInfo(action, version), &openapi.OpenApiRequest{
Query: openapiutil.Query(queries),
}, &util.RuntimeOptions{})
if err != nil {
return nil, err
}
if res == nil {
return nil, nil
}
return res["body"], nil
}
@@ -0,0 +1,13 @@
package config
type Config struct {
Nacos NacosConf
}
type NacosConf struct {
Hosts []string
NamespaceId string `json:",optional"`
Group string `json:",optional"`
RegisterIP string `json:",optional"`
ConfigID string `json:",optional"`
}
@@ -0,0 +1,43 @@
package logic
import (
"context"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
support "lone-services/rpc/support/pb"
"lone-services/services/support/internal/ccc"
"lone-services/services/support/internal/svc"
"lone-services/services/support/validator"
"github.com/zeromicro/go-zero/core/logx"
)
type CallLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewCallLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CallLogic {
return &CallLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *CallLogic) Call(in *support.CallReq) (*support.Response, error) {
var req validator.CallValidator
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
endpoint := utils.GetConfigString("ccc.endpoint")
body, err := ccc.Call(req.Action, req.Version, req.Request, req.A, req.S, endpoint)
if err != nil {
l.Errorf("ccc call action=%s version=%s: %v", req.Action, req.Version, err)
return outResponse(utils.Fail, err.Error()), nil
}
return okResponse(body), nil
}
@@ -0,0 +1,31 @@
package logic
import (
"lone-services/pkg/utils"
support "lone-services/rpc/support/pb"
jsoniter "github.com/json-iterator/go"
)
func okResponse(data any) *support.Response {
buf, _ := jsoniter.Marshal(data)
return &support.Response{
Code: utils.Ok.Code,
Msg: utils.Ok.Msg,
Data: string(buf),
}
}
func failResponse(status utils.Status) *support.Response {
return &support.Response{
Code: status.Code,
Msg: status.Msg,
}
}
func outResponse(status utils.Status, msg string) *support.Response {
return &support.Response{
Code: status.Code,
Msg: msg,
}
}
@@ -0,0 +1,25 @@
package server
import (
"context"
support "lone-services/rpc/support/pb"
"lone-services/services/support/internal/logic"
"lone-services/services/support/internal/svc"
)
type SupportServer struct {
svcCtx *svc.ServiceContext
support.UnimplementedSupportServer
}
func NewSupportServer(svcCtx *svc.ServiceContext) *SupportServer {
return &SupportServer{
svcCtx: svcCtx,
}
}
func (s *SupportServer) Call(ctx context.Context, in *support.CallReq) (*support.Response, error) {
l := logic.NewCallLogic(ctx, s.svcCtx)
return l.Call(in)
}
@@ -0,0 +1,22 @@
package svc
import (
"lone-services/pkg/utils"
"lone-services/services/support/internal/config"
"gorm.io/gorm"
)
type ServiceContext struct {
Config config.Config
DB *gorm.DB
Prefix string
}
func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext {
return &ServiceContext{
Config: c,
DB: db,
Prefix: utils.GetConfigString("mysql.prefix"),
}
}