add grpc service template code and optimize service initial logic

pull/196/head
Michael Li 2 years ago
parent da633590a1
commit f287c825a6
No known key found for this signature in database

@ -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"

@ -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
}

@ -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",
}

@ -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

@ -0,0 +1,3 @@
version: v1
directories:
- proto

@ -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:

@ -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

@ -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=

@ -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,

@ -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:

@ -57,6 +57,11 @@ type HttpServerSettingS struct {
WriteTimeout time.Duration
}
type GRPCServerSettingS struct {
Host string
Port string
}
type AppSettingS struct {
RunMode string
MaxCommentCount int64

@ -0,0 +1,2 @@
### IMS(Instant Message Push System
消息推送模块,用于优化@、私信、系统通知等消息服务,目前还没实现,先占个位置~

@ -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

@ -0,0 +1,2 @@
### OBS(Object Blob Storage System)
简单对象存储模块,目前还没实现,先占个位置~

@ -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

@ -1,10 +0,0 @@
syntax = "proto3";
package paopao;
option go_package = "proto";
message Tag {
string name = 1;
int32 type = 2;
}

@ -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

@ -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"
)

@ -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

@ -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"
)

@ -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"
)

@ -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"
)

@ -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{}
}

@ -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())
}

@ -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)
}

@ -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

@ -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"
)

@ -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"
)

@ -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

@ -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
}

@ -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
}

@ -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
}

@ -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()
}

@ -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
}

@ -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()
}

@ -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
}

@ -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,
},
}
}

@ -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
}

@ -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
}

@ -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
}

@ -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
}

@ -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
}

@ -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()
}

@ -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)

@ -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)
}

@ -0,0 +1,7 @@
# Generated by buf. DO NOT EDIT.
version: v1
deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: 75b4300737fb4efca0831636be94e517

@ -0,0 +1,10 @@
version: v1
lint:
use:
- DEFAULT
breaking:
use:
- FILE
deps:
- buf.build/googleapis/googleapis

@ -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);
}
Loading…
Cancel
Save