验证码随机生成的业务逻辑,优化版

main
chenpeng 5 months ago
parent 708a980522
commit 5ad6640c99

@ -0,0 +1,293 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.34.2
// protoc v5.26.1
// 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}
}
// 定义 GetVerifyCodeRequest 消息
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
}
// 定义 GetVerifyCodeReply 消息
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, 0x26, 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 = []any{
(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 any, i int) any {
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 any, i int) any {
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,29 @@
syntax = "proto3";
package api.verifyCode;
option go_package = "verifyCode/api/verifyCode;verifyCode";
//
enum TYPE {
DEFAULT = 0;
DIGIT = 1;
LETTER = 2;
MIXED = 3;
};
// GetVerifyCodeRequest
message GetVerifyCodeRequest {
//
uint32 length = 1;
//
TYPE type = 2;
}
// GetVerifyCodeReply
message GetVerifyCodeReply {
//
string code = 1;
}
service VerifyCode {
rpc GetVerifyCode (GetVerifyCodeRequest) returns (GetVerifyCodeReply);
}

@ -0,0 +1,110 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions:
// - protoc-gen-go-grpc v1.4.0
// - protoc v5.26.1
// 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.62.0 or later.
const _ = grpc.SupportPackageIsVersion8
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) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetVerifyCodeReply)
err := c.cc.Invoke(ctx, VerifyCode_GetVerifyCode_FullMethodName, in, out, cOpts...)
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"
@ -79,7 +81,7 @@ func main() {
panic(err)
}
defer cleanup()
rand.NewSource(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() {

@ -0,0 +1,9 @@
package main
import (
"fmt"
)
func main() {
fmt.Printf("%E", 121414.4353553535)
}

@ -6,9 +6,9 @@ require (
github.com/go-kratos/kratos/v2 v2.7.2
github.com/google/wire v0.6.0
go.uber.org/automaxprocs v1.5.1
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529
google.golang.org/grpc v1.56.3
google.golang.org/protobuf v1.32.0
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
)
require (
@ -17,7 +17,6 @@ require (
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-playground/form/v4 v4.2.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
@ -25,11 +24,10 @@ require (
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto v0.0.0-20230629202037-9506855d4529 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

@ -1,9 +1,9 @@
github.com/census-instrumentation/opencensus-proto v0.4.1 h1:iKLQ0xPNFxR/2hzXZMrBo8f1j86j5WHzznCCQxV/b8g=
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+gqO04wryn5h75LSazbRlnya1k=
github.com/cncf/xds/go v0.0.0-20240318125728-8a4994d93e50 h1:DBmgJDC9dTfkVyGgipamEh2BpGYxScCH1TOF1LL1cXc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/envoyproxy/go-control-plane v0.11.2-0.20230627204322-7d0032219fcb h1:kxNVXsNro/lpR5WD+P1FI/yUHn2G03Glber3k8cQL2Y=
github.com/envoyproxy/protoc-gen-validate v0.10.1 h1:c0g45+xCJhdgFGw7a5QAfdS4byAbud7miNWJ1WwEVf8=
github.com/envoyproxy/go-control-plane v0.12.0 h1:4X+VP1GHd1Mhj6IB5mMeGbLCleqxjletLK6K0rbxyZI=
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-kratos/aegis v0.2.0 h1:dObzCDWn3XVjUkgxyBp6ZeWtx/do0DPZ7LY3yNSJLUQ=
@ -19,12 +19,9 @@ github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBY
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/form/v4 v4.2.1 h1:HjdRDKO0fftVMU5epjPW2SOREcZ6/wLUzEobqUGJuPw=
github.com/go-playground/form/v4 v4.2.1/go.mod h1:q1a2BY+AQUUzhl6xA/6hBetay6dEIhMHjgvJiGo6K7U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
@ -66,14 +63,16 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ=
golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@ -83,8 +82,9 @@ golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
@ -97,8 +97,9 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
@ -106,19 +107,14 @@ golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20230629202037-9506855d4529 h1:9JucMWR7sPvCxUFd6UsOUNmA5kCcWOfORaT3tpAsKQs=
google.golang.org/genproto v0.0.0-20230629202037-9506855d4529/go.mod h1:xZnkP7mREFX5MORlOPEzLMr+90PPZQ2QWzrVTWfAq64=
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 h1:s5YSX+ZH5b5vS9rnpGymvIyMpLRJizowqDlOuyjXnTk=
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529/go.mod h1:vHYtlOoi6TsQ3Uk2yxR7NI5z8uoV+3pZtR4jmHIkRig=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529 h1:DEH99RbiLZhMxrpEJCZ0A+wdTe0EOgou/poSLx9vWf4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230629202037-9506855d4529/go.mod h1:66JfowdXAEgad5O9NnYcsNPLCPZJD++2L9X0PCMODrA=
google.golang.org/grpc v1.56.3 h1:8I4C0Yq1EjstUzUJzpcRVbuYA2mODtEmpWiQoN/b2nc=
google.golang.org/grpc v1.56.3/go.mod h1:I9bI3vqKfayGqPUAwGdOSu7kt6oIJLixfffKrpXqQ9s=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237 h1:RFiFrvy37/mpSpdySBDrUdipW/dHwsRwh3J3+A9VgT4=
google.golang.org/genproto/googleapis/api v0.0.0-20240318140521-94a12d6c2237/go.mod h1:Z5Iiy3jtmioajWHDGFk7CeugTyHtPvMHA4UTmUkyalE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 h1:Di6ANFilr+S60a4S61ZM00vLdw0IrQOSMS2/6mrnOU0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

@ -2,6 +2,7 @@ package server
import (
v1 "verifyCode/api/helloworld/v1"
"verifyCode/api/verifyCode"
"verifyCode/internal/conf"
"verifyCode/internal/service"
@ -11,7 +12,7 @@ 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 +29,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,86 @@
package service
import (
"context"
"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) {
//log.Info("current verifyCode service Run")
return &pb.GetVerifyCodeReply{
Code: RandCode(int(req.Length), req.Type),
}, nil
}
// RandCode 开放的被调用的方法,用于区分类型
func RandCode(l int, t pb.TYPE) string {
switch t {
case pb.TYPE_DEFAULT:
fallthrough
case pb.TYPE_DIGIT:
// idxBits表示使用4位二进制数就可以表示完chars的索引了
return randCode("0123456789", l, 4)
case pb.TYPE_LETTER:
return randCode("abcdefghijklmnopqrstuvwxyz", l, 5)
case pb.TYPE_MIXED:
return randCode("0123456789abcdefghijklmnopqrstuvwxyz", l, 6)
default:
}
return ""
}
// 随机数的核心方法(优化实现)
// 一次随机多个随机位,分部分多次使用,
// idxBits表示使用4位二进制数就可以表示完chars的索引了
func randCode(chars string, l, idxBits int) string {
// 计算有效的二进制数位,基于 chars 的长度
// 推荐写死因为chars固定对应的位数长度也固定
// 形成掩码mask
// 例如使用低idxBits位00000000000111111
idxMask := 1<<idxBits - 1 // 00001000000 - 1 = 00000111111
// 63 位可以用多少次
idxMax := 63 / idxBits
result := make([]byte, l)
// 生成随机字符,cache:随机位缓存 ;remain:当前还可以用几次
for i, cache, remain := 0, rand.Int63(), idxMax; i < l; {
// 如果使用的剩余次数为0则重新获取随机
if remain == 0 {
cache, remain = rand.Int63(), idxMax
}
// 利用掩码获取cache的低位作为randIndex索引
if randIndex := int(cache & int64(idxMask)); randIndex < len(chars) {
result[i] = chars[randIndex]
i++
}
// 使用下一组随机位。右移会丢掉先前的低位高位补0
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++ {
// // 核心函数 rand.Intn() 生成[0, n)的整型随机数
// randIndex := rand.Intn(charsLen)
// // 字符串的单个字符是uint8类型即byte类型,因此可以赋值
// 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