Files
iotlcm/internal/logic/upstreamimpl/sim_dx.go
T
MiFaZhan 82466d587d chore: 初始开源发布
iotlcm-upstream-proxy:渠道缓存与转发网关。

- 内联原私有依赖:yunze-public → internal/pub/(18 文件),
  yunze-pb/pb → internal/pb/(9 文件);Consul 注册改用官方
  gogf/gf/contrib/registry/consul/v2(API 兼容)
- Go module:yz-svr → git.5iot.com/iotlcm-public/iotlcm-upstream-proxy
- 凭据与内部标识脱敏:MySQL/Redis/Consul 凭据、私网与生产 IP、内网域名
- 移除内部资产:CI 触发器
- Dockerfile 改用公共基础镜像,移除写死的 registry 凭据
- 补 Apache-2.0 LICENSE,重写 README,LF 行尾 + .gitattributes
2026-10-22 16:08:08 +08:00

96 lines
2.6 KiB
Go

package upstreamimpl
import (
"context"
"fmt"
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/logic/upstreamimpl/dx"
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-upstream-proxy/internal/service"
"regexp"
"time"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/gsvc"
)
// XML命名空间正则
var xmlNamespaceRegex = regexp.MustCompile(`xmlns[^=]*="[^"]*"\s*|:web:`)
type sSimDX struct {
channelConfig *model.SimChannelConfigItemCache
client *dx.Client
}
func NewSimDX() *sSimDX {
return &sSimDX{}
}
func init() {
s := NewSimDX()
service.UpstreamAPI().RegisterTemp("sim_dx_v1", s)
service.UpstreamAPI().RegisterTemp("sim_dx_v3", s)
service.RegisterSimDX(s)
}
func (s *sSimDX) NewTemp(ctx context.Context, channelConfig *model.SimChannelConfigItemCache) model.SimCardChannelI {
client := dx.NewClient(dx.Config{
BaseURL: dx.DefaultBaseURL,
AppKey: channelConfig.Channel.AppKey,
AppSecret: channelConfig.Channel.AppSecret,
})
return &sSimDX{
channelConfig: channelConfig,
client: client,
}
}
func (s *sSimDX) getUpstreamUrl(ctx context.Context, serviceUrl string) (string, error) {
address, err := g.Cfg().Get(ctx, "gateway.address")
if err != nil {
return "", err
}
return fmt.Sprintf("http://%s%s", address, serviceUrl), nil
// return fmt.Sprintf("http://%s%s", "127.0.0.1:8110", serviceUrl), nil
}
func (s *sSimDX) getUpstreamTimeout(ctx context.Context) time.Duration {
timeout, _ := g.Cfg().Get(ctx, "gateway.timeout")
if timeout == nil {
return 10 * time.Second
}
if d := timeout.Duration(); d > 0 {
return d
}
if seconds := timeout.Int(); seconds > 0 {
return time.Duration(seconds) * time.Second
}
return 10 * time.Second
}
func (s *sSimDX) SimBase(ctx context.Context, input *model.SimBaseReqJson) (model.SimBaseResJson, error) {
r := g.RequestFromCtx(ctx)
if r.Method != "POST" {
return nil, gerror.New("method not allowed")
}
path := r.URL.Path
if path == "" {
return nil, gerror.New("path is required")
}
url, err := s.getUpstreamUrl(ctx, path)
if err != nil {
g.Log().Debugf(ctx, "SimBase getUpstreamUrl error %+v, ", err)
return nil, err
}
client := g.Client()
client.SetDiscovery(gsvc.GetRegistry())
client.SetTimeout(s.getUpstreamTimeout(ctx) * time.Duration(len(input.SimKeys)))
resp, err := client.Post(ctx, url, input)
if err != nil {
g.Log().Errorf(ctx, "SimBase post error %+v,url %s, params %+v, ", err, url, input)
return nil, err
}
return readGatewayResponse(resp)
}