验证码生成逻辑

master
dondon27 11 months ago
parent 421b7e9f45
commit ad666723dd

@ -0,0 +1,288 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.31.0
// protoc v3.21.6
// source: api/verifyCode/verifyCode.proto
package verifyCode
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
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 TYPE int32
const (
TYPE_DEFAULT TYPE = 0
TYPE_DIGIT TYPE = 1
TYPE_LETTER TYPE = 2
TYPE_MIXED TYPE = 3
)
// Enum value maps for TYPE.
var (
TYPE_name = map[int32]string{
0: "DEFAULT",
1: "DIGIT",
2: "LETTER",
3: "MIXED",
}
TYPE_value = map[string]int32{
"DEFAULT": 0,
"DIGIT": 1,
"LETTER": 2,
"MIXED": 3,
}
)
func (x TYPE) Enum() *TYPE {
p := new(TYPE)
*p = x
return p
}
func (x TYPE) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (TYPE) Descriptor() protoreflect.EnumDescriptor {
return file_api_verifyCode_verifyCode_proto_enumTypes[0].Descriptor()
}
func (TYPE) Type() protoreflect.EnumType {
return &file_api_verifyCode_verifyCode_proto_enumTypes[0]
}
func (x TYPE) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Use TYPE.Descriptor instead.
func (TYPE) EnumDescriptor() ([]byte, []int) {
return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{0}
}
type GetVerifyCodeRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Length uint32 `protobuf:"varint,1,opt,name=length,proto3" json:"length,omitempty"`
Type TYPE `protobuf:"varint,2,opt,name=type,proto3,enum=api.verifyCode.TYPE" json:"type,omitempty"`
}
func (x *GetVerifyCodeRequest) Reset() {
*x = GetVerifyCodeRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_api_verifyCode_verifyCode_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetVerifyCodeRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetVerifyCodeRequest) ProtoMessage() {}
func (x *GetVerifyCodeRequest) ProtoReflect() protoreflect.Message {
mi := &file_api_verifyCode_verifyCode_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetVerifyCodeRequest.ProtoReflect.Descriptor instead.
func (*GetVerifyCodeRequest) Descriptor() ([]byte, []int) {
return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{0}
}
func (x *GetVerifyCodeRequest) GetLength() uint32 {
if x != nil {
return x.Length
}
return 0
}
func (x *GetVerifyCodeRequest) GetType() TYPE {
if x != nil {
return x.Type
}
return TYPE_DEFAULT
}
type GetVerifyCodeReply struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Code string `protobuf:"bytes,1,opt,name=code,proto3" json:"code,omitempty"`
}
func (x *GetVerifyCodeReply) Reset() {
*x = GetVerifyCodeReply{}
if protoimpl.UnsafeEnabled {
mi := &file_api_verifyCode_verifyCode_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GetVerifyCodeReply) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GetVerifyCodeReply) ProtoMessage() {}
func (x *GetVerifyCodeReply) ProtoReflect() protoreflect.Message {
mi := &file_api_verifyCode_verifyCode_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GetVerifyCodeReply.ProtoReflect.Descriptor instead.
func (*GetVerifyCodeReply) Descriptor() ([]byte, []int) {
return file_api_verifyCode_verifyCode_proto_rawDescGZIP(), []int{1}
}
func (x *GetVerifyCodeReply) GetCode() string {
if x != nil {
return x.Code
}
return ""
}
var File_api_verifyCode_verifyCode_proto protoreflect.FileDescriptor
var file_api_verifyCode_verifyCode_proto_rawDesc = []byte{
0x0a, 0x1f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65,
0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x12, 0x0e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64,
0x65, 0x22, 0x58, 0x0a, 0x14, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f,
0x64, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6c, 0x65, 0x6e,
0x67, 0x74, 0x68, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x6c, 0x65, 0x6e, 0x67, 0x74,
0x68, 0x12, 0x28, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32,
0x14, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65,
0x2e, 0x54, 0x59, 0x50, 0x45, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x22, 0x28, 0x0a, 0x12, 0x47,
0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x65, 0x70, 0x6c,
0x79, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52,
0x04, 0x63, 0x6f, 0x64, 0x65, 0x2a, 0x35, 0x0a, 0x04, 0x54, 0x59, 0x50, 0x45, 0x12, 0x0b, 0x0a,
0x07, 0x44, 0x45, 0x46, 0x41, 0x55, 0x4c, 0x54, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x44, 0x49,
0x47, 0x49, 0x54, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x4c, 0x45, 0x54, 0x54, 0x45, 0x52, 0x10,
0x02, 0x12, 0x09, 0x0a, 0x05, 0x4d, 0x49, 0x58, 0x45, 0x44, 0x10, 0x03, 0x32, 0x67, 0x0a, 0x0a,
0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x59, 0x0a, 0x0d, 0x47, 0x65,
0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x24, 0x2e, 0x61, 0x70,
0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x2e, 0x47, 0x65, 0x74,
0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73,
0x74, 0x1a, 0x22, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f,
0x64, 0x65, 0x2e, 0x47, 0x65, 0x74, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65,
0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x38, 0x0a, 0x0e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x65, 0x72,
0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x50, 0x01, 0x5a, 0x24, 0x76, 0x65, 0x72, 0x69, 0x66,
0x79, 0x43, 0x6f, 0x64, 0x65, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79,
0x43, 0x6f, 0x64, 0x65, 0x3b, 0x76, 0x65, 0x72, 0x69, 0x66, 0x79, 0x43, 0x6f, 0x64, 0x65, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_api_verifyCode_verifyCode_proto_rawDescOnce sync.Once
file_api_verifyCode_verifyCode_proto_rawDescData = file_api_verifyCode_verifyCode_proto_rawDesc
)
func file_api_verifyCode_verifyCode_proto_rawDescGZIP() []byte {
file_api_verifyCode_verifyCode_proto_rawDescOnce.Do(func() {
file_api_verifyCode_verifyCode_proto_rawDescData = protoimpl.X.CompressGZIP(file_api_verifyCode_verifyCode_proto_rawDescData)
})
return file_api_verifyCode_verifyCode_proto_rawDescData
}
var file_api_verifyCode_verifyCode_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
var file_api_verifyCode_verifyCode_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_api_verifyCode_verifyCode_proto_goTypes = []interface{}{
(TYPE)(0), // 0: api.verifyCode.TYPE
(*GetVerifyCodeRequest)(nil), // 1: api.verifyCode.GetVerifyCodeRequest
(*GetVerifyCodeReply)(nil), // 2: api.verifyCode.GetVerifyCodeReply
}
var file_api_verifyCode_verifyCode_proto_depIdxs = []int32{
0, // 0: api.verifyCode.GetVerifyCodeRequest.type:type_name -> api.verifyCode.TYPE
1, // 1: api.verifyCode.VerifyCode.GetVerifyCode:input_type -> api.verifyCode.GetVerifyCodeRequest
2, // 2: api.verifyCode.VerifyCode.GetVerifyCode:output_type -> api.verifyCode.GetVerifyCodeReply
2, // [2:3] is the sub-list for method output_type
1, // [1:2] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_api_verifyCode_verifyCode_proto_init() }
func file_api_verifyCode_verifyCode_proto_init() {
if File_api_verifyCode_verifyCode_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_api_verifyCode_verifyCode_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetVerifyCodeRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_api_verifyCode_verifyCode_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GetVerifyCodeReply); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_api_verifyCode_verifyCode_proto_rawDesc,
NumEnums: 1,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_api_verifyCode_verifyCode_proto_goTypes,
DependencyIndexes: file_api_verifyCode_verifyCode_proto_depIdxs,
EnumInfos: file_api_verifyCode_verifyCode_proto_enumTypes,
MessageInfos: file_api_verifyCode_verifyCode_proto_msgTypes,
}.Build()
File_api_verifyCode_verifyCode_proto = out.File
file_api_verifyCode_verifyCode_proto_rawDesc = nil
file_api_verifyCode_verifyCode_proto_goTypes = nil
file_api_verifyCode_verifyCode_proto_depIdxs = nil
}

@ -0,0 +1,26 @@
syntax = "proto3";
package api.verifyCode;
option go_package = "verifyCode/api/verifyCode;verifyCode";
option java_multiple_files = true;
option java_package = "api.verifyCode";
service VerifyCode {
rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply);
}
enum TYPE {
DEFAULT = 0;
DIGIT = 1;
LETTER = 2;
MIXED = 3;
}
message GetVerifyCodeRequest {
uint32 length = 1;
TYPE type = 2;
}
message GetVerifyCodeReply {
string code = 1;
}

@ -0,0 +1,109 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.3.0
// - protoc v3.21.6
// source: api/verifyCode/verifyCode.proto
package verifyCode
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.32.0 or later.
const _ = grpc.SupportPackageIsVersion7
const (
VerifyCode_GetVerifyCode_FullMethodName = "/api.verifyCode.VerifyCode/GetVerifyCode"
)
// VerifyCodeClient is the client API for VerifyCode 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 VerifyCodeClient interface {
GetVerifyCode(ctx context.Context, in *GetVerifyCodeRequest, opts ...grpc.CallOption) (*GetVerifyCodeReply, error)
}
type verifyCodeClient struct {
cc grpc.ClientConnInterface
}
func NewVerifyCodeClient(cc grpc.ClientConnInterface) VerifyCodeClient {
return &verifyCodeClient{cc}
}
func (c *verifyCodeClient) GetVerifyCode(ctx context.Context, in *GetVerifyCodeRequest, opts ...grpc.CallOption) (*GetVerifyCodeReply, error) {
out := new(GetVerifyCodeReply)
err := c.cc.Invoke(ctx, VerifyCode_GetVerifyCode_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// VerifyCodeServer is the server API for VerifyCode service.
// All implementations must embed UnimplementedVerifyCodeServer
// for forward compatibility
type VerifyCodeServer interface {
GetVerifyCode(context.Context, *GetVerifyCodeRequest) (*GetVerifyCodeReply, error)
mustEmbedUnimplementedVerifyCodeServer()
}
// UnimplementedVerifyCodeServer must be embedded to have forward compatible implementations.
type UnimplementedVerifyCodeServer struct {
}
func (UnimplementedVerifyCodeServer) GetVerifyCode(context.Context, *GetVerifyCodeRequest) (*GetVerifyCodeReply, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetVerifyCode not implemented")
}
func (UnimplementedVerifyCodeServer) mustEmbedUnimplementedVerifyCodeServer() {}
// UnsafeVerifyCodeServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to VerifyCodeServer will
// result in compilation errors.
type UnsafeVerifyCodeServer interface {
mustEmbedUnimplementedVerifyCodeServer()
}
func RegisterVerifyCodeServer(s grpc.ServiceRegistrar, srv VerifyCodeServer) {
s.RegisterService(&VerifyCode_ServiceDesc, srv)
}
func _VerifyCode_GetVerifyCode_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetVerifyCodeRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(VerifyCodeServer).GetVerifyCode(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: VerifyCode_GetVerifyCode_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(VerifyCodeServer).GetVerifyCode(ctx, req.(*GetVerifyCodeRequest))
}
return interceptor(ctx, in, info, handler)
}
// VerifyCode_ServiceDesc is the grpc.ServiceDesc for VerifyCode service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var VerifyCode_ServiceDesc = grpc.ServiceDesc{
ServiceName: "api.verifyCode.VerifyCode",
HandlerType: (*VerifyCodeServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "GetVerifyCode",
Handler: _VerifyCode_GetVerifyCode_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api/verifyCode/verifyCode.proto",
}

@ -2,7 +2,9 @@ package main
import (
"flag"
"math/rand"
"os"
"time"
"verifyCode/internal/conf"
@ -80,6 +82,7 @@ func main() {
}
defer cleanup()
rand.Seed(time.Now().UnixNano())
// start and wait for stop signal
if err := app.Run(); err != nil {
panic(err)

@ -31,7 +31,8 @@ func wireApp(confServer *conf.Server, confData *conf.Data, logger log.Logger) (*
greeterRepo := data.NewGreeterRepo(dataData, logger)
greeterUsecase := biz.NewGreeterUsecase(greeterRepo, logger)
greeterService := service.NewGreeterService(greeterUsecase)
grpcServer := server.NewGRPCServer(confServer, greeterService, logger)
verifyCodeService := service.NewVerifyCodeService()
grpcServer := server.NewGRPCServer(confServer, greeterService, verifyCodeService, logger)
httpServer := server.NewHTTPServer(confServer, greeterService, logger)
app := newApp(logger, grpcServer, httpServer)
return app, func() {

@ -2,6 +2,7 @@ package server
import (
v1 "verifyCode/api/helloworld/v1"
"verifyCode/api/verifyCode"
"verifyCode/internal/conf"
"verifyCode/internal/service"
@ -11,7 +12,10 @@ import (
)
// NewGRPCServer new a gRPC server.
func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.Logger) *grpc.Server {
func NewGRPCServer(c *conf.Server,
greeter *service.GreeterService,
verifyCodeService *service.VerifyCodeService,
logger log.Logger) *grpc.Server {
var opts = []grpc.ServerOption{
grpc.Middleware(
recovery.Recovery(),
@ -28,5 +32,6 @@ func NewGRPCServer(c *conf.Server, greeter *service.GreeterService, logger log.L
}
srv := grpc.NewServer(opts...)
v1.RegisterGreeterServer(srv, greeter)
verifyCode.RegisterVerifyCodeServer(srv,verifyCodeService)
return srv
}

@ -3,4 +3,4 @@ package service
import "github.com/google/wire"
// ProviderSet is service providers.
var ProviderSet = wire.NewSet(NewGreeterService)
var ProviderSet = wire.NewSet(NewGreeterService, NewVerifyCodeService)

@ -0,0 +1,69 @@
package service
import (
"context"
"fmt"
"math/rand"
pb "verifyCode/api/verifyCode"
)
type VerifyCodeService struct {
pb.UnimplementedVerifyCodeServer
}
func NewVerifyCodeService() *VerifyCodeService {
return &VerifyCodeService{}
}
func (s *VerifyCodeService) GetVerifyCode(ctx context.Context, req *pb.GetVerifyCodeRequest) (*pb.GetVerifyCodeReply, error) {
return &pb.GetVerifyCodeReply{
Code: RandCode(int(req.Length), req.Type),
}, nil
}
func RandCode(l int, t pb.TYPE) string {
switch t {
case pb.TYPE_DEFAULT:
fallthrough
case pb.TYPE_DIGIT:
return randCode("0123456789", l, 4)
case pb.TYPE_LETTER:
return randCode("abcdefghijklmnopqrstuvwxyz", l, 5)
case pb.TYPE_MIXED:
return randCode("0123456789abcdefghijklmnopqrstuvwxyz", l, 5)
default:
}
return ""
}
func randCode(chars string, l int, idxBits int) string {
idxMask := 1<<idxBits - 1
idxMax := 63 / idxBits
result := make([]byte, l)
for i, cache, remain := 0, rand.Int63(), idxMax; i < l; {
fmt.Println("for:rand.Int63")
if 0 == remain {
fmt.Println("if:rand.Int63")
cache = rand.Int63()
remain = idxMax
}
if randIndex := int(cache & int64(idxMask)); randIndex < len(chars) {
result[i] = chars[randIndex]
i++
}
cache >>= idxBits
remain--
}
return string(result)
}
//func randCode(chars string, l int) string {
// charsLen := len(chars)
// result := make([]byte, l)
// for i := 0; i < l; i++ {
// randIndex := rand.Intn(charsLen)
// result[i] = chars[randIndex]
// }
// return string(result)
//}

@ -3,37 +3,8 @@
openapi: 3.0.3
info:
title: Greeter API
description: The greeting service definition.
title: ""
version: 0.0.1
paths:
/helloworld/{name}:
get:
tags:
- Greeter
- subgroup
description: Sends a greeting
operationId: Greeter_SayHello
parameters:
- name: name
in: path
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/helloworld.v1.HelloReply'
paths: {}
components:
schemas:
helloworld.v1.HelloReply:
type: object
properties:
message:
type: string
description: The response message containing the greetings
tags:
- name: Greeter
schemas: {}

Loading…
Cancel
Save