add: anowflake email kafka, refa: redis connectg
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"juwan-backend/app/email/api/internal/config"
|
||||
"juwan-backend/app/email/api/internal/handler"
|
||||
"juwan-backend/app/email/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/email-api.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
|
||||
server := rest.MustNewServer(c.RestConf)
|
||||
defer server.Stop()
|
||||
|
||||
ctx := svc.NewServiceContext(c)
|
||||
handler.RegisterHandlers(server, ctx)
|
||||
|
||||
fmt.Printf("Starting server at %s:%d...\n", c.Host, c.Port)
|
||||
server.Start()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
Name: email-api
|
||||
Host: 0.0.0.0
|
||||
Port: 8888
|
||||
|
||||
CacheConf:
|
||||
- Host: "${REDIS_M_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
- Host: "${REDIS_S_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
|
||||
Kmq:
|
||||
Name: email-api
|
||||
Brokers:
|
||||
- "${KAFKA_BROKER}"
|
||||
Topic: "email-task"
|
||||
@@ -0,0 +1,16 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-queue/kq"
|
||||
"github.com/zeromicro/go-zero/core/stores/cache"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
CacheConf cache.CacheConf
|
||||
Kmq kq.KqConf
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package email
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
"juwan-backend/app/email/api/internal/logic/email"
|
||||
"juwan-backend/app/email/api/internal/svc"
|
||||
"juwan-backend/app/email/api/internal/types"
|
||||
)
|
||||
|
||||
// 发送邮箱验证码
|
||||
func SendVerificationCodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.SendVerificationCodeReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := email.NewSendVerificationCodeLogic(r.Context(), svcCtx)
|
||||
resp, err := l.SendVerificationCode(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
email "juwan-backend/app/email/api/internal/handler/email"
|
||||
"juwan-backend/app/email/api/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.Logger},
|
||||
[]rest.Route{
|
||||
{
|
||||
// 发送邮箱验证码
|
||||
Method: http.MethodPost,
|
||||
Path: "/verification-code/send",
|
||||
Handler: email.SendVerificationCodeHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/email"),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package email
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/email/api/internal/svc"
|
||||
"juwan-backend/app/email/api/internal/types"
|
||||
"juwan-backend/app/email/api/internal/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SendVerificationCodeLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 发送邮箱验证码
|
||||
func NewSendVerificationCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SendVerificationCodeLogic {
|
||||
return &SendVerificationCodeLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SendVerificationCodeLogic) SendVerificationCode(req *types.SendVerificationCodeReq) (resp *types.SendVerificationCodeResp, err error) {
|
||||
if l.svcCtx.RedisCluster == nil {
|
||||
return nil, fmt.Errorf("redis not configured")
|
||||
}
|
||||
|
||||
if l.svcCtx.EmailPusher == nil {
|
||||
return nil, fmt.Errorf("kafka pusher not configured")
|
||||
}
|
||||
|
||||
code := utils.GenCode()
|
||||
requestID := uuid.NewString()
|
||||
|
||||
redisKey := fmt.Sprintf("%s:%s:%s", req.Email, code, req.Email)
|
||||
if exists, getErr := l.svcCtx.RedisCluster.Get(l.ctx, redisKey).Result(); getErr == nil && exists != "" {
|
||||
return nil, fmt.Errorf("verification code already sent, please wait before requesting a new one")
|
||||
}
|
||||
if setErr := l.svcCtx.RedisCluster.Set(l.ctx, redisKey, req.Scene, 60*time.Second).Err(); setErr != nil {
|
||||
return nil, setErr
|
||||
}
|
||||
|
||||
payload := map[string]any{
|
||||
"type": "verification_code",
|
||||
"requestId": requestID,
|
||||
"email": req.Email,
|
||||
"scene": req.Scene,
|
||||
"code": code,
|
||||
"expireIn": 60,
|
||||
}
|
||||
messageBytes, marshalErr := json.Marshal(payload)
|
||||
if marshalErr != nil {
|
||||
return nil, marshalErr
|
||||
}
|
||||
|
||||
if pushErr := l.svcCtx.EmailPusher.PushWithKey(l.ctx, req.Email, string(messageBytes)); pushErr != nil {
|
||||
return nil, pushErr
|
||||
}
|
||||
|
||||
resp = &types.SendVerificationCodeResp{
|
||||
RequestId: requestID,
|
||||
ExpireInSec: 60,
|
||||
Message: "verification code send task submitted",
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
|
||||
type LoggerMiddleware struct {
|
||||
}
|
||||
|
||||
func NewLoggerMiddleware() *LoggerMiddleware {
|
||||
return &LoggerMiddleware{}
|
||||
}
|
||||
|
||||
func (m *LoggerMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// TODO generate middleware implement function, delete after code implementation
|
||||
|
||||
// Passthrough to next handler if need
|
||||
next(w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.9.2
|
||||
|
||||
package svc
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/email/api/internal/config"
|
||||
"juwan-backend/app/email/api/internal/middleware"
|
||||
"juwan-backend/common/redisx"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/zeromicro/go-queue/kq"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Logger rest.Middleware
|
||||
RedisCluster *redis.ClusterClient
|
||||
EmailPusher *kq.Pusher
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
redisConn, err := redisx.ConnectMasterSlaveCluster(c.CacheConf, 5*time.Second)
|
||||
if err != nil {
|
||||
logx.Errorf("failed to connect redis for email-api: %v", err)
|
||||
}
|
||||
|
||||
var emailPusher *kq.Pusher
|
||||
if len(c.Kmq.Brokers) > 0 && c.Kmq.Topic != "" {
|
||||
emailPusher = kq.NewPusher(c.Kmq.Brokers, c.Kmq.Topic)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Logger: middleware.NewLoggerMiddleware().Handle,
|
||||
RedisCluster: redisConn.Client,
|
||||
EmailPusher: emailPusher,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
|
||||
package types
|
||||
|
||||
type SendVerificationCodeReq struct {
|
||||
Email string `json:"email" binding:"required,email"`
|
||||
Scene string `json:"scene" binding:"required,oneof=register login reset_password bind_email"`
|
||||
}
|
||||
|
||||
type SendVerificationCodeResp struct {
|
||||
RequestId string `json:"requestId"`
|
||||
ExpireInSec int64 `json:"expireInSec"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
func GenCode() string {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
||||
if err != nil {
|
||||
return "000000"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%06d", n.Int64())
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"juwan-backend/app/email/mq/internal/config"
|
||||
"juwan-backend/app/email/mq/internal/consumer"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/email.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
if err := c.SetUp(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
serviceGroup := service.NewServiceGroup()
|
||||
defer serviceGroup.Stop()
|
||||
|
||||
for _, mq := range consumer.Mqs(c) {
|
||||
serviceGroup.Add(mq)
|
||||
}
|
||||
|
||||
fmt.Print("Starting email service\n")
|
||||
serviceGroup.Start()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
Name: email-mq
|
||||
|
||||
Prometheus:
|
||||
Host: 0.0.0.0
|
||||
Port: 4003
|
||||
Path: /metrics
|
||||
|
||||
Kmq:
|
||||
Name: email-mq
|
||||
Brokers:
|
||||
- my-cluster-kafka-bootstrap.kafka.svc.cluster.local:9092
|
||||
Topic: email-task
|
||||
Group: email-consumer-group
|
||||
ForceCommit: true
|
||||
CommitInOrder: false
|
||||
Offset: last
|
||||
Consumers: 8
|
||||
Processors: 8
|
||||
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"github.com/zeromicro/go-queue/kq"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
service.ServiceConf
|
||||
Kmq kq.KqConf
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package consumer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/email/mq/internal/config"
|
||||
|
||||
"juwan-backend/app/email/mq/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
)
|
||||
|
||||
func Mqs(c config.Config) []service.Service {
|
||||
//svcContext := NewServiceContext
|
||||
ctx := context.Background()
|
||||
svcCtx := svc.NewServiceContext(c)
|
||||
|
||||
var services []service.Service
|
||||
services = append(services, Kqs(ctx, c, svcCtx)...)
|
||||
|
||||
return services
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package consumer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/email/mq/internal/config"
|
||||
"juwan-backend/app/email/mq/internal/logic"
|
||||
"juwan-backend/app/email/mq/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-queue/kq"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
)
|
||||
|
||||
func Kqs(ctx context.Context, c config.Config, svcCtx *svc.ServiceContext) []service.Service {
|
||||
return []service.Service{kq.MustNewQueue(c.Kmq, logic.NewSendVerificationCodeMq(ctx, c, svcCtx))}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/email/mq/internal/config"
|
||||
"juwan-backend/app/email/mq/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SendVerificationCodeMq struct {
|
||||
c config.Config
|
||||
ctx context.Context
|
||||
svcCxt *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSendVerificationCodeMq(ctx context.Context, c config.Config, svcCtx *svc.ServiceContext) *SendVerificationCodeMq {
|
||||
return &SendVerificationCodeMq{
|
||||
c: c,
|
||||
ctx: ctx,
|
||||
svcCxt: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SendVerificationCodeMq) Consume(ctx context.Context, key, value string) error {
|
||||
_ = ctx
|
||||
_ = key
|
||||
_ = value
|
||||
logx.Infof("Consume get message key: %s, value: %s", key, value)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package svc
|
||||
|
||||
import "juwan-backend/app/email/mq/internal/config"
|
||||
|
||||
type ServiceContext struct {
|
||||
c config.Config
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
return &ServiceContext{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
Name: snowflake.rpc
|
||||
ListenOn: 0.0.0.0:8080
|
||||
#Etcd:
|
||||
# Hosts:
|
||||
# - 127.0.0.1:2379
|
||||
# Key: snowflake.rpc
|
||||
|
||||
Snowflake:
|
||||
DatacenterId: 1
|
||||
WorkerId: 0
|
||||
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
Snowflake struct {
|
||||
DatacenterId int64
|
||||
WorkerId int64
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/internal/svc"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type NextIdLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewNextIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NextIdLogic {
|
||||
return &NextIdLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *NextIdLogic) NextId(_ *snowflake.NextIdReq) (*snowflake.NextIdResp, error) {
|
||||
id, err := l.svcCtx.Generator.NextID()
|
||||
if err != nil {
|
||||
l.Error("generator.NextID", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
return &snowflake.NextIdResp{Id: id}, nil
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/internal/svc"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type NextIdsLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewNextIdsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NextIdsLogic {
|
||||
return &NextIdsLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *NextIdsLogic) NextIds(in *snowflake.NextIdsReq) (*snowflake.NextIdsResp, error) {
|
||||
if in.Count <= 0 || in.Count > 1000 {
|
||||
return nil, errors.New("count must be between 1 and 1000")
|
||||
}
|
||||
ids, err := l.svcCtx.Generator.NextIDs(int(in.Count))
|
||||
if err != nil {
|
||||
l.Errorf("generate snowflake ids failed: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return &snowflake.NextIdsResp{Ids: ids}, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
epoch = int64(1609459200000)
|
||||
datacenterIdBits = uint(5)
|
||||
workerIdBits = uint(5)
|
||||
sequenceBits = uint(12)
|
||||
|
||||
maxDatacenterId = -1 ^ (-1 << datacenterIdBits)
|
||||
maxWorkerId = -1 ^ (-1 << workerIdBits)
|
||||
maxSequence = -1 ^ (-1 << sequenceBits)
|
||||
|
||||
workerIdShift = sequenceBits
|
||||
datacenterIdShift = sequenceBits + workerIdBits
|
||||
timestampLeftShift = sequenceBits + workerIdBits + datacenterIdBits
|
||||
)
|
||||
|
||||
type Snowflake struct {
|
||||
mu sync.Mutex
|
||||
timestamp int64
|
||||
datacenterId int64
|
||||
workerId int64
|
||||
sequence int64
|
||||
}
|
||||
|
||||
func NewSnowflake(datacenterId, workerId int64) (*Snowflake, error) {
|
||||
if datacenterId < 0 || datacenterId > maxDatacenterId {
|
||||
return nil, errors.New("datacenter id must be between 0 and 31")
|
||||
}
|
||||
if workerId < 0 || workerId > maxWorkerId {
|
||||
return nil, errors.New("worker id must be between 0 and 31")
|
||||
}
|
||||
|
||||
return &Snowflake{
|
||||
timestamp: 0,
|
||||
datacenterId: datacenterId,
|
||||
workerId: workerId,
|
||||
sequence: 0,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Snowflake) NextID() (int64, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
now := time.Now().UnixMilli()
|
||||
|
||||
if now < s.timestamp {
|
||||
return 0, errors.New("clock moved backwards")
|
||||
}
|
||||
|
||||
if now == s.timestamp {
|
||||
s.sequence = (s.sequence + 1) & maxSequence
|
||||
if s.sequence == 0 {
|
||||
for now <= s.timestamp {
|
||||
now = time.Now().UnixMilli()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s.sequence = 0
|
||||
}
|
||||
|
||||
s.timestamp = now
|
||||
|
||||
id := ((now - epoch) << timestampLeftShift) |
|
||||
(s.datacenterId << datacenterIdShift) |
|
||||
(s.workerId << workerIdShift) |
|
||||
s.sequence
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (s *Snowflake) NextIDs(count int) ([]int64, error) {
|
||||
if count <= 0 || count > 1000 {
|
||||
return nil, errors.New("count must be between 1 and 1000")
|
||||
}
|
||||
|
||||
ids := make([]int64, count)
|
||||
for i := 0; i < count; i++ {
|
||||
id, err := s.NextID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids[i] = id
|
||||
}
|
||||
|
||||
return ids, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
// Source: snowflake.proto
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/internal/logic"
|
||||
"juwan-backend/app/snowflake/rpc/internal/svc"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
)
|
||||
|
||||
type SnowflakeServiceServer struct {
|
||||
svcCtx *svc.ServiceContext
|
||||
snowflake.UnimplementedSnowflakeServiceServer
|
||||
}
|
||||
|
||||
func NewSnowflakeServiceServer(svcCtx *svc.ServiceContext) *SnowflakeServiceServer {
|
||||
return &SnowflakeServiceServer{
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SnowflakeServiceServer) NextId(ctx context.Context, in *snowflake.NextIdReq) (*snowflake.NextIdResp, error) {
|
||||
l := logic.NewNextIdLogic(ctx, s.svcCtx)
|
||||
return l.NextId(in)
|
||||
}
|
||||
|
||||
func (s *SnowflakeServiceServer) NextIds(ctx context.Context, in *snowflake.NextIdsReq) (*snowflake.NextIdsResp, error) {
|
||||
l := logic.NewNextIdsLogic(ctx, s.svcCtx)
|
||||
return l.NextIds(in)
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"juwan-backend/app/snowflake/rpc/internal/config"
|
||||
generator "juwan-backend/app/snowflake/rpc/internal/pkg"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Generator *generator.Snowflake
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
gen, err := generator.NewSnowflake(
|
||||
c.Snowflake.DatacenterId,
|
||||
c.Snowflake.WorkerId,
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Generator: gen,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/internal/config"
|
||||
"juwan-backend/app/snowflake/rpc/internal/server"
|
||||
"juwan-backend/app/snowflake/rpc/internal/svc"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/conf"
|
||||
"github.com/zeromicro/go-zero/core/service"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
var configFile = flag.String("f", "etc/snowflake.yaml", "the config file")
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
var c config.Config
|
||||
conf.MustLoad(*configFile, &c)
|
||||
ctx := svc.NewServiceContext(c)
|
||||
|
||||
s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
|
||||
snowflake.RegisterSnowflakeServiceServer(grpcServer, server.NewSnowflakeServiceServer(ctx))
|
||||
|
||||
if c.Mode == service.DevMode || c.Mode == service.TestMode {
|
||||
reflection.Register(grpcServer)
|
||||
}
|
||||
})
|
||||
defer s.Stop()
|
||||
|
||||
fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
|
||||
s.Start()
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.36.11
|
||||
// protoc v3.19.4
|
||||
// source: snowflake.proto
|
||||
|
||||
package snowflake
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
unsafe "unsafe"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type NextIdReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *NextIdReq) Reset() {
|
||||
*x = NextIdReq{}
|
||||
mi := &file_snowflake_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *NextIdReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*NextIdReq) ProtoMessage() {}
|
||||
|
||||
func (x *NextIdReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_snowflake_proto_msgTypes[0]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use NextIdReq.ProtoReflect.Descriptor instead.
|
||||
func (*NextIdReq) Descriptor() ([]byte, []int) {
|
||||
return file_snowflake_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type NextIdResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *NextIdResp) Reset() {
|
||||
*x = NextIdResp{}
|
||||
mi := &file_snowflake_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *NextIdResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*NextIdResp) ProtoMessage() {}
|
||||
|
||||
func (x *NextIdResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_snowflake_proto_msgTypes[1]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use NextIdResp.ProtoReflect.Descriptor instead.
|
||||
func (*NextIdResp) Descriptor() ([]byte, []int) {
|
||||
return file_snowflake_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *NextIdResp) GetId() int64 {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type NextIdsReq struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Count int32 `protobuf:"varint,1,opt,name=count,proto3" json:"count,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *NextIdsReq) Reset() {
|
||||
*x = NextIdsReq{}
|
||||
mi := &file_snowflake_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *NextIdsReq) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*NextIdsReq) ProtoMessage() {}
|
||||
|
||||
func (x *NextIdsReq) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_snowflake_proto_msgTypes[2]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use NextIdsReq.ProtoReflect.Descriptor instead.
|
||||
func (*NextIdsReq) Descriptor() ([]byte, []int) {
|
||||
return file_snowflake_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *NextIdsReq) GetCount() int32 {
|
||||
if x != nil {
|
||||
return x.Count
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
type NextIdsResp struct {
|
||||
state protoimpl.MessageState `protogen:"open.v1"`
|
||||
Ids []int64 `protobuf:"varint,1,rep,packed,name=ids,proto3" json:"ids,omitempty"`
|
||||
unknownFields protoimpl.UnknownFields
|
||||
sizeCache protoimpl.SizeCache
|
||||
}
|
||||
|
||||
func (x *NextIdsResp) Reset() {
|
||||
*x = NextIdsResp{}
|
||||
mi := &file_snowflake_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
|
||||
func (x *NextIdsResp) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*NextIdsResp) ProtoMessage() {}
|
||||
|
||||
func (x *NextIdsResp) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_snowflake_proto_msgTypes[3]
|
||||
if x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use NextIdsResp.ProtoReflect.Descriptor instead.
|
||||
func (*NextIdsResp) Descriptor() ([]byte, []int) {
|
||||
return file_snowflake_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *NextIdsResp) GetIds() []int64 {
|
||||
if x != nil {
|
||||
return x.Ids
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var File_snowflake_proto protoreflect.FileDescriptor
|
||||
|
||||
const file_snowflake_proto_rawDesc = "" +
|
||||
"\n" +
|
||||
"\x0fsnowflake.proto\x12\tsnowflake\"\v\n" +
|
||||
"\tNextIdReq\"\x1c\n" +
|
||||
"\n" +
|
||||
"NextIdResp\x12\x0e\n" +
|
||||
"\x02id\x18\x01 \x01(\x03R\x02id\"\"\n" +
|
||||
"\n" +
|
||||
"NextIdsReq\x12\x14\n" +
|
||||
"\x05count\x18\x01 \x01(\x05R\x05count\"\x1f\n" +
|
||||
"\vNextIdsResp\x12\x10\n" +
|
||||
"\x03ids\x18\x01 \x03(\x03R\x03ids2\x83\x01\n" +
|
||||
"\x10SnowflakeService\x125\n" +
|
||||
"\x06NextId\x12\x14.snowflake.NextIdReq\x1a\x15.snowflake.NextIdResp\x128\n" +
|
||||
"\aNextIds\x12\x15.snowflake.NextIdsReq\x1a\x16.snowflake.NextIdsRespB\rZ\v./snowflakeb\x06proto3"
|
||||
|
||||
var (
|
||||
file_snowflake_proto_rawDescOnce sync.Once
|
||||
file_snowflake_proto_rawDescData []byte
|
||||
)
|
||||
|
||||
func file_snowflake_proto_rawDescGZIP() []byte {
|
||||
file_snowflake_proto_rawDescOnce.Do(func() {
|
||||
file_snowflake_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_snowflake_proto_rawDesc), len(file_snowflake_proto_rawDesc)))
|
||||
})
|
||||
return file_snowflake_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_snowflake_proto_msgTypes = make([]protoimpl.MessageInfo, 4)
|
||||
var file_snowflake_proto_goTypes = []any{
|
||||
(*NextIdReq)(nil), // 0: snowflake.NextIdReq
|
||||
(*NextIdResp)(nil), // 1: snowflake.NextIdResp
|
||||
(*NextIdsReq)(nil), // 2: snowflake.NextIdsReq
|
||||
(*NextIdsResp)(nil), // 3: snowflake.NextIdsResp
|
||||
}
|
||||
var file_snowflake_proto_depIdxs = []int32{
|
||||
0, // 0: snowflake.SnowflakeService.NextId:input_type -> snowflake.NextIdReq
|
||||
2, // 1: snowflake.SnowflakeService.NextIds:input_type -> snowflake.NextIdsReq
|
||||
1, // 2: snowflake.SnowflakeService.NextId:output_type -> snowflake.NextIdResp
|
||||
3, // 3: snowflake.SnowflakeService.NextIds:output_type -> snowflake.NextIdsResp
|
||||
2, // [2:4] is the sub-list for method output_type
|
||||
0, // [0:2] is the sub-list for method input_type
|
||||
0, // [0:0] is the sub-list for extension type_name
|
||||
0, // [0:0] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_snowflake_proto_init() }
|
||||
func file_snowflake_proto_init() {
|
||||
if File_snowflake_proto != nil {
|
||||
return
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: unsafe.Slice(unsafe.StringData(file_snowflake_proto_rawDesc), len(file_snowflake_proto_rawDesc)),
|
||||
NumEnums: 0,
|
||||
NumMessages: 4,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_snowflake_proto_goTypes,
|
||||
DependencyIndexes: file_snowflake_proto_depIdxs,
|
||||
MessageInfos: file_snowflake_proto_msgTypes,
|
||||
}.Build()
|
||||
File_snowflake_proto = out.File
|
||||
file_snowflake_proto_goTypes = nil
|
||||
file_snowflake_proto_depIdxs = nil
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.6.1
|
||||
// - protoc v3.19.4
|
||||
// source: snowflake.proto
|
||||
|
||||
package snowflake
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.64.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion9
|
||||
|
||||
const (
|
||||
SnowflakeService_NextId_FullMethodName = "/snowflake.SnowflakeService/NextId"
|
||||
SnowflakeService_NextIds_FullMethodName = "/snowflake.SnowflakeService/NextIds"
|
||||
)
|
||||
|
||||
// SnowflakeServiceClient is the client API for SnowflakeService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type SnowflakeServiceClient interface {
|
||||
NextId(ctx context.Context, in *NextIdReq, opts ...grpc.CallOption) (*NextIdResp, error)
|
||||
NextIds(ctx context.Context, in *NextIdsReq, opts ...grpc.CallOption) (*NextIdsResp, error)
|
||||
}
|
||||
|
||||
type snowflakeServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewSnowflakeServiceClient(cc grpc.ClientConnInterface) SnowflakeServiceClient {
|
||||
return &snowflakeServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *snowflakeServiceClient) NextId(ctx context.Context, in *NextIdReq, opts ...grpc.CallOption) (*NextIdResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(NextIdResp)
|
||||
err := c.cc.Invoke(ctx, SnowflakeService_NextId_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *snowflakeServiceClient) NextIds(ctx context.Context, in *NextIdsReq, opts ...grpc.CallOption) (*NextIdsResp, error) {
|
||||
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
|
||||
out := new(NextIdsResp)
|
||||
err := c.cc.Invoke(ctx, SnowflakeService_NextIds_FullMethodName, in, out, cOpts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// SnowflakeServiceServer is the server API for SnowflakeService service.
|
||||
// All implementations must embed UnimplementedSnowflakeServiceServer
|
||||
// for forward compatibility.
|
||||
type SnowflakeServiceServer interface {
|
||||
NextId(context.Context, *NextIdReq) (*NextIdResp, error)
|
||||
NextIds(context.Context, *NextIdsReq) (*NextIdsResp, error)
|
||||
mustEmbedUnimplementedSnowflakeServiceServer()
|
||||
}
|
||||
|
||||
// UnimplementedSnowflakeServiceServer must be embedded to have
|
||||
// forward compatible implementations.
|
||||
//
|
||||
// NOTE: this should be embedded by value instead of pointer to avoid a nil
|
||||
// pointer dereference when methods are called.
|
||||
type UnimplementedSnowflakeServiceServer struct{}
|
||||
|
||||
func (UnimplementedSnowflakeServiceServer) NextId(context.Context, *NextIdReq) (*NextIdResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method NextId not implemented")
|
||||
}
|
||||
func (UnimplementedSnowflakeServiceServer) NextIds(context.Context, *NextIdsReq) (*NextIdsResp, error) {
|
||||
return nil, status.Error(codes.Unimplemented, "method NextIds not implemented")
|
||||
}
|
||||
func (UnimplementedSnowflakeServiceServer) mustEmbedUnimplementedSnowflakeServiceServer() {}
|
||||
func (UnimplementedSnowflakeServiceServer) testEmbeddedByValue() {}
|
||||
|
||||
// UnsafeSnowflakeServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to SnowflakeServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeSnowflakeServiceServer interface {
|
||||
mustEmbedUnimplementedSnowflakeServiceServer()
|
||||
}
|
||||
|
||||
func RegisterSnowflakeServiceServer(s grpc.ServiceRegistrar, srv SnowflakeServiceServer) {
|
||||
// If the following call panics, it indicates UnimplementedSnowflakeServiceServer was
|
||||
// embedded by pointer and is nil. This will cause panics if an
|
||||
// unimplemented method is ever invoked, so we test this at initialization
|
||||
// time to prevent it from happening at runtime later due to I/O.
|
||||
if t, ok := srv.(interface{ testEmbeddedByValue() }); ok {
|
||||
t.testEmbeddedByValue()
|
||||
}
|
||||
s.RegisterService(&SnowflakeService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _SnowflakeService_NextId_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(NextIdReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SnowflakeServiceServer).NextId(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: SnowflakeService_NextId_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SnowflakeServiceServer).NextId(ctx, req.(*NextIdReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
func _SnowflakeService_NextIds_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(NextIdsReq)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(SnowflakeServiceServer).NextIds(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: SnowflakeService_NextIds_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(SnowflakeServiceServer).NextIds(ctx, req.(*NextIdsReq))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// SnowflakeService_ServiceDesc is the grpc.ServiceDesc for SnowflakeService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var SnowflakeService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "snowflake.SnowflakeService",
|
||||
HandlerType: (*SnowflakeServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "NextId",
|
||||
Handler: _SnowflakeService_NextId_Handler,
|
||||
},
|
||||
{
|
||||
MethodName: "NextIds",
|
||||
Handler: _SnowflakeService_NextIds_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{},
|
||||
Metadata: "snowflake.proto",
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// goctl 1.9.2
|
||||
// Source: snowflake.proto
|
||||
|
||||
package snowflakeservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
type (
|
||||
NextIdReq = snowflake.NextIdReq
|
||||
NextIdResp = snowflake.NextIdResp
|
||||
NextIdsReq = snowflake.NextIdsReq
|
||||
NextIdsResp = snowflake.NextIdsResp
|
||||
|
||||
SnowflakeService interface {
|
||||
NextId(ctx context.Context, in *NextIdReq, opts ...grpc.CallOption) (*NextIdResp, error)
|
||||
NextIds(ctx context.Context, in *NextIdsReq, opts ...grpc.CallOption) (*NextIdsResp, error)
|
||||
}
|
||||
|
||||
defaultSnowflakeService struct {
|
||||
cli zrpc.Client
|
||||
}
|
||||
)
|
||||
|
||||
func NewSnowflakeService(cli zrpc.Client) SnowflakeService {
|
||||
return &defaultSnowflakeService{
|
||||
cli: cli,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultSnowflakeService) NextId(ctx context.Context, in *NextIdReq, opts ...grpc.CallOption) (*NextIdResp, error) {
|
||||
client := snowflake.NewSnowflakeServiceClient(m.cli.Conn())
|
||||
return client.NextId(ctx, in, opts...)
|
||||
}
|
||||
|
||||
func (m *defaultSnowflakeService) NextIds(ctx context.Context, in *NextIdsReq, opts ...grpc.CallOption) (*NextIdsResp, error) {
|
||||
client := snowflake.NewSnowflakeServiceClient(m.cli.Conn())
|
||||
return client.NextIds(ctx, in, opts...)
|
||||
}
|
||||
@@ -37,23 +37,23 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
Username: req.Username,
|
||||
})
|
||||
if err == nil && existingUser != nil {
|
||||
return nil, errors.New("用户已存在")
|
||||
return nil, errors.New("user already exists")
|
||||
}
|
||||
|
||||
// 生成用户ID
|
||||
userId, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return nil, errors.New("注册失败:无法生成用户ID")
|
||||
return nil, errors.New("generate user ID failed")
|
||||
}
|
||||
|
||||
// 加密密码
|
||||
hashedPassword, err := utils.HashPassword(req.Password)
|
||||
if err != nil {
|
||||
return nil, errors.New("注册失败:密码加密失败")
|
||||
return nil, errors.New("hash password failed")
|
||||
}
|
||||
|
||||
// 创建新用户
|
||||
newUser, err := l.svcCtx.UserRpc.AddUsers(l.ctx, &pb.AddUsersReq{
|
||||
_res, err := l.svcCtx.UserRpc.AddUsers(l.ctx, &pb.AddUsersReq{
|
||||
UserId: userId.String(),
|
||||
Username: req.Username,
|
||||
Passwd: hashedPassword,
|
||||
@@ -62,14 +62,9 @@ func (l *RegisterLogic) Register(req *types.RegisterReq) (resp *types.RegisterRe
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("AddUsers failed: %v", err)
|
||||
return nil, errors.New("注册失败:创建用户失败")
|
||||
return nil, errors.New("add user failed")
|
||||
}
|
||||
|
||||
// 返回响应
|
||||
return &types.RegisterResp{
|
||||
UserId: int64(newUser.), // RPC 返回的可能是用户信息,这里简化处理
|
||||
Username: req.Username,
|
||||
Email: req.Email,
|
||||
Message: "注册成功",
|
||||
}, nil
|
||||
return &types.RegisterResp{}, nil
|
||||
}
|
||||
|
||||
@@ -8,9 +8,20 @@ Prometheus:
|
||||
|
||||
DataSource: "${DB_URI}?sslmode=disable"
|
||||
|
||||
SnowflakeRpcConf:
|
||||
Target: k8s://juwan/snowflake-svc:8080
|
||||
|
||||
DB:
|
||||
Master: "postgresql://${PD_USERNAME}:${DB_PASSWORD}@user-db-rw.juwan:${DB_PORT}/${DB_NAME}?sslmode=disable"
|
||||
Slave: "postgresql://${PD_USERNAME}:${DB_PASSWORD}@user-db-ro.juwan:${DB_PORT}/${DB_NAME}?sslmode=disable"
|
||||
|
||||
CacheConf:
|
||||
- Host: "${REDIS_HOST}"
|
||||
Type: cluster
|
||||
- Host: "${REDIS_M_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
- Host: "${REDIS_S_HOST}"
|
||||
Type: node
|
||||
Pass: "${REDIS_PASSWORD}"
|
||||
User: "default"
|
||||
|
||||
|
||||
@@ -13,6 +13,11 @@ type JwtConfig struct {
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
DataSource string `json:"dataSource"`
|
||||
CacheConf cache.CacheConf
|
||||
Jwt JwtConfig `json:"jwt"`
|
||||
DB struct {
|
||||
Master string
|
||||
Slave string
|
||||
}
|
||||
CacheConf cache.CacheConf
|
||||
Jwt JwtConfig `json:"jwt"`
|
||||
SnowflakeRpcConf zrpc.RpcClientConf
|
||||
}
|
||||
|
||||
@@ -25,9 +25,7 @@ func NewGetUserByUsernameLogic(ctx context.Context, svcCtx *svc.ServiceContext)
|
||||
}
|
||||
|
||||
func (l *GetUserByUsernameLogic) GetUserByUsername(in *pb.GetUserByUsernameReq) (*pb.GetUserByUsernameResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
user, err := l.svcCtx.UsersModel.FindOneByUsername(l.ctx, in.Username)
|
||||
user, err := l.svcCtx.UsersModelRO.FindOneByUsername(l.ctx, in.Username)
|
||||
pbUsers := &pb.Users{}
|
||||
converter.StructToStruct(user, pbUsers)
|
||||
if err == nil || user != nil {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"juwan-backend/app/users/rpc/internal/svc"
|
||||
"juwan-backend/app/users/rpc/pb"
|
||||
"juwan-backend/common/converter"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
@@ -25,6 +26,12 @@ func NewGetUsersByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetU
|
||||
|
||||
func (l *GetUsersByIdLogic) GetUsersById(in *pb.GetUsersByIdReq) (*pb.GetUsersByIdResp, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
user, err := l.svcCtx.UsersModelRO.FindOne(l.ctx, in.Id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pbUser := &pb.Users{}
|
||||
converter.StructToStruct(&user, &pbUser)
|
||||
|
||||
return &pb.GetUsersByIdResp{}, nil
|
||||
return &pb.GetUsersByIdResp{Users: pbUser}, nil
|
||||
}
|
||||
|
||||
@@ -32,11 +32,11 @@ var (
|
||||
type (
|
||||
usersModel interface {
|
||||
Insert(ctx context.Context, data *Users) (sql.Result, error)
|
||||
FindOne(ctx context.Context, userId string) (*Users, error)
|
||||
FindOne(ctx context.Context, userId int64) (*Users, error)
|
||||
FindOneByPhone(ctx context.Context, phone string) (*Users, error)
|
||||
FindOneByUsername(ctx context.Context, username string) (*Users, error)
|
||||
Update(ctx context.Context, data *Users) error
|
||||
Delete(ctx context.Context, userId string) error
|
||||
Delete(ctx context.Context, userId int64) error
|
||||
}
|
||||
|
||||
defaultUsersModel struct {
|
||||
@@ -45,7 +45,7 @@ type (
|
||||
}
|
||||
|
||||
Users struct {
|
||||
UserId string `db:"user_id"`
|
||||
UserId int64 `db:"user_id"`
|
||||
Username string `db:"username"`
|
||||
Passwd string `db:"passwd"`
|
||||
Nickname string `db:"nickname"`
|
||||
@@ -66,7 +66,7 @@ func newUsersModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) Delete(ctx context.Context, userId string) error {
|
||||
func (m *defaultUsersModel) Delete(ctx context.Context, userId int64) error {
|
||||
data, err := m.FindOne(ctx, userId)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -82,7 +82,7 @@ func (m *defaultUsersModel) Delete(ctx context.Context, userId string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultUsersModel) FindOne(ctx context.Context, userId string) (*Users, error) {
|
||||
func (m *defaultUsersModel) FindOne(ctx context.Context, userId int64) (*Users, error) {
|
||||
publicUsersUserIdKey := fmt.Sprintf("%s%v", cachePublicUsersUserIdPrefix, userId)
|
||||
var resp Users
|
||||
err := m.QueryRowCtx(ctx, &resp, publicUsersUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"juwan-backend/app/snowflake/rpc/snowflake"
|
||||
"juwan-backend/app/users/rpc/internal/config"
|
||||
"juwan-backend/app/users/rpc/internal/models"
|
||||
"juwan-backend/app/users/rpc/internal/utils"
|
||||
"juwan-backend/common/redisx"
|
||||
"juwan-backend/common/snowflakex"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
@@ -14,30 +16,30 @@ import (
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
UsersModel models.UsersModel
|
||||
UsersModelRW models.UsersModel
|
||||
UsersModelRO models.UsersModel
|
||||
RedisCluster *redis.ClusterClient
|
||||
Snowflake snowflake.SnowflakeServiceClient
|
||||
JwtManager *utils.JwtManager
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
conn := sqlx.NewSqlConn("postgres", c.DataSource)
|
||||
RWDBConn := sqlx.NewSqlConn("postgres", c.DB.Master)
|
||||
RODBConn := sqlx.NewSqlConn("postgres", c.DB.Slave)
|
||||
logx.Infof("success to connect to postgres~")
|
||||
|
||||
// Initialize Redis Cluster client from CacheConf
|
||||
var redisCluster *redis.ClusterClient
|
||||
if len(c.CacheConf) > 0 {
|
||||
redisCluster = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: []string{c.CacheConf[0].Host},
|
||||
Password: c.CacheConf[0].Pass,
|
||||
})
|
||||
|
||||
// Test Redis Cluster connection
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := redisCluster.Ping(ctx).Err(); err != nil {
|
||||
redisConn, err := redisx.ConnectMasterSlaveCluster(c.CacheConf, 5*time.Second)
|
||||
redisCluster := redisConn.Client
|
||||
if redisCluster != nil {
|
||||
if err != nil {
|
||||
logx.Errorf("failed to connect to redis cluster: %v", err)
|
||||
} else {
|
||||
logx.Infof("success to connect to redis cluster~")
|
||||
if redisConn.HasSlave {
|
||||
logx.Infof("success to connect to redis master/slave (M: %s, S: %s)", redisConn.MasterHost, redisConn.SlaveHost)
|
||||
} else {
|
||||
logx.Infof("success to connect to redis master (M: %s), slave not configured", redisConn.MasterHost)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +48,10 @@ func NewServiceContext(c config.Config) *ServiceContext {
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
UsersModel: models.NewUsersModel(conn, c.CacheConf),
|
||||
UsersModelRW: models.NewUsersModel(RWDBConn, c.CacheConf),
|
||||
UsersModelRO: models.NewUsersModel(RODBConn, c.CacheConf),
|
||||
RedisCluster: redisCluster,
|
||||
JwtManager: jwtManager,
|
||||
Snowflake: snowflakex.NewClient(c.SnowflakeRpcConf),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user