4fb1016977
iotlcm-sms:独立短信 HTTP/gRPC 服务,支持阿里云与腾讯云。 - Go module 迁移至 git.5iot.com/iotlcm-public/iotlcm-sms - 补 Apache-2.0 LICENSE,重写 README(特性/配置/接口/部署/目录/测试) - 配置改为 config.example.yaml + SMS_* 环境变量注入,仓库不含真实凭证 - 清理内部标识(私有镜像仓库 / 公司名 / 客户签名 / 真实模板ID / 真实手机号) - 统一 LF 行尾并加 .gitattributes
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package grpcapi
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"git.5iot.com/iotlcm-public/iotlcm-sms/api/pb"
|
|
"git.5iot.com/iotlcm-public/iotlcm-sms/internal/sms"
|
|
)
|
|
|
|
type Sender interface {
|
|
Send(context.Context, sms.Config, sms.Message) (sms.Result, error)
|
|
}
|
|
|
|
type Server struct {
|
|
pb.UnimplementedNoticeServer
|
|
sender Sender
|
|
}
|
|
|
|
func NewServer(sender Sender) *Server {
|
|
return &Server{sender: sender}
|
|
}
|
|
|
|
func (s *Server) Send(ctx context.Context, request *pb.SendReq) (*pb.SendRes, error) {
|
|
if request == nil {
|
|
return nil, status.Error(codes.InvalidArgument, "请求不能为空")
|
|
}
|
|
|
|
var config sms.Config
|
|
if err := json.Unmarshal(request.GetConfig(), &config); err != nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "config JSON 无效: %v", err)
|
|
}
|
|
var message sms.Message
|
|
if err := json.Unmarshal(request.GetParams(), &message); err != nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "params JSON 无效: %v", err)
|
|
}
|
|
|
|
result, _ := s.sender.Send(ctx, config, message)
|
|
responseHead := &pb.HeadRes{
|
|
Code: int32(result.Code),
|
|
Message: result.Message,
|
|
}
|
|
if request.Head != nil {
|
|
responseHead.RouterId = request.Head.RouterId
|
|
responseHead.TraceId = request.Head.TraceId
|
|
}
|
|
return &pb.SendRes{Head: responseHead}, nil
|
|
}
|