Version: 0.9.68.dev.260504

后端:
1. 阶段 3 notification 服务边界落地,新增 `cmd/notification`、`services/notification`、`gateway/notification`、`shared/contracts/notification` 和 notification port,按 userauth 同款最小手搓 zrpc 样板收口
2. notification outbox consumer、relay 和 retry loop 迁入独立服务入口,处理 `notification.feishu.requested`,gateway 改为通过 zrpc client 调用 notification
3. 清退旧单体 notification DAO/model/service/provider/runner 和 `service/events/notification_feishu.go`,旧实现不再作为活跃编译路径
4. 修复 outbox 路由归属、dispatch 启动扫描、Kafka topic 探测/投递超时、sending 租约恢复、毒消息 MarkDead 错误回传和 RPC timeout 边界
5. 同步调整 active-scheduler 触发通知事件、核心 outbox handler、MySQL 迁移边界和 notification 配置

文档:
1. 更新微服务迁移计划,将阶段 3 notification 标记为已完成,并明确下一阶段从 active-scheduler 开始
This commit is contained in:
Losita
2026-05-04 18:40:39 +08:00
parent 9742dc8b1c
commit abe3b4960e
41 changed files with 2178 additions and 889 deletions

View File

@@ -0,0 +1,76 @@
package rpc
import (
"errors"
"log"
"strings"
"github.com/LoveLosita/smartflow/backend/respond"
"google.golang.org/genproto/googleapis/rpc/errdetails"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
const notificationErrorDomain = "smartflow.notification"
// grpcErrorFromServiceError 负责把 notification 内部错误收口成 gRPC status。
//
// 职责边界:
// 1. 只负责把本服务内部的 respond.Response / 普通 error 转成 gRPC 可传输错误;
// 2. 不负责决定 HTTP 语义,也不负责写回前端响应体;
// 3. 上层 handler 只要直接 return 这个结果,就能让 client 侧按 `res, err :=` 的方式接收。
func grpcErrorFromServiceError(err error) error {
if err == nil {
return nil
}
var resp respond.Response
if errors.As(err, &resp) {
return grpcErrorFromResponse(resp)
}
log.Printf("notification rpc internal error: %v", err)
return status.Error(codes.Internal, "notification service internal error")
}
// grpcErrorFromResponse 负责把项目内业务响应映射成 gRPC status。
//
// 职责边界:
// 1. 只处理 notification 这组响应码到 gRPC code 的映射;
// 2. 业务码和业务文案通过 ErrorInfo 附带,方便 gateway 再反解回 respond.Response
// 3. 失败时退化为普通 gRPC status不阻断请求链路。
func grpcErrorFromResponse(resp respond.Response) error {
code := grpcCodeFromRespondStatus(resp.Status)
message := strings.TrimSpace(resp.Info)
if message == "" {
message = strings.TrimSpace(resp.Status)
}
st := status.New(code, message)
detail := &errdetails.ErrorInfo{
Domain: notificationErrorDomain,
Reason: resp.Status,
Metadata: map[string]string{
"info": resp.Info,
},
}
withDetails, err := st.WithDetails(detail)
if err != nil {
return st.Err()
}
return withDetails.Err()
}
func grpcCodeFromRespondStatus(statusValue string) codes.Code {
switch strings.TrimSpace(statusValue) {
case respond.MissingToken.Status, respond.InvalidToken.Status, respond.InvalidClaims.Status,
respond.ErrUnauthorized.Status, respond.WrongTokenType.Status, respond.UserLoggedOut.Status:
return codes.Unauthenticated
case respond.MissingParam.Status, respond.WrongParamType.Status, respond.ParamTooLong.Status:
return codes.InvalidArgument
}
if strings.HasPrefix(strings.TrimSpace(statusValue), "5") {
return codes.Internal
}
return codes.InvalidArgument
}

View File

@@ -0,0 +1,133 @@
package rpc
import (
"context"
"errors"
"time"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/LoveLosita/smartflow/backend/services/notification/rpc/pb"
notificationsv "github.com/LoveLosita/smartflow/backend/services/notification/sv"
contracts "github.com/LoveLosita/smartflow/backend/shared/contracts/notification"
)
type Handler struct {
pb.UnimplementedNotificationServer
svc *notificationsv.Service
}
func NewHandler(svc *notificationsv.Service) *Handler {
return &Handler{svc: svc}
}
// GetFeishuWebhook 负责把配置查询请求从 gRPC 协议转成内部服务调用。
//
// 职责边界:
// 1. 只做 transport -> service 的参数搬运,不碰 DAO/provider/outbox 细节;
// 2. 业务错误统一转成 gRPC status让 client 侧继续使用 `res, err :=`
// 3. 成功时只回传业务数据,不在 payload 里塞 status/info。
func (h *Handler) GetFeishuWebhook(ctx context.Context, req *pb.GetFeishuWebhookRequest) (*pb.ChannelResponse, error) {
if h == nil || h.svc == nil {
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
}
if req == nil {
return nil, grpcErrorFromServiceError(respond.MissingParam)
}
resp, err := h.svc.GetFeishuWebhook(ctx, int(req.UserId))
if err != nil {
return nil, grpcErrorFromServiceError(err)
}
return channelToPB(resp), nil
}
func (h *Handler) SaveFeishuWebhook(ctx context.Context, req *pb.SaveFeishuWebhookRequest) (*pb.ChannelResponse, error) {
if h == nil || h.svc == nil {
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
}
if req == nil {
return nil, grpcErrorFromServiceError(respond.MissingParam)
}
resp, err := h.svc.SaveFeishuWebhook(ctx, int(req.UserId), contracts.SaveFeishuWebhookRequest{
UserID: int(req.UserId),
Enabled: req.Enabled,
WebhookURL: req.WebhookUrl,
AuthType: req.AuthType,
BearerToken: req.BearerToken,
})
if err != nil {
return nil, grpcErrorFromServiceError(err)
}
return channelToPB(resp), nil
}
func (h *Handler) DeleteFeishuWebhook(ctx context.Context, req *pb.DeleteFeishuWebhookRequest) (*pb.StatusResponse, error) {
if h == nil || h.svc == nil {
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
}
if req == nil {
return nil, grpcErrorFromServiceError(respond.MissingParam)
}
if err := h.svc.DeleteFeishuWebhook(ctx, int(req.UserId)); err != nil {
return nil, grpcErrorFromServiceError(err)
}
return &pb.StatusResponse{}, nil
}
func (h *Handler) TestFeishuWebhook(ctx context.Context, req *pb.TestFeishuWebhookRequest) (*pb.TestResult, error) {
if h == nil || h.svc == nil {
return nil, grpcErrorFromServiceError(errors.New("notification service dependency not initialized"))
}
if req == nil {
return nil, grpcErrorFromServiceError(respond.MissingParam)
}
resp, err := h.svc.TestFeishuWebhook(ctx, int(req.UserId))
if err != nil {
return nil, grpcErrorFromServiceError(err)
}
return testResultToPB(resp), nil
}
func channelToPB(resp contracts.ChannelResponse) *pb.ChannelResponse {
return &pb.ChannelResponse{
Channel: resp.Channel,
Enabled: resp.Enabled,
Configured: resp.Configured,
WebhookUrlMask: resp.WebhookURLMask,
AuthType: resp.AuthType,
HasBearerToken: resp.HasBearerToken,
LastTestStatus: resp.LastTestStatus,
LastTestError: resp.LastTestError,
LastTestAtUnixNano: timePtrToUnixNano(resp.LastTestAt),
}
}
func testResultToPB(resp contracts.TestResult) *pb.TestResult {
return &pb.TestResult{
Channel: channelToPB(resp.Channel),
Status: resp.Status,
Outcome: resp.Outcome,
Message: resp.Message,
TraceId: resp.TraceID,
SentAtUnixNano: timeToUnixNano(resp.SentAt),
Skipped: resp.Skipped,
Provider: resp.Provider,
}
}
func timePtrToUnixNano(value *time.Time) int64 {
if value == nil || value.IsZero() {
return 0
}
return value.UnixNano()
}
func timeToUnixNano(value time.Time) int64 {
if value.IsZero() {
return 0
}
return value.UnixNano()
}

View File

@@ -0,0 +1,58 @@
syntax = "proto3";
package smartflow.notification;
option go_package = "github.com/LoveLosita/smartflow/backend/services/notification/rpc/pb";
service Notification {
rpc GetFeishuWebhook(GetFeishuWebhookRequest) returns (ChannelResponse);
rpc SaveFeishuWebhook(SaveFeishuWebhookRequest) returns (ChannelResponse);
rpc DeleteFeishuWebhook(DeleteFeishuWebhookRequest) returns (StatusResponse);
rpc TestFeishuWebhook(TestFeishuWebhookRequest) returns (TestResult);
}
message GetFeishuWebhookRequest {
int64 user_id = 1;
}
message SaveFeishuWebhookRequest {
int64 user_id = 1;
bool enabled = 2;
string webhook_url = 3;
string auth_type = 4;
string bearer_token = 5;
}
message DeleteFeishuWebhookRequest {
int64 user_id = 1;
}
message TestFeishuWebhookRequest {
int64 user_id = 1;
}
message StatusResponse {
}
message ChannelResponse {
string channel = 1;
bool enabled = 2;
bool configured = 3;
string webhook_url_mask = 4;
string auth_type = 5;
bool has_bearer_token = 6;
string last_test_status = 7;
string last_test_error = 8;
int64 last_test_at_unix_nano = 9;
}
message TestResult {
ChannelResponse channel = 1;
string status = 2;
string outcome = 3;
string message = 4;
string trace_id = 5;
int64 sent_at_unix_nano = 6;
bool skipped = 7;
string provider = 8;
}

View File

@@ -0,0 +1,102 @@
package pb
import proto "github.com/golang/protobuf/proto"
var _ = proto.Marshal
const _ = proto.ProtoPackageIsVersion3
type GetFeishuWebhookRequest struct {
UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *GetFeishuWebhookRequest) Reset() { *m = GetFeishuWebhookRequest{} }
func (m *GetFeishuWebhookRequest) String() string { return proto.CompactTextString(m) }
func (*GetFeishuWebhookRequest) ProtoMessage() {}
type SaveFeishuWebhookRequest struct {
UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
Enabled bool `protobuf:"varint,2,opt,name=enabled,proto3" json:"enabled,omitempty"`
WebhookUrl string `protobuf:"bytes,3,opt,name=webhook_url,json=webhookUrl,proto3" json:"webhook_url,omitempty"`
AuthType string `protobuf:"bytes,4,opt,name=auth_type,json=authType,proto3" json:"auth_type,omitempty"`
BearerToken string `protobuf:"bytes,5,opt,name=bearer_token,json=bearerToken,proto3" json:"bearer_token,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *SaveFeishuWebhookRequest) Reset() { *m = SaveFeishuWebhookRequest{} }
func (m *SaveFeishuWebhookRequest) String() string { return proto.CompactTextString(m) }
func (*SaveFeishuWebhookRequest) ProtoMessage() {}
type DeleteFeishuWebhookRequest struct {
UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *DeleteFeishuWebhookRequest) Reset() { *m = DeleteFeishuWebhookRequest{} }
func (m *DeleteFeishuWebhookRequest) String() string { return proto.CompactTextString(m) }
func (*DeleteFeishuWebhookRequest) ProtoMessage() {}
type TestFeishuWebhookRequest struct {
UserId int64 `protobuf:"varint,1,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TestFeishuWebhookRequest) Reset() { *m = TestFeishuWebhookRequest{} }
func (m *TestFeishuWebhookRequest) String() string { return proto.CompactTextString(m) }
func (*TestFeishuWebhookRequest) ProtoMessage() {}
type StatusResponse struct {
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *StatusResponse) Reset() { *m = StatusResponse{} }
func (m *StatusResponse) String() string { return proto.CompactTextString(m) }
func (*StatusResponse) ProtoMessage() {}
type ChannelResponse struct {
Channel string `protobuf:"bytes,1,opt,name=channel,proto3" json:"channel,omitempty"`
Enabled bool `protobuf:"varint,2,opt,name=enabled,proto3" json:"enabled,omitempty"`
Configured bool `protobuf:"varint,3,opt,name=configured,proto3" json:"configured,omitempty"`
WebhookUrlMask string `protobuf:"bytes,4,opt,name=webhook_url_mask,json=webhookUrlMask,proto3" json:"webhook_url_mask,omitempty"`
AuthType string `protobuf:"bytes,5,opt,name=auth_type,json=authType,proto3" json:"auth_type,omitempty"`
HasBearerToken bool `protobuf:"varint,6,opt,name=has_bearer_token,json=hasBearerToken,proto3" json:"has_bearer_token,omitempty"`
LastTestStatus string `protobuf:"bytes,7,opt,name=last_test_status,json=lastTestStatus,proto3" json:"last_test_status,omitempty"`
LastTestError string `protobuf:"bytes,8,opt,name=last_test_error,json=lastTestError,proto3" json:"last_test_error,omitempty"`
LastTestAtUnixNano int64 `protobuf:"varint,9,opt,name=last_test_at_unix_nano,json=lastTestAtUnixNano,proto3" json:"last_test_at_unix_nano,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *ChannelResponse) Reset() { *m = ChannelResponse{} }
func (m *ChannelResponse) String() string { return proto.CompactTextString(m) }
func (*ChannelResponse) ProtoMessage() {}
type TestResult struct {
Channel *ChannelResponse `protobuf:"bytes,1,opt,name=channel,proto3" json:"channel,omitempty"`
Status string `protobuf:"bytes,2,opt,name=status,proto3" json:"status,omitempty"`
Outcome string `protobuf:"bytes,3,opt,name=outcome,proto3" json:"outcome,omitempty"`
Message string `protobuf:"bytes,4,opt,name=message,proto3" json:"message,omitempty"`
TraceId string `protobuf:"bytes,5,opt,name=trace_id,json=traceId,proto3" json:"trace_id,omitempty"`
SentAtUnixNano int64 `protobuf:"varint,6,opt,name=sent_at_unix_nano,json=sentAtUnixNano,proto3" json:"sent_at_unix_nano,omitempty"`
Skipped bool `protobuf:"varint,7,opt,name=skipped,proto3" json:"skipped,omitempty"`
Provider string `protobuf:"bytes,8,opt,name=provider,proto3" json:"provider,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
func (m *TestResult) Reset() { *m = TestResult{} }
func (m *TestResult) String() string { return proto.CompactTextString(m) }
func (*TestResult) ProtoMessage() {}

View File

@@ -0,0 +1,193 @@
package pb
import (
context "context"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
)
const (
Notification_GetFeishuWebhook_FullMethodName = "/smartflow.notification.Notification/GetFeishuWebhook"
Notification_SaveFeishuWebhook_FullMethodName = "/smartflow.notification.Notification/SaveFeishuWebhook"
Notification_DeleteFeishuWebhook_FullMethodName = "/smartflow.notification.Notification/DeleteFeishuWebhook"
Notification_TestFeishuWebhook_FullMethodName = "/smartflow.notification.Notification/TestFeishuWebhook"
)
type NotificationClient interface {
GetFeishuWebhook(ctx context.Context, in *GetFeishuWebhookRequest, opts ...grpc.CallOption) (*ChannelResponse, error)
SaveFeishuWebhook(ctx context.Context, in *SaveFeishuWebhookRequest, opts ...grpc.CallOption) (*ChannelResponse, error)
DeleteFeishuWebhook(ctx context.Context, in *DeleteFeishuWebhookRequest, opts ...grpc.CallOption) (*StatusResponse, error)
TestFeishuWebhook(ctx context.Context, in *TestFeishuWebhookRequest, opts ...grpc.CallOption) (*TestResult, error)
}
type notificationClient struct {
cc grpc.ClientConnInterface
}
func NewNotificationClient(cc grpc.ClientConnInterface) NotificationClient {
return &notificationClient{cc}
}
func (c *notificationClient) GetFeishuWebhook(ctx context.Context, in *GetFeishuWebhookRequest, opts ...grpc.CallOption) (*ChannelResponse, error) {
out := new(ChannelResponse)
err := c.cc.Invoke(ctx, Notification_GetFeishuWebhook_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notificationClient) SaveFeishuWebhook(ctx context.Context, in *SaveFeishuWebhookRequest, opts ...grpc.CallOption) (*ChannelResponse, error) {
out := new(ChannelResponse)
err := c.cc.Invoke(ctx, Notification_SaveFeishuWebhook_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notificationClient) DeleteFeishuWebhook(ctx context.Context, in *DeleteFeishuWebhookRequest, opts ...grpc.CallOption) (*StatusResponse, error) {
out := new(StatusResponse)
err := c.cc.Invoke(ctx, Notification_DeleteFeishuWebhook_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *notificationClient) TestFeishuWebhook(ctx context.Context, in *TestFeishuWebhookRequest, opts ...grpc.CallOption) (*TestResult, error) {
out := new(TestResult)
err := c.cc.Invoke(ctx, Notification_TestFeishuWebhook_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
type NotificationServer interface {
GetFeishuWebhook(context.Context, *GetFeishuWebhookRequest) (*ChannelResponse, error)
SaveFeishuWebhook(context.Context, *SaveFeishuWebhookRequest) (*ChannelResponse, error)
DeleteFeishuWebhook(context.Context, *DeleteFeishuWebhookRequest) (*StatusResponse, error)
TestFeishuWebhook(context.Context, *TestFeishuWebhookRequest) (*TestResult, error)
}
type UnimplementedNotificationServer struct{}
func (UnimplementedNotificationServer) GetFeishuWebhook(context.Context, *GetFeishuWebhookRequest) (*ChannelResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetFeishuWebhook not implemented")
}
func (UnimplementedNotificationServer) SaveFeishuWebhook(context.Context, *SaveFeishuWebhookRequest) (*ChannelResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SaveFeishuWebhook not implemented")
}
func (UnimplementedNotificationServer) DeleteFeishuWebhook(context.Context, *DeleteFeishuWebhookRequest) (*StatusResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteFeishuWebhook not implemented")
}
func (UnimplementedNotificationServer) TestFeishuWebhook(context.Context, *TestFeishuWebhookRequest) (*TestResult, error) {
return nil, status.Errorf(codes.Unimplemented, "method TestFeishuWebhook not implemented")
}
func RegisterNotificationServer(s grpc.ServiceRegistrar, srv NotificationServer) {
s.RegisterService(&Notification_ServiceDesc, srv)
}
func _Notification_GetFeishuWebhook_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetFeishuWebhookRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(NotificationServer).GetFeishuWebhook(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Notification_GetFeishuWebhook_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NotificationServer).GetFeishuWebhook(ctx, req.(*GetFeishuWebhookRequest))
}
return interceptor(ctx, in, info, handler)
}
func _Notification_SaveFeishuWebhook_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(SaveFeishuWebhookRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(NotificationServer).SaveFeishuWebhook(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Notification_SaveFeishuWebhook_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NotificationServer).SaveFeishuWebhook(ctx, req.(*SaveFeishuWebhookRequest))
}
return interceptor(ctx, in, info, handler)
}
func _Notification_DeleteFeishuWebhook_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(DeleteFeishuWebhookRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(NotificationServer).DeleteFeishuWebhook(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Notification_DeleteFeishuWebhook_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NotificationServer).DeleteFeishuWebhook(ctx, req.(*DeleteFeishuWebhookRequest))
}
return interceptor(ctx, in, info, handler)
}
func _Notification_TestFeishuWebhook_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(TestFeishuWebhookRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(NotificationServer).TestFeishuWebhook(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Notification_TestFeishuWebhook_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(NotificationServer).TestFeishuWebhook(ctx, req.(*TestFeishuWebhookRequest))
}
return interceptor(ctx, in, info, handler)
}
var Notification_ServiceDesc = grpc.ServiceDesc{
ServiceName: "smartflow.notification.Notification",
HandlerType: (*NotificationServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetFeishuWebhook",
Handler: _Notification_GetFeishuWebhook_Handler,
},
{
MethodName: "SaveFeishuWebhook",
Handler: _Notification_SaveFeishuWebhook_Handler,
},
{
MethodName: "DeleteFeishuWebhook",
Handler: _Notification_DeleteFeishuWebhook_Handler,
},
{
MethodName: "TestFeishuWebhook",
Handler: _Notification_TestFeishuWebhook_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "services/notification/rpc/notification.proto",
}

View File

@@ -0,0 +1,54 @@
package rpc
import (
"errors"
"strings"
"time"
"github.com/LoveLosita/smartflow/backend/services/notification/rpc/pb"
notificationsv "github.com/LoveLosita/smartflow/backend/services/notification/sv"
"github.com/zeromicro/go-zero/core/service"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
)
const (
defaultListenOn = "0.0.0.0:9082"
defaultTimeout = 6 * time.Second
)
type ServerOptions struct {
ListenOn string
Timeout time.Duration
Service *notificationsv.Service
}
func NewServer(opts ServerOptions) (*zrpc.RpcServer, string, error) {
if opts.Service == nil {
return nil, "", errors.New("notification service dependency not initialized")
}
listenOn := strings.TrimSpace(opts.ListenOn)
if listenOn == "" {
listenOn = defaultListenOn
}
timeout := opts.Timeout
if timeout <= 0 {
timeout = defaultTimeout
}
server, err := zrpc.NewServer(zrpc.RpcServerConf{
ServiceConf: service.ServiceConf{
Name: "notification.rpc",
Mode: service.DevMode,
},
ListenOn: listenOn,
Timeout: int64(timeout / time.Millisecond),
}, func(grpcServer *grpc.Server) {
pb.RegisterNotificationServer(grpcServer, NewHandler(opts.Service))
})
if err != nil {
return nil, "", err
}
return server, listenOn, nil
}