diff --git a/Makefile b/Makefile index dd26e18c..3104045c 100644 --- a/Makefile +++ b/Makefile @@ -73,12 +73,22 @@ generate: gen-mir gen-grpc .PHONY: gen-mir gen-mir: - @go generate internal/mirc/main.go - @go fmt ./internal/mirc/... + @go generate mirc/main.go + @go fmt ./auto/api/... .PHONY: gen-grpc gen-grpc: - @go fmt ./internal/proto/... + @rm -rf auto/rpc + @buf generate proto + @go fmt ./auto/rpc/... + +.PHONY: proto-mod +proto-mod: + @cd proto/ && buf mod update + +.PHONY: proto-lint +proto-lint: + @cd proto/ && buf lint clean: @go clean @@ -95,7 +105,15 @@ test: @go test ./... pre-commit: fmt - go mod tidy + @go mod tidy + +.PHONY: install-protobuf-plugins +install-protobuf-plugins: + @go install github.com/bufbuild/buf/cmd/buf@v1.11.0 + @go install github.com/bufbuild/buf/cmd/protoc-gen-buf-breaking@v1.11.0 + @go install github.com/bufbuild/buf/cmd/protoc-gen-buf-lint@v1.11.0 + @go install google.golang.org/protobuf/cmd/protoc-gen-go@latest + @go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest help: @echo "make: make" diff --git a/internal/mirc/auto/api/m/v1/user.go b/auto/api/m/v1/user.go similarity index 100% rename from internal/mirc/auto/api/m/v1/user.go rename to auto/api/m/v1/user.go diff --git a/internal/mirc/auto/api/r/v1/user.go b/auto/api/r/v1/user.go similarity index 100% rename from internal/mirc/auto/api/r/v1/user.go rename to auto/api/r/v1/user.go diff --git a/internal/mirc/auto/api/s/v1/user.go b/auto/api/s/v1/user.go similarity index 100% rename from internal/mirc/auto/api/s/v1/user.go rename to auto/api/s/v1/user.go diff --git a/internal/mirc/auto/api/v1/web_core.go b/auto/api/v1/web_core.go similarity index 100% rename from internal/mirc/auto/api/v1/web_core.go rename to auto/api/v1/web_core.go diff --git a/internal/mirc/auto/api/x/v1/user.go b/auto/api/x/v1/user.go similarity index 100% rename from internal/mirc/auto/api/x/v1/user.go rename to auto/api/x/v1/user.go diff --git a/auto/rpc/v1/auth.pb.go b/auto/rpc/v1/auth.pb.go new file mode 100644 index 00000000..ab5109a6 --- /dev/null +++ b/auto/rpc/v1/auth.pb.go @@ -0,0 +1,371 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.28.1 +// protoc (unknown) +// source: v1/auth.proto + +package v1 + +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 User struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + PhoneNum string `protobuf:"bytes,1,opt,name=phoneNum,proto3" json:"phoneNum,omitempty"` +} + +func (x *User) Reset() { + *x = User{} + if protoimpl.UnsafeEnabled { + mi := &file_v1_auth_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *User) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*User) ProtoMessage() {} + +func (x *User) ProtoReflect() protoreflect.Message { + mi := &file_v1_auth_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 User.ProtoReflect.Descriptor instead. +func (*User) Descriptor() ([]byte, []int) { + return file_v1_auth_proto_rawDescGZIP(), []int{0} +} + +func (x *User) GetPhoneNum() string { + if x != nil { + return x.PhoneNum + } + return "" +} + +type UserVerify struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + PhoneNum string `protobuf:"bytes,1,opt,name=phoneNum,proto3" json:"phoneNum,omitempty"` + VerificationCode string `protobuf:"bytes,2,opt,name=VerificationCode,proto3" json:"VerificationCode,omitempty"` +} + +func (x *UserVerify) Reset() { + *x = UserVerify{} + if protoimpl.UnsafeEnabled { + mi := &file_v1_auth_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *UserVerify) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*UserVerify) ProtoMessage() {} + +func (x *UserVerify) ProtoReflect() protoreflect.Message { + mi := &file_v1_auth_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 UserVerify.ProtoReflect.Descriptor instead. +func (*UserVerify) Descriptor() ([]byte, []int) { + return file_v1_auth_proto_rawDescGZIP(), []int{1} +} + +func (x *UserVerify) GetPhoneNum() string { + if x != nil { + return x.PhoneNum + } + return "" +} + +func (x *UserVerify) GetVerificationCode() string { + if x != nil { + return x.VerificationCode + } + return "" +} + +type LoginReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StatuCode int32 `protobuf:"varint,1,opt,name=statuCode,proto3" json:"statuCode,omitempty"` + Token string `protobuf:"bytes,2,opt,name=token,proto3" json:"token,omitempty"` +} + +func (x *LoginReply) Reset() { + *x = LoginReply{} + if protoimpl.UnsafeEnabled { + mi := &file_v1_auth_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *LoginReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*LoginReply) ProtoMessage() {} + +func (x *LoginReply) ProtoReflect() protoreflect.Message { + mi := &file_v1_auth_proto_msgTypes[2] + 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 LoginReply.ProtoReflect.Descriptor instead. +func (*LoginReply) Descriptor() ([]byte, []int) { + return file_v1_auth_proto_rawDescGZIP(), []int{2} +} + +func (x *LoginReply) GetStatuCode() int32 { + if x != nil { + return x.StatuCode + } + return 0 +} + +func (x *LoginReply) GetToken() string { + if x != nil { + return x.Token + } + return "" +} + +type ActionReply struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + StatusCode int32 `protobuf:"varint,1,opt,name=statusCode,proto3" json:"statusCode,omitempty"` +} + +func (x *ActionReply) Reset() { + *x = ActionReply{} + if protoimpl.UnsafeEnabled { + mi := &file_v1_auth_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ActionReply) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ActionReply) ProtoMessage() {} + +func (x *ActionReply) ProtoReflect() protoreflect.Message { + mi := &file_v1_auth_proto_msgTypes[3] + 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 ActionReply.ProtoReflect.Descriptor instead. +func (*ActionReply) Descriptor() ([]byte, []int) { + return file_v1_auth_proto_rawDescGZIP(), []int{3} +} + +func (x *ActionReply) GetStatusCode() int32 { + if x != nil { + return x.StatusCode + } + return 0 +} + +var File_v1_auth_proto protoreflect.FileDescriptor + +var file_v1_auth_proto_rawDesc = []byte{ + 0x0a, 0x0d, 0x76, 0x31, 0x2f, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, + 0x04, 0x61, 0x75, 0x74, 0x68, 0x22, 0x22, 0x0a, 0x04, 0x55, 0x73, 0x65, 0x72, 0x12, 0x1a, 0x0a, + 0x08, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x4e, 0x75, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x08, 0x70, 0x68, 0x6f, 0x6e, 0x65, 0x4e, 0x75, 0x6d, 0x22, 0x54, 0x0a, 0x0a, 0x55, 0x73, 0x65, + 0x72, 0x56, 0x65, 0x72, 0x69, 0x66, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x70, 0x68, 0x6f, 0x6e, 0x65, + 0x4e, 0x75, 0x6d, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x70, 0x68, 0x6f, 0x6e, 0x65, + 0x4e, 0x75, 0x6d, 0x12, 0x2a, 0x0a, 0x10, 0x56, 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, + 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x10, 0x56, + 0x65, 0x72, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x43, 0x6f, 0x64, 0x65, 0x22, + 0x40, 0x0a, 0x0a, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x1c, 0x0a, + 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, + 0x52, 0x09, 0x73, 0x74, 0x61, 0x74, 0x75, 0x43, 0x6f, 0x64, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x74, + 0x6f, 0x6b, 0x65, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x6f, 0x6b, 0x65, + 0x6e, 0x22, 0x2d, 0x0a, 0x0b, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, + 0x12, 0x1e, 0x0a, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, 0x18, 0x01, + 0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x43, 0x6f, 0x64, 0x65, + 0x32, 0x89, 0x01, 0x0a, 0x0c, 0x41, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74, 0x69, 0x63, 0x61, 0x74, + 0x65, 0x12, 0x29, 0x0a, 0x08, 0x70, 0x72, 0x65, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x0a, 0x2e, + 0x61, 0x75, 0x74, 0x68, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x1a, 0x11, 0x2e, 0x61, 0x75, 0x74, 0x68, + 0x2e, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x12, 0x25, 0x0a, 0x05, + 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x12, 0x0a, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x55, 0x73, 0x65, + 0x72, 0x1a, 0x10, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x4c, 0x6f, 0x67, 0x69, 0x6e, 0x52, 0x65, + 0x70, 0x6c, 0x79, 0x12, 0x27, 0x0a, 0x06, 0x6c, 0x6f, 0x67, 0x6f, 0x75, 0x74, 0x12, 0x0a, 0x2e, + 0x61, 0x75, 0x74, 0x68, 0x2e, 0x55, 0x73, 0x65, 0x72, 0x1a, 0x11, 0x2e, 0x61, 0x75, 0x74, 0x68, + 0x2e, 0x41, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x65, 0x70, 0x6c, 0x79, 0x42, 0x6f, 0x0a, 0x08, + 0x63, 0x6f, 0x6d, 0x2e, 0x61, 0x75, 0x74, 0x68, 0x42, 0x09, 0x41, 0x75, 0x74, 0x68, 0x50, 0x72, + 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, + 0x6d, 0x2f, 0x72, 0x6f, 0x63, 0x62, 0x6f, 0x73, 0x73, 0x2f, 0x70, 0x61, 0x6f, 0x70, 0x61, 0x6f, + 0x2d, 0x63, 0x65, 0x2f, 0x61, 0x75, 0x74, 0x6f, 0x2f, 0x72, 0x70, 0x63, 0x2f, 0x76, 0x31, 0xa2, + 0x02, 0x03, 0x41, 0x58, 0x58, 0xaa, 0x02, 0x04, 0x41, 0x75, 0x74, 0x68, 0xca, 0x02, 0x04, 0x41, + 0x75, 0x74, 0x68, 0xe2, 0x02, 0x10, 0x41, 0x75, 0x74, 0x68, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65, + 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x04, 0x41, 0x75, 0x74, 0x68, 0x62, 0x06, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_v1_auth_proto_rawDescOnce sync.Once + file_v1_auth_proto_rawDescData = file_v1_auth_proto_rawDesc +) + +func file_v1_auth_proto_rawDescGZIP() []byte { + file_v1_auth_proto_rawDescOnce.Do(func() { + file_v1_auth_proto_rawDescData = protoimpl.X.CompressGZIP(file_v1_auth_proto_rawDescData) + }) + return file_v1_auth_proto_rawDescData +} + +var file_v1_auth_proto_msgTypes = make([]protoimpl.MessageInfo, 4) +var file_v1_auth_proto_goTypes = []interface{}{ + (*User)(nil), // 0: auth.User + (*UserVerify)(nil), // 1: auth.UserVerify + (*LoginReply)(nil), // 2: auth.LoginReply + (*ActionReply)(nil), // 3: auth.ActionReply +} +var file_v1_auth_proto_depIdxs = []int32{ + 0, // 0: auth.Authenticate.preLogin:input_type -> auth.User + 0, // 1: auth.Authenticate.login:input_type -> auth.User + 0, // 2: auth.Authenticate.logout:input_type -> auth.User + 3, // 3: auth.Authenticate.preLogin:output_type -> auth.ActionReply + 2, // 4: auth.Authenticate.login:output_type -> auth.LoginReply + 3, // 5: auth.Authenticate.logout:output_type -> auth.ActionReply + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] 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_v1_auth_proto_init() } +func file_v1_auth_proto_init() { + if File_v1_auth_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_v1_auth_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*User); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_v1_auth_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*UserVerify); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_v1_auth_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*LoginReply); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_v1_auth_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ActionReply); 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_v1_auth_proto_rawDesc, + NumEnums: 0, + NumMessages: 4, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_v1_auth_proto_goTypes, + DependencyIndexes: file_v1_auth_proto_depIdxs, + MessageInfos: file_v1_auth_proto_msgTypes, + }.Build() + File_v1_auth_proto = out.File + file_v1_auth_proto_rawDesc = nil + file_v1_auth_proto_goTypes = nil + file_v1_auth_proto_depIdxs = nil +} diff --git a/auto/rpc/v1/auth_grpc.pb.go b/auto/rpc/v1/auth_grpc.pb.go new file mode 100644 index 00000000..64fdc1d1 --- /dev/null +++ b/auto/rpc/v1/auth_grpc.pb.go @@ -0,0 +1,177 @@ +// Code generated by protoc-gen-go-grpc. DO NOT EDIT. +// versions: +// - protoc-gen-go-grpc v1.2.0 +// - protoc (unknown) +// source: v1/auth.proto + +package v1 + +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 + +// AuthenticateClient is the client API for Authenticate 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 AuthenticateClient interface { + PreLogin(ctx context.Context, in *User, opts ...grpc.CallOption) (*ActionReply, error) + Login(ctx context.Context, in *User, opts ...grpc.CallOption) (*LoginReply, error) + Logout(ctx context.Context, in *User, opts ...grpc.CallOption) (*ActionReply, error) +} + +type authenticateClient struct { + cc grpc.ClientConnInterface +} + +func NewAuthenticateClient(cc grpc.ClientConnInterface) AuthenticateClient { + return &authenticateClient{cc} +} + +func (c *authenticateClient) PreLogin(ctx context.Context, in *User, opts ...grpc.CallOption) (*ActionReply, error) { + out := new(ActionReply) + err := c.cc.Invoke(ctx, "/auth.Authenticate/preLogin", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *authenticateClient) Login(ctx context.Context, in *User, opts ...grpc.CallOption) (*LoginReply, error) { + out := new(LoginReply) + err := c.cc.Invoke(ctx, "/auth.Authenticate/login", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *authenticateClient) Logout(ctx context.Context, in *User, opts ...grpc.CallOption) (*ActionReply, error) { + out := new(ActionReply) + err := c.cc.Invoke(ctx, "/auth.Authenticate/logout", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// AuthenticateServer is the server API for Authenticate service. +// All implementations must embed UnimplementedAuthenticateServer +// for forward compatibility +type AuthenticateServer interface { + PreLogin(context.Context, *User) (*ActionReply, error) + Login(context.Context, *User) (*LoginReply, error) + Logout(context.Context, *User) (*ActionReply, error) + mustEmbedUnimplementedAuthenticateServer() +} + +// UnimplementedAuthenticateServer must be embedded to have forward compatible implementations. +type UnimplementedAuthenticateServer struct { +} + +func (UnimplementedAuthenticateServer) PreLogin(context.Context, *User) (*ActionReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method PreLogin not implemented") +} +func (UnimplementedAuthenticateServer) Login(context.Context, *User) (*LoginReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Login not implemented") +} +func (UnimplementedAuthenticateServer) Logout(context.Context, *User) (*ActionReply, error) { + return nil, status.Errorf(codes.Unimplemented, "method Logout not implemented") +} +func (UnimplementedAuthenticateServer) mustEmbedUnimplementedAuthenticateServer() {} + +// UnsafeAuthenticateServer may be embedded to opt out of forward compatibility for this service. +// Use of this interface is not recommended, as added methods to AuthenticateServer will +// result in compilation errors. +type UnsafeAuthenticateServer interface { + mustEmbedUnimplementedAuthenticateServer() +} + +func RegisterAuthenticateServer(s grpc.ServiceRegistrar, srv AuthenticateServer) { + s.RegisterService(&Authenticate_ServiceDesc, srv) +} + +func _Authenticate_PreLogin_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(User) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AuthenticateServer).PreLogin(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/auth.Authenticate/preLogin", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AuthenticateServer).PreLogin(ctx, req.(*User)) + } + return interceptor(ctx, in, info, handler) +} + +func _Authenticate_Login_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(User) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AuthenticateServer).Login(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/auth.Authenticate/login", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AuthenticateServer).Login(ctx, req.(*User)) + } + return interceptor(ctx, in, info, handler) +} + +func _Authenticate_Logout_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(User) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(AuthenticateServer).Logout(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/auth.Authenticate/logout", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(AuthenticateServer).Logout(ctx, req.(*User)) + } + return interceptor(ctx, in, info, handler) +} + +// Authenticate_ServiceDesc is the grpc.ServiceDesc for Authenticate service. +// It's only intended for direct use with grpc.RegisterService, +// and not to be introspected or modified (even as a copy) +var Authenticate_ServiceDesc = grpc.ServiceDesc{ + ServiceName: "auth.Authenticate", + HandlerType: (*AuthenticateServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "preLogin", + Handler: _Authenticate_PreLogin_Handler, + }, + { + MethodName: "login", + Handler: _Authenticate_Login_Handler, + }, + { + MethodName: "logout", + Handler: _Authenticate_Logout_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "v1/auth.proto", +} diff --git a/buf.gen.yaml b/buf.gen.yaml new file mode 100644 index 00000000..bf9ba452 --- /dev/null +++ b/buf.gen.yaml @@ -0,0 +1,16 @@ +version: v1 +managed: + enabled: true + go_package_prefix: + default: github.com/rocboss/paopao-ce/auto/rpc + except: + - buf.build/googleapis/googleapis +plugins: + - plugin: go + out: auto/rpc + opt: paths=source_relative + - plugin: go-grpc + out: auto/rpc + opt: + - paths=source_relative + - require_unimplemented_servers=true diff --git a/buf.work.yaml b/buf.work.yaml new file mode 100644 index 00000000..1878b341 --- /dev/null +++ b/buf.work.yaml @@ -0,0 +1,3 @@ +version: v1 +directories: + - proto diff --git a/config.yaml.sample b/config.yaml.sample index a3df0dd9..30535257 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -19,7 +19,7 @@ Features: Base: ["Redis", "PhoneBind"] Docs: ["Docs:OpenAPI"] Deprecated: ["Deprecated:OldWeb"] - Service: ["Admin", "SpaceX", "Bot", "LocalOSS"] + Service: ["Admin", "SpaceX", "Bot", "LocalOSS", "Mobile"] Option: ["SimpleCacheIndex"] Sms: "SmsJuhe" WebServer: # Web服务 @@ -52,6 +52,9 @@ DocsServer: # 开发文档服务 HttpPort: 8011 ReadTimeout: 60 WriteTimeout: 60 +MobileServer: # 移动端grpc api服务 + Host: 0.0.0.0 + Port: 8020 SmsJuhe: Gateway: https://v.juhe.cn/sms/send Key: diff --git a/go.mod b/go.mod index f5279d4d..eee36fdb 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/spf13/viper v1.14.0 github.com/tencentyun/cos-go-sdk-v5 v0.7.35 github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc + google.golang.org/grpc v1.50.1 google.golang.org/protobuf v1.28.1 gopkg.in/natefinch/lumberjack.v2 v2.0.0 gopkg.in/resty.v1 v1.12.0 @@ -117,6 +118,7 @@ require ( golang.org/x/text v0.4.0 // indirect golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect golang.org/x/tools v0.1.12 // indirect + google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 88972d10..ca0a32cb 100644 --- a/go.sum +++ b/go.sum @@ -2066,6 +2066,7 @@ google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa/go.mod h1:5CzLGKJ6 google.golang.org/genproto v0.0.0-20220111164026-67b88f271998/go.mod h1:5CzLGKJ67TSI2B9POpiiyGha0AjJvZIUgRMt1dSmuhc= google.golang.org/genproto v0.0.0-20220314164441-57ef72a4c106/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E= google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e h1:S9GbmC1iCgvbLyAokVCwiO6tVIrU9Y7c5oMx1V/ki/Y= +google.golang.org/genproto v0.0.0-20221024183307-1bc688fe9f3e/go.mod h1:9qHF0xnpdSfF6knlcsnpzUu5y+rpwgbvsyGAZPBMg4s= google.golang.org/grpc v0.0.0-20160317175043-d3ddb4469d5a/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= @@ -2100,6 +2101,7 @@ google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ5 google.golang.org/grpc v1.43.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU= google.golang.org/grpc v1.45.0/go.mod h1:lN7owxKUQEqMfSyQikvvk5tf/6zMPsrK+ONuO11+0rQ= google.golang.org/grpc v1.50.1 h1:DS/BukOZWp8s6p4Dt/tOaJaTQyPyOoCcrjroHuCeLzY= +google.golang.org/grpc v1.50.1/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI= google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= diff --git a/internal/conf/conf.go b/internal/conf/conf.go index bdadf5e4..35755f04 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -30,6 +30,7 @@ var ( BotServerSetting *HttpServerSettingS LocalossServerSetting *HttpServerSettingS DocsServerSetting *HttpServerSettingS + MobileServerSetting *GRPCServerSettingS AppSetting *AppSettingS CacheIndexSetting *CacheIndexSettingS SimpleCacheIndexSetting *SimpleCacheIndexSettingS @@ -72,6 +73,7 @@ func setupSetting(suite []string, noDefault bool) error { "BotServer": &BotServerSetting, "LocalossServer": &LocalossServerSetting, "DocsServer": &DocsServerSetting, + "MobileServer": &MobileServerSetting, "CacheIndex": &CacheIndexSetting, "SimpleCacheIndex": &SimpleCacheIndexSetting, "BigCacheIndex": &BigCacheIndexSetting, diff --git a/internal/conf/config.yaml b/internal/conf/config.yaml index 307e8451..6c642ed8 100644 --- a/internal/conf/config.yaml +++ b/internal/conf/config.yaml @@ -43,6 +43,9 @@ DocsServer: # 开发文档服务 HttpPort: 8011 ReadTimeout: 60 WriteTimeout: 60 +MobileServer: # 移动端grpc api服务 + Host: 0.0.0.0 + Port: 8020 SmsJuhe: Gateway: https://v.juhe.cn/sms/send Key: diff --git a/internal/conf/settting.go b/internal/conf/settting.go index 9fa232db..0c543706 100644 --- a/internal/conf/settting.go +++ b/internal/conf/settting.go @@ -57,6 +57,11 @@ type HttpServerSettingS struct { WriteTimeout time.Duration } +type GRPCServerSettingS struct { + Host string + Port string +} + type AppSettingS struct { RunMode string MaxCommentCount int64 diff --git a/internal/ims/README.md b/internal/ims/README.md new file mode 100644 index 00000000..d095421b --- /dev/null +++ b/internal/ims/README.md @@ -0,0 +1,2 @@ +### IMS(Instant Message Push System) +消息推送模块,用于优化@、私信、系统通知等消息服务,目前还没实现,先占个位置~ diff --git a/internal/proto/gen.go b/internal/ims/ims.go similarity index 91% rename from internal/proto/gen.go rename to internal/ims/ims.go index adb9c8db..1042e45d 100644 --- a/internal/proto/gen.go +++ b/internal/ims/ims.go @@ -2,4 +2,4 @@ // Use of this source code is governed by a MIT style // license that can be found in the LICENSE file. -package proto +package ims diff --git a/internal/obs/README.md b/internal/obs/README.md new file mode 100644 index 00000000..f56a1ef9 --- /dev/null +++ b/internal/obs/README.md @@ -0,0 +1,2 @@ +### OBS(Object Blob Storage System) +简单对象存储模块,目前还没实现,先占个位置~ diff --git a/internal/obs/obs.go b/internal/obs/obs.go new file mode 100644 index 00000000..13923b07 --- /dev/null +++ b/internal/obs/obs.go @@ -0,0 +1,5 @@ +// Copyright 2022 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package obs diff --git a/internal/proto/tweets.proto b/internal/proto/tweets.proto deleted file mode 100644 index 1fcc9364..00000000 --- a/internal/proto/tweets.proto +++ /dev/null @@ -1,10 +0,0 @@ -syntax = "proto3"; - -package paopao; - -option go_package = "proto"; - -message Tag { - string name = 1; - int32 type = 2; -} diff --git a/internal/servants/admin/admin.go b/internal/servants/admin/admin.go index 3fa2a7cf..409bdd78 100644 --- a/internal/servants/admin/admin.go +++ b/internal/servants/admin/admin.go @@ -6,7 +6,7 @@ package admin import ( "github.com/gin-gonic/gin" - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/m/v1" + api "github.com/rocboss/paopao-ce/auto/api/m/v1" ) // RouteWeb register Manager route diff --git a/internal/servants/admin/user.go b/internal/servants/admin/user.go index 6c3f11ab..fef348cf 100644 --- a/internal/servants/admin/user.go +++ b/internal/servants/admin/user.go @@ -5,7 +5,7 @@ package admin import ( - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/m/v1" + api "github.com/rocboss/paopao-ce/auto/api/m/v1" "github.com/rocboss/paopao-ce/internal/servants/base" ) diff --git a/internal/servants/bot/bot.go b/internal/servants/bot/bot.go index 6ddd6846..a2e0e699 100644 --- a/internal/servants/bot/bot.go +++ b/internal/servants/bot/bot.go @@ -6,7 +6,7 @@ package bot import ( "github.com/gin-gonic/gin" - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/r/v1" + api "github.com/rocboss/paopao-ce/auto/api/r/v1" ) // RouteBot register Bot route diff --git a/internal/servants/bot/user.go b/internal/servants/bot/user.go index 9f17a6e8..aae9048c 100644 --- a/internal/servants/bot/user.go +++ b/internal/servants/bot/user.go @@ -5,7 +5,7 @@ package bot import ( - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/r/v1" + api "github.com/rocboss/paopao-ce/auto/api/r/v1" "github.com/rocboss/paopao-ce/internal/servants/base" ) diff --git a/internal/servants/localoss/localoss.go b/internal/servants/localoss/localoss.go index 721f0407..97fdafbe 100644 --- a/internal/servants/localoss/localoss.go +++ b/internal/servants/localoss/localoss.go @@ -8,8 +8,8 @@ import ( "path/filepath" "github.com/gin-gonic/gin" + api "github.com/rocboss/paopao-ce/auto/api/s/v1" "github.com/rocboss/paopao-ce/internal/conf" - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/s/v1" "github.com/sirupsen/logrus" ) diff --git a/internal/servants/localoss/user.go b/internal/servants/localoss/user.go index b8d2309a..37fc715e 100644 --- a/internal/servants/localoss/user.go +++ b/internal/servants/localoss/user.go @@ -5,7 +5,7 @@ package localoss import ( - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/s/v1" + api "github.com/rocboss/paopao-ce/auto/api/s/v1" "github.com/rocboss/paopao-ce/internal/servants/base" ) diff --git a/internal/servants/mobile/auth.go b/internal/servants/mobile/auth.go new file mode 100644 index 00000000..00a9b747 --- /dev/null +++ b/internal/servants/mobile/auth.go @@ -0,0 +1,17 @@ +package mobile + +import ( + api "github.com/rocboss/paopao-ce/auto/rpc/v1" +) + +var ( + _ api.AuthenticateServer = (*authenticateServant)(nil) +) + +type authenticateServant struct { + api.UnimplementedAuthenticateServer +} + +func newAuthenticateServer() *authenticateServant { + return &authenticateServant{} +} diff --git a/internal/servants/mobile/mobile.go b/internal/servants/mobile/mobile.go new file mode 100644 index 00000000..425b71c1 --- /dev/null +++ b/internal/servants/mobile/mobile.go @@ -0,0 +1,10 @@ +package mobile + +import ( + api "github.com/rocboss/paopao-ce/auto/rpc/v1" + "google.golang.org/grpc" +) + +func RegisterServants(s *grpc.Server) { + api.RegisterAuthenticateServer(s, newAuthenticateServer()) +} diff --git a/internal/servants/servants.go b/internal/servants/servants.go index dd03b1de..74d7e8ce 100644 --- a/internal/servants/servants.go +++ b/internal/servants/servants.go @@ -11,9 +11,11 @@ import ( "github.com/rocboss/paopao-ce/internal/servants/bot" "github.com/rocboss/paopao-ce/internal/servants/docs" "github.com/rocboss/paopao-ce/internal/servants/localoss" + "github.com/rocboss/paopao-ce/internal/servants/mobile" "github.com/rocboss/paopao-ce/internal/servants/space" "github.com/rocboss/paopao-ce/internal/servants/statick" "github.com/rocboss/paopao-ce/internal/servants/web" + "google.golang.org/grpc" ) // RegisterWebServants register all the servants to gin.Engine @@ -47,3 +49,8 @@ func RegisterBotServants(e *gin.Engine) { func RegisterLocalossServants(e *gin.Engine) { localoss.RouteLocaloss(e) } + +// RegisterMobileServants register all the servants to grpc.Server +func RegisterMobileServants(s *grpc.Server) { + mobile.RegisterServants(s) +} diff --git a/internal/servants/space/space.go b/internal/servants/space/space.go index 311f76b0..c9952035 100644 --- a/internal/servants/space/space.go +++ b/internal/servants/space/space.go @@ -6,7 +6,7 @@ package space import ( "github.com/gin-gonic/gin" - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/x/v1" + api "github.com/rocboss/paopao-ce/auto/api/x/v1" ) // RouteWeb register SpaceX route diff --git a/internal/servants/space/user.go b/internal/servants/space/user.go index 2e6014a0..d0e5c632 100644 --- a/internal/servants/space/user.go +++ b/internal/servants/space/user.go @@ -5,7 +5,7 @@ package space import ( - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/x/v1" + api "github.com/rocboss/paopao-ce/auto/api/x/v1" "github.com/rocboss/paopao-ce/internal/servants/base" ) diff --git a/internal/servants/web/core.go b/internal/servants/web/core.go index 085e5bcf..75c1409a 100644 --- a/internal/servants/web/core.go +++ b/internal/servants/web/core.go @@ -5,7 +5,7 @@ package web import ( - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/v1" + api "github.com/rocboss/paopao-ce/auto/api/v1" "github.com/rocboss/paopao-ce/internal/servants/base" ) diff --git a/internal/servants/web/web.go b/internal/servants/web/web.go index 76ac9dd3..044440fa 100644 --- a/internal/servants/web/web.go +++ b/internal/servants/web/web.go @@ -6,7 +6,7 @@ package web import ( "github.com/gin-gonic/gin" - api "github.com/rocboss/paopao-ce/internal/mirc/auto/api/v1" + api "github.com/rocboss/paopao-ce/auto/api/v1" ) // RouteWeb register web route diff --git a/internal/service/admin.go b/internal/service/admin.go index 7acd408e..e91b68f5 100644 --- a/internal/service/admin.go +++ b/internal/service/admin.go @@ -32,7 +32,7 @@ func (s *adminService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *adminService) Init() error { +func (s *adminService) OnInit() error { s.registerRoute(servants.RegisterAdminServants) return nil } diff --git a/internal/service/bot.go b/internal/service/bot.go index 38fd97c3..2b9eef20 100644 --- a/internal/service/bot.go +++ b/internal/service/bot.go @@ -32,7 +32,7 @@ func (s *botService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *botService) Init() error { +func (s *botService) OnInit() error { s.registerRoute(servants.RegisterBotServants) return nil } diff --git a/internal/service/grpc_server.go b/internal/service/grpc_server.go new file mode 100644 index 00000000..3f0a3b94 --- /dev/null +++ b/internal/service/grpc_server.go @@ -0,0 +1,59 @@ +// Copyright 2022 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package service + +import ( + "net" + "sync" + + "google.golang.org/grpc" +) + +// grpcServer wraper for grpc.Server +type grpcServer struct { + sync.RWMutex + + listener net.Listener + server *grpc.Server + serverStatus uint8 +} + +func (s *grpcServer) status() uint8 { + s.RLock() + defer s.RUnlock() + + return s.serverStatus +} + +func (s *grpcServer) start() error { + s.Lock() + if s.serverStatus == _statusServerStarted || s.serverStatus == _statusServerStoped { + return nil + } + oldStatus := s.serverStatus + s.serverStatus = _statusServerStarted + s.Unlock() + + if err := s.server.Serve(s.listener); err != nil { + s.Lock() + s.serverStatus = oldStatus + s.Unlock() + + return err + } + return nil +} + +func (s *grpcServer) stop() error { + s.Lock() + defer s.Unlock() + + if s.serverStatus == _statusServerStoped || s.serverStatus == _statusServerInitilized { + return nil + } + s.server.Stop() + s.serverStatus = _statusServerStoped + return nil +} diff --git a/internal/service/grpc_service.go b/internal/service/grpc_service.go new file mode 100644 index 00000000..ded7b507 --- /dev/null +++ b/internal/service/grpc_service.go @@ -0,0 +1,32 @@ +// Copyright 2022 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package service + +import ( + "google.golang.org/grpc" +) + +type baseGRPCService struct { + baseService + + server *grpcServer +} + +func (s *baseGRPCService) registerServer(h func(s *grpc.Server)) { + if s.server.status() != _statusServerStarted { + h(s.server.server) + } +} + +func (s *baseGRPCService) OnStart() error { + if err := s.server.start(); err != nil { + return err + } + return nil +} + +func (s *baseGRPCService) OnStop() error { + return s.server.stop() +} diff --git a/internal/service/http_server.go b/internal/service/http_server.go index 4a8fdf3b..a7ca3879 100644 --- a/internal/service/http_server.go +++ b/internal/service/http_server.go @@ -12,16 +12,6 @@ import ( "github.com/gin-gonic/gin" ) -var ( - httpServers = make(map[string]*httpServer) -) - -const ( - httpServerInitilized uint8 = iota + 1 - httpServerStarted - httpServerStoped -) - // httpServer wraper for gin.engine and http.Server type httpServer struct { sync.RWMutex @@ -40,11 +30,11 @@ func (s *httpServer) status() uint8 { func (s *httpServer) start() error { s.Lock() - if s.serverStatus == httpServerStarted || s.serverStatus == httpServerStoped { + if s.serverStatus == _statusServerStarted || s.serverStatus == _statusServerStoped { return nil } oldStatus := s.serverStatus - s.serverStatus = httpServerStarted + s.serverStatus = _statusServerStarted s.Unlock() if err := s.server.ListenAndServe(); err != nil { @@ -61,23 +51,12 @@ func (s *httpServer) stop() error { s.Lock() defer s.Unlock() - if s.serverStatus == httpServerStoped || s.serverStatus == httpServerInitilized { + if s.serverStatus == _statusServerStoped || s.serverStatus == _statusServerInitilized { return nil } if err := s.server.Shutdown(context.Background()); err != nil { return err } - s.serverStatus = httpServerStoped + s.serverStatus = _statusServerStoped return nil } - -func httpServerFrom(addr string, newServer func() *httpServer) *httpServer { - s, exist := httpServers[addr] - if exist { - return s - } - s = newServer() - s.serverStatus = httpServerInitilized - httpServers[addr] = s - return s -} diff --git a/internal/service/http_service.go b/internal/service/http_service.go index fc7ce9b6..42f3f90c 100644 --- a/internal/service/http_service.go +++ b/internal/service/http_service.go @@ -15,18 +15,18 @@ type baseHttpService struct { } func (s *baseHttpService) registerRoute(h func(e *gin.Engine)) { - if s.server.status() != httpServerStarted { + if s.server.status() != _statusServerStarted { h(s.server.e) } } -func (s *baseHttpService) Start() error { +func (s *baseHttpService) OnStart() error { if err := s.server.start(); err != nil { return err } return nil } -func (s *baseHttpService) Stop() error { +func (s *baseHttpService) OnStop() error { return s.server.stop() } diff --git a/internal/service/localoss.go b/internal/service/localoss.go index 4c09412d..3f33e7ef 100644 --- a/internal/service/localoss.go +++ b/internal/service/localoss.go @@ -31,7 +31,7 @@ func (s *localossService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *localossService) Init() error { +func (s *localossService) OnInit() error { s.registerRoute(servants.RegisterLocalossServants) return nil } diff --git a/internal/service/mobile.go b/internal/service/mobile.go new file mode 100644 index 00000000..262d35af --- /dev/null +++ b/internal/service/mobile.go @@ -0,0 +1,66 @@ +// Copyright 2022 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package service + +import ( + "fmt" + "log" + "net" + + "github.com/Masterminds/semver/v3" + "github.com/fatih/color" + "github.com/rocboss/paopao-ce/internal/conf" + "github.com/rocboss/paopao-ce/internal/servants" + "google.golang.org/grpc" +) + +var ( + _ Service = (*mobileService)(nil) +) + +type mobileService struct { + *baseGRPCService +} + +func (s *mobileService) Name() string { + return "MobileService" +} + +func (s *mobileService) Version() *semver.Version { + return semver.MustParse("v0.1.0") +} + +func (s *mobileService) OnInit() error { + s.registerServer(servants.RegisterMobileServants) + return nil +} + +func (s *mobileService) String() string { + return fmt.Sprintf("listen on %s\n", color.GreenString("http://%s:%s", conf.MobileServerSetting.Host, conf.MobileServerSetting.Port)) +} + +func newMobileService() Service { + addr := conf.MobileServerSetting.Host + ":" + conf.MobileServerSetting.Port + server := grpcServerFrom(addr, func() *grpcServer { + l, err := net.Listen("tcp", addr) + if err != nil { + // TODO: optimize error process + log.Fatal(fmt.Errorf("failed to listen: %w", err)) + } + + // TODO: init grpc.Server + s := grpc.NewServer() + + return &grpcServer{ + listener: l, + server: s, + } + }) + return &mobileService{ + baseGRPCService: &baseGRPCService{ + server: server, + }, + } +} diff --git a/internal/service/server.go b/internal/service/server.go new file mode 100644 index 00000000..f2439777 --- /dev/null +++ b/internal/service/server.go @@ -0,0 +1,38 @@ +// Copyright 2022 ROC. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package service + +var ( + httpServers = make(map[string]*httpServer) + grpcServers = make(map[string]*grpcServer) +) + +const ( + _statusServerInitilized uint8 = iota + 1 + _statusServerStarted + _statusServerStoped +) + +func httpServerFrom(addr string, newServer func() *httpServer) *httpServer { + s, exist := httpServers[addr] + if exist { + return s + } + s = newServer() + s.serverStatus = _statusServerInitilized + httpServers[addr] = s + return s +} + +func grpcServerFrom(addr string, newServer func() *grpcServer) *grpcServer { + s, exist := grpcServers[addr] + if exist { + return s + } + s = newServer() + s.serverStatus = _statusServerInitilized + grpcServers[addr] = s + return s +} diff --git a/internal/service/service.go b/internal/service/service.go index c1f8aa94..957979c3 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -15,9 +15,9 @@ import ( type Service interface { Name() string Version() *semver.Version - Init() error - Start() error - Stop() error + OnInit() error + OnStart() error + OnStop() error } type baseService types.Empty @@ -38,7 +38,7 @@ func (baseService) String() string { func InitService() []Service { ss := newService() for _, s := range ss { - if err := s.Init(); err != nil { + if err := s.OnInit(); err != nil { log.Fatalf("initial %s service error: %s", s.Name(), err) } } @@ -79,6 +79,9 @@ func newService() (ss []Service) { "NativeOBS": func() { ss = append(ss, newLocalossService()) }, + "Mobile": func() { + ss = append(ss, newMobileService()) + }, }) return } diff --git a/internal/service/space.go b/internal/service/space.go index 30dfeec0..fd6ee217 100644 --- a/internal/service/space.go +++ b/internal/service/space.go @@ -32,7 +32,7 @@ func (s *spaceXService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *spaceXService) Init() error { +func (s *spaceXService) OnInit() error { s.registerRoute(servants.RegisterSpaceXServants) return nil } diff --git a/internal/service/web.go b/internal/service/web.go index e983e6c5..56b8ef3b 100644 --- a/internal/service/web.go +++ b/internal/service/web.go @@ -32,7 +32,7 @@ func (s *webService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *webService) Init() error { +func (s *webService) OnInit() error { s.registerRoute(servants.RegisterWebServants) return nil } diff --git a/internal/service/web_old.go b/internal/service/web_old.go index de51c072..70e7ec37 100644 --- a/internal/service/web_old.go +++ b/internal/service/web_old.go @@ -30,7 +30,7 @@ func (s *oldWebService) Version() *semver.Version { return semver.MustParse("v0.1.0") } -func (s *oldWebService) Init() error { +func (s *oldWebService) OnInit() error { s.registerRoute(routers.RegisterRoute) return nil } diff --git a/main.go b/main.go index aafc14e3..5e0fcc01 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,7 @@ func runService(wg *sync.WaitGroup, ss []service.Service) { for _, s := range ss { go func(s service.Service) { fmt.Fprintf(color.Output, "%s [start] - %s", util.SidStr(s.Name(), s.Version(), l), s) - if err := s.Start(); err != nil { + if err := s.OnStart(); err != nil { fmt.Fprintf(color.Output, "%s [start] - occurs on error: %s\n", util.SidStr(s.Name(), s.Version(), l), err) } wg.Done() @@ -79,10 +79,10 @@ func runManage(wg *sync.WaitGroup, ss []service.Service) { fmt.Fprintf(color.Output, "\nshutting down server...\n\n") l := service.MaxSidSize(ss) for _, s := range ss { - if err := s.Stop(); err != nil { - fmt.Fprintf(color.Output, "%s [stop] - occurs on error: %s\n", util.SidStr(s.Name(), s.Version(), l), err) + if err := s.OnStop(); err != nil { + fmt.Fprintf(color.Output, "%s [stop] - occurs on error: %s\n", util.SidStr(s.Name(), s.Version(), l), err) } - fmt.Fprintf(color.Output, "%s [stop] - finish...\n", util.SidStr(s.Name(), s.Version(), l)) + fmt.Fprintf(color.Output, "%s [stop] - finish...\n", util.SidStr(s.Name(), s.Version(), l)) } wg.Done() } diff --git a/internal/mirc/README.md b/mirc/README.md similarity index 100% rename from internal/mirc/README.md rename to mirc/README.md diff --git a/internal/mirc/main.go b/mirc/main.go similarity index 61% rename from internal/mirc/main.go rename to mirc/main.go index 05431a6c..a2c17cdd 100644 --- a/internal/mirc/main.go +++ b/mirc/main.go @@ -10,11 +10,11 @@ import ( . "github.com/alimy/mir/v3/core" . "github.com/alimy/mir/v3/engine" - _ "github.com/rocboss/paopao-ce/internal/mirc/routes/admin/v1" - _ "github.com/rocboss/paopao-ce/internal/mirc/routes/bot/v1" - _ "github.com/rocboss/paopao-ce/internal/mirc/routes/localoss/v1" - _ "github.com/rocboss/paopao-ce/internal/mirc/routes/space/v1" - _ "github.com/rocboss/paopao-ce/internal/mirc/routes/v1" + _ "github.com/rocboss/paopao-ce/mirc/routes/admin/v1" + _ "github.com/rocboss/paopao-ce/mirc/routes/bot/v1" + _ "github.com/rocboss/paopao-ce/mirc/routes/localoss/v1" + _ "github.com/rocboss/paopao-ce/mirc/routes/space/v1" + _ "github.com/rocboss/paopao-ce/mirc/routes/v1" ) //go:generate go run main.go @@ -24,7 +24,7 @@ func main() { RunMode(InSerialMode), GeneratorName(GeneratorGin), WatchCtxDone(true), - SinkPath("auto"), + SinkPath("../auto"), } if err := Generate(opts); err != nil { log.Fatal(err) diff --git a/internal/mirc/routes/README.md b/mirc/routes/README.md similarity index 100% rename from internal/mirc/routes/README.md rename to mirc/routes/README.md diff --git a/internal/mirc/routes/admin/README.md b/mirc/routes/admin/README.md similarity index 100% rename from internal/mirc/routes/admin/README.md rename to mirc/routes/admin/README.md diff --git a/internal/mirc/routes/admin/v1/user.go b/mirc/routes/admin/v1/user.go similarity index 100% rename from internal/mirc/routes/admin/v1/user.go rename to mirc/routes/admin/v1/user.go diff --git a/internal/mirc/routes/bot/README.md b/mirc/routes/bot/README.md similarity index 100% rename from internal/mirc/routes/bot/README.md rename to mirc/routes/bot/README.md diff --git a/internal/mirc/routes/bot/v1/user.go b/mirc/routes/bot/v1/user.go similarity index 100% rename from internal/mirc/routes/bot/v1/user.go rename to mirc/routes/bot/v1/user.go diff --git a/internal/mirc/routes/localoss/README.md b/mirc/routes/localoss/README.md similarity index 100% rename from internal/mirc/routes/localoss/README.md rename to mirc/routes/localoss/README.md diff --git a/internal/mirc/routes/localoss/v1/user.go b/mirc/routes/localoss/v1/user.go similarity index 100% rename from internal/mirc/routes/localoss/v1/user.go rename to mirc/routes/localoss/v1/user.go diff --git a/internal/mirc/routes/space/README.md b/mirc/routes/space/README.md similarity index 100% rename from internal/mirc/routes/space/README.md rename to mirc/routes/space/README.md diff --git a/internal/mirc/routes/space/v1/user.go b/mirc/routes/space/v1/user.go similarity index 100% rename from internal/mirc/routes/space/v1/user.go rename to mirc/routes/space/v1/user.go diff --git a/internal/mirc/routes/v1/localoss.go b/mirc/routes/v1/localoss.go similarity index 100% rename from internal/mirc/routes/v1/localoss.go rename to mirc/routes/v1/localoss.go diff --git a/internal/mirc/routes/v1/web_admin.go b/mirc/routes/v1/web_admin.go similarity index 100% rename from internal/mirc/routes/v1/web_admin.go rename to mirc/routes/v1/web_admin.go diff --git a/internal/mirc/routes/v1/web_alipay.go b/mirc/routes/v1/web_alipay.go similarity index 100% rename from internal/mirc/routes/v1/web_alipay.go rename to mirc/routes/v1/web_alipay.go diff --git a/internal/mirc/routes/v1/web_core.go b/mirc/routes/v1/web_core.go similarity index 100% rename from internal/mirc/routes/v1/web_core.go rename to mirc/routes/v1/web_core.go diff --git a/internal/mirc/routes/v1/web_followship.go b/mirc/routes/v1/web_followship.go similarity index 100% rename from internal/mirc/routes/v1/web_followship.go rename to mirc/routes/v1/web_followship.go diff --git a/internal/mirc/routes/v1/web_friendship.go b/mirc/routes/v1/web_friendship.go similarity index 100% rename from internal/mirc/routes/v1/web_friendship.go rename to mirc/routes/v1/web_friendship.go diff --git a/internal/mirc/routes/v1/web_loose.go b/mirc/routes/v1/web_loose.go similarity index 100% rename from internal/mirc/routes/v1/web_loose.go rename to mirc/routes/v1/web_loose.go diff --git a/internal/mirc/routes/v1/web_priv.go b/mirc/routes/v1/web_priv.go similarity index 100% rename from internal/mirc/routes/v1/web_priv.go rename to mirc/routes/v1/web_priv.go diff --git a/internal/mirc/routes/v1/web_pub.go b/mirc/routes/v1/web_pub.go similarity index 100% rename from internal/mirc/routes/v1/web_pub.go rename to mirc/routes/v1/web_pub.go diff --git a/pkg/util/banner.go b/pkg/util/banner.go index 5e9795c2..b8f16971 100644 --- a/pkg/util/banner.go +++ b/pkg/util/banner.go @@ -20,5 +20,5 @@ func PrintHelloBanner(text string) { } func SidStr(name string, version *semver.Version, size int) string { - return fmt.Sprintf(fmt.Sprintf("%%s%%-%ds", size-len(name+version.String())+4), name, version) + return fmt.Sprintf(fmt.Sprintf("%%s@%%-%ds", size-len(name+version.String())+4), name, version) } diff --git a/internal/proto/RADME.md b/proto/RADME.md similarity index 100% rename from internal/proto/RADME.md rename to proto/RADME.md diff --git a/proto/buf.lock b/proto/buf.lock new file mode 100644 index 00000000..19abf63f --- /dev/null +++ b/proto/buf.lock @@ -0,0 +1,7 @@ +# Generated by buf. DO NOT EDIT. +version: v1 +deps: + - remote: buf.build + owner: googleapis + repository: googleapis + commit: 75b4300737fb4efca0831636be94e517 diff --git a/proto/buf.yaml b/proto/buf.yaml new file mode 100644 index 00000000..eda8f330 --- /dev/null +++ b/proto/buf.yaml @@ -0,0 +1,10 @@ +version: v1 +lint: + use: + - DEFAULT +breaking: + use: + - FILE + +deps: + - buf.build/googleapis/googleapis diff --git a/proto/v1/auth.proto b/proto/v1/auth.proto new file mode 100644 index 00000000..bc18fae3 --- /dev/null +++ b/proto/v1/auth.proto @@ -0,0 +1,29 @@ +syntax = "proto3"; + +package auth; + +option go_package = "github.com/rocboss/paopao-ce/auto/rpc"; + +message User { + string phoneNum = 1; +} + +message UserVerify { + string phoneNum = 1; + string VerificationCode = 2; +} + +message LoginReply { + int32 statuCode = 1; + string token = 2; +} + +message ActionReply { + int32 statusCode = 1; +} + +service Authenticate { + rpc preLogin(User) returns (ActionReply); + rpc login(User) returns (LoginReply); + rpc logout(User) returns (ActionReply); +}