diff --git a/api/user.http.go b/api/user.http.go index 8b48490..8d22c4a 100644 --- a/api/user.http.go +++ b/api/user.http.go @@ -1,17 +1,17 @@ package user import ( - "github.com/gin-gonic/gin" "context" "git.diulo.com/mogfee/kit/middleware" "git.diulo.com/mogfee/kit/errors" "git.diulo.com/mogfee/kit/response" + "github.com/gin-gonic/gin" ) func RegisterUserHandler(app *gin.Engine, srv UserServer, m ...middleware.Middleware) { app.GET("/api/v1/user/list", httpListHandler(srv, m...)) - app.GET("/api/v1/user/auto", httpAutoHandler(srv, m...)) app.GET("/api/v1/user/all", httpAllHandler(srv, m...)) + app.GET("/api/v1/user/auto", httpAutoHandler(srv, m...)) app.GET("/api/v1/user/login_list", httpLoginWithListHandler(srv, m...)) app.GET("/api/v1/user/login", httpLoginHandler(srv, m...)) } @@ -38,7 +38,7 @@ func httpListHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Con } } } -func httpAutoHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { +func httpAllHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { return func(c *gin.Context) { var post Request resp := response.New(c) @@ -47,7 +47,7 @@ func httpAutoHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Con return } h := func(ctx context.Context, a any) (any, error) { - return srv.Auto(ctx, a.(*Request)) + return srv.All(ctx, a.(*Request)) } out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) if err != nil { @@ -61,7 +61,7 @@ func httpAutoHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Con } } } -func httpAllHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { +func httpAutoHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Context) { return func(c *gin.Context) { var post Request resp := response.New(c) @@ -70,7 +70,7 @@ func httpAllHandler(srv UserServer, m ...middleware.Middleware) func(c *gin.Cont return } h := func(ctx context.Context, a any) (any, error) { - return srv.All(ctx, a.(*Request)) + return srv.Auto(ctx, a.(*Request)) } out, err := middleware.HttpMiddleware(c, h, m...)(c, &post) if err != nil { diff --git a/api/user.pb.go b/api/user.pb.go index 1507694..0c566dc 100644 --- a/api/user.pb.go +++ b/api/user.pb.go @@ -125,17 +125,17 @@ var file_user_proto_rawDesc = []byte{ 0x2e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x25, 0xba, 0x45, 0x09, 0x75, 0x73, 0x65, 0x72, 0x3a, 0x6c, 0x69, 0x73, 0x74, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x6c, 0x69, 0x73, 0x74, - 0x12, 0x55, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x6f, 0x12, 0x16, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, - 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, - 0x2e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1c, 0xc0, 0x45, 0x01, 0x82, 0xd3, - 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, - 0x65, 0x72, 0x2f, 0x61, 0x75, 0x74, 0x6f, 0x12, 0x53, 0x0a, 0x03, 0x61, 0x6c, 0x6c, 0x12, 0x16, - 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, - 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, - 0x1b, 0xc0, 0x45, 0x00, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x12, 0x12, 0x10, 0x2f, 0x61, 0x70, 0x69, - 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x61, 0x6c, 0x6c, 0x12, 0x61, 0x0a, 0x0d, + 0x12, 0x53, 0x0a, 0x03, 0x61, 0x6c, 0x6c, 0x12, 0x16, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, + 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, + 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1b, 0xc0, 0x45, 0x01, 0x82, 0xd3, 0xe4, + 0x93, 0x02, 0x12, 0x12, 0x10, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, + 0x72, 0x2f, 0x61, 0x6c, 0x6c, 0x12, 0x55, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x6f, 0x12, 0x16, 0x2e, + 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, + 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x1c, + 0xc0, 0x45, 0x00, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, + 0x76, 0x31, 0x2f, 0x75, 0x73, 0x65, 0x72, 0x2f, 0x61, 0x75, 0x74, 0x6f, 0x12, 0x61, 0x0a, 0x0d, 0x6c, 0x6f, 0x67, 0x69, 0x6e, 0x57, 0x69, 0x74, 0x68, 0x4c, 0x69, 0x73, 0x74, 0x12, 0x16, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x72, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x6f, 0x6d, 0x2e, 0x64, 0x69, 0x75, 0x6c, @@ -170,13 +170,13 @@ var file_user_proto_goTypes = []interface{}{ } var file_user_proto_depIdxs = []int32{ 0, // 0: com.diulo.api.user.list:input_type -> com.diulo.api.request - 0, // 1: com.diulo.api.user.auto:input_type -> com.diulo.api.request - 0, // 2: com.diulo.api.user.all:input_type -> com.diulo.api.request + 0, // 1: com.diulo.api.user.all:input_type -> com.diulo.api.request + 0, // 2: com.diulo.api.user.auto:input_type -> com.diulo.api.request 0, // 3: com.diulo.api.user.loginWithList:input_type -> com.diulo.api.request 0, // 4: com.diulo.api.user.login:input_type -> com.diulo.api.request 1, // 5: com.diulo.api.user.list:output_type -> com.diulo.api.response - 1, // 6: com.diulo.api.user.auto:output_type -> com.diulo.api.response - 1, // 7: com.diulo.api.user.all:output_type -> com.diulo.api.response + 1, // 6: com.diulo.api.user.all:output_type -> com.diulo.api.response + 1, // 7: com.diulo.api.user.auto:output_type -> com.diulo.api.response 1, // 8: com.diulo.api.user.loginWithList:output_type -> com.diulo.api.response 1, // 9: com.diulo.api.user.login:output_type -> com.diulo.api.response 5, // [5:10] is the sub-list for method output_type diff --git a/api/user.ts b/api/user.ts index 072ea67..f63e1ee 100644 --- a/api/user.ts +++ b/api/user.ts @@ -16,16 +16,15 @@ export class userService{ method:'GET' }) } - //等了 - static async auto(data :request, param?: Config):Promise{ - return http('/api/v1/user/auto', { + static async all(data :request, param?: Config):Promise{ + return http('/api/v1/user/all', { ...param, data: data, method:'GET' }) } - static async all(data :request, param?: Config):Promise{ - return http('/api/v1/user/all', { + static async auto(data :request, param?: Config):Promise{ + return http('/api/v1/user/auto', { ...param, data: data, method:'GET' diff --git a/api/user_grpc.pb.go b/api/user_grpc.pb.go index 7279ac8..5b6c20a 100644 --- a/api/user_grpc.pb.go +++ b/api/user_grpc.pb.go @@ -20,8 +20,8 @@ const _ = grpc.SupportPackageIsVersion7 const ( User_List_FullMethodName = "/com.diulo.api.user/list" - User_Auto_FullMethodName = "/com.diulo.api.user/auto" User_All_FullMethodName = "/com.diulo.api.user/all" + User_Auto_FullMethodName = "/com.diulo.api.user/auto" User_LoginWithList_FullMethodName = "/com.diulo.api.user/loginWithList" User_Login_FullMethodName = "/com.diulo.api.user/login" ) @@ -31,9 +31,8 @@ const ( // 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 UserClient interface { List(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) - // 等了 - Auto(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) All(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) + Auto(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) // 有 "user:list" LoginWithList(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) // 没有 "user:list" 权限 @@ -57,18 +56,18 @@ func (c *userClient) List(ctx context.Context, in *Request, opts ...grpc.CallOpt return out, nil } -func (c *userClient) Auto(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { +func (c *userClient) All(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { out := new(Response) - err := c.cc.Invoke(ctx, User_Auto_FullMethodName, in, out, opts...) + err := c.cc.Invoke(ctx, User_All_FullMethodName, in, out, opts...) if err != nil { return nil, err } return out, nil } -func (c *userClient) All(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { +func (c *userClient) Auto(ctx context.Context, in *Request, opts ...grpc.CallOption) (*Response, error) { out := new(Response) - err := c.cc.Invoke(ctx, User_All_FullMethodName, in, out, opts...) + err := c.cc.Invoke(ctx, User_Auto_FullMethodName, in, out, opts...) if err != nil { return nil, err } @@ -98,9 +97,8 @@ func (c *userClient) Login(ctx context.Context, in *Request, opts ...grpc.CallOp // for forward compatibility type UserServer interface { List(context.Context, *Request) (*Response, error) - // 等了 - Auto(context.Context, *Request) (*Response, error) All(context.Context, *Request) (*Response, error) + Auto(context.Context, *Request) (*Response, error) // 有 "user:list" LoginWithList(context.Context, *Request) (*Response, error) // 没有 "user:list" 权限 @@ -115,12 +113,12 @@ type UnimplementedUserServer struct { func (UnimplementedUserServer) List(context.Context, *Request) (*Response, error) { return nil, status.Errorf(codes.Unimplemented, "method List not implemented") } -func (UnimplementedUserServer) Auto(context.Context, *Request) (*Response, error) { - return nil, status.Errorf(codes.Unimplemented, "method Auto not implemented") -} func (UnimplementedUserServer) All(context.Context, *Request) (*Response, error) { return nil, status.Errorf(codes.Unimplemented, "method All not implemented") } +func (UnimplementedUserServer) Auto(context.Context, *Request) (*Response, error) { + return nil, status.Errorf(codes.Unimplemented, "method Auto not implemented") +} func (UnimplementedUserServer) LoginWithList(context.Context, *Request) (*Response, error) { return nil, status.Errorf(codes.Unimplemented, "method LoginWithList not implemented") } @@ -158,38 +156,38 @@ func _User_List_Handler(srv interface{}, ctx context.Context, dec func(interface return interceptor(ctx, in, info, handler) } -func _User_Auto_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { +func _User_All_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(Request) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(UserServer).Auto(ctx, in) + return srv.(UserServer).All(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: User_Auto_FullMethodName, + FullMethod: User_All_FullMethodName, } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(UserServer).Auto(ctx, req.(*Request)) + return srv.(UserServer).All(ctx, req.(*Request)) } return interceptor(ctx, in, info, handler) } -func _User_All_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { +func _User_Auto_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(Request) if err := dec(in); err != nil { return nil, err } if interceptor == nil { - return srv.(UserServer).All(ctx, in) + return srv.(UserServer).Auto(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, - FullMethod: User_All_FullMethodName, + FullMethod: User_Auto_FullMethodName, } handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(UserServer).All(ctx, req.(*Request)) + return srv.(UserServer).Auto(ctx, req.(*Request)) } return interceptor(ctx, in, info, handler) } @@ -241,14 +239,14 @@ var User_ServiceDesc = grpc.ServiceDesc{ MethodName: "list", Handler: _User_List_Handler, }, - { - MethodName: "auto", - Handler: _User_Auto_Handler, - }, { MethodName: "all", Handler: _User_All_Handler, }, + { + MethodName: "auto", + Handler: _User_Auto_Handler, + }, { MethodName: "loginWithList", Handler: _User_LoginWithList_Handler, diff --git a/api/user_http.pb.go b/api/user_http.pb.go index 8ffd733..573a2f7 100644 --- a/api/user_http.pb.go +++ b/api/user_http.pb.go @@ -2,14 +2,14 @@ package user import ( "context" - "git.diulo.com/mogfee/kit/middleware/jwt" "git.diulo.com/mogfee/kit/transport/http" + "git.diulo.com/mogfee/kit/middleware/jwt" ) type UserHTTPServer interface { List(context.Context, *Request) (*Response, error) - Auto(context.Context, *Request) (*Response, error) All(context.Context, *Request) (*Response, error) + Auto(context.Context, *Request) (*Response, error) LoginWithList(context.Context, *Request) (*Response, error) Login(context.Context, *Request) (*Response, error) } @@ -17,8 +17,8 @@ type UserHTTPServer interface { func RegisterUserHTTPServer(s *http.Server, srv UserServer) { r := s.Route("/") r.GET("/api/v1/user/list", _User_List0_HTTP_Handler(srv)) - r.GET("/api/v1/user/auto", _User_Auto0_HTTP_Handler(srv)) r.GET("/api/v1/user/all", _User_All0_HTTP_Handler(srv)) + r.GET("/api/v1/user/auto", _User_Auto0_HTTP_Handler(srv)) r.GET("/api/v1/user/login_list", _User_LoginWithList0_HTTP_Handler(srv)) r.GET("/api/v1/user/login", _User_Login0_HTTP_Handler(srv)) } @@ -44,7 +44,7 @@ func _User_List0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return ctx.Result(200, reply) } } -func _User_Auto0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { +func _User_All0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return func(ctx http.Context) error { var in Request var newCtx context.Context = ctx @@ -52,9 +52,9 @@ func _User_Auto0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { if err := ctx.BindQuery(&in); err != nil { return err } - http.SetOperation(ctx, "/com.diulo.api.user/auto") + http.SetOperation(ctx, "/com.diulo.api.user/all") h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.Auto(ctx, req.(*Request)) + return srv.All(ctx, req.(*Request)) }) out, err := h(newCtx, &in) if err != nil { @@ -64,16 +64,16 @@ func _User_Auto0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return ctx.Result(200, reply) } } -func _User_All0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { +func _User_Auto0_HTTP_Handler(srv UserHTTPServer) func(ctx http.Context) error { return func(ctx http.Context) error { var in Request var newCtx context.Context = ctx if err := ctx.BindQuery(&in); err != nil { return err } - http.SetOperation(ctx, "/com.diulo.api.user/all") + http.SetOperation(ctx, "/com.diulo.api.user/auto") h := ctx.Middleware(func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.All(ctx, req.(*Request)) + return srv.Auto(ctx, req.(*Request)) }) out, err := h(newCtx, &in) if err != nil { diff --git a/example/service/service.go b/example/service/service.go index 8d35a30..e4e88c0 100644 --- a/example/service/service.go +++ b/example/service/service.go @@ -12,27 +12,36 @@ type userServer struct { } func NewUserService(tokenKey string) user.UserServer { + return &userServer{ tokenKey: tokenKey, } } func (userServer) List(ctx context.Context, request *user.Request) (*user.Response, error) { + userInfo, _ := jwt.FromUserContext(ctx) return &user.Response{ - Token: "ok", + Token: userInfo.UserName, }, nil } -func (userServer) Auto(ctx context.Context, request *user.Request) (*user.Response, error) { +func (userServer) All(ctx context.Context, request *user.Request) (*user.Response, error) { + userInfo, _ := jwt.FromUserContext(ctx) return &user.Response{ - Token: "ok", + Token: userInfo.UserName, }, nil - } -func (userServer) All(ctx context.Context, request *user.Request) (*user.Response, error) { - return &user.Response{ - Token: "ok", - }, nil +func (userServer) Auto(ctx context.Context, request *user.Request) (*user.Response, error) { + userInfo, ok := jwt.FromUserContext(ctx) + if ok { + return &user.Response{ + Token: userInfo.UserName, + }, nil + } else { + return &user.Response{ + Token: "no login", + }, nil + } } func (s *userServer) LoginWithList(ctx context.Context, request *user.Request) (*user.Response, error) { diff --git a/middleware/jwt/jwt.go b/middleware/jwt/jwt.go index 4a4707e..2cc1fc3 100644 --- a/middleware/jwt/jwt.go +++ b/middleware/jwt/jwt.go @@ -77,8 +77,9 @@ func JWT(opts ...JwtOption) middleware.Middleware { return nil, err } } - - ctx = SetUserContext(ctx, userInfo) + if userInfo.UserId > 0 { + ctx = SetUserContext(ctx, userInfo) + } return handler(ctx, a) } } diff --git a/proto/user.proto b/proto/user.proto index dcf172b..0d9161e 100644 --- a/proto/user.proto +++ b/proto/user.proto @@ -17,20 +17,20 @@ service user{ get:"/api/v1/user/list" }; } - //等了 - rpc auto(request)returns(response){ + rpc all(request)returns(response){ option(auth.auth) = true; option (google.api.http) = { - get: "/api/v1/user/auto", + get: "/api/v1/user/all", }; } - rpc all(request)returns(response){ + rpc auto(request)returns(response){ option(auth.auth) = false; option (google.api.http) = { - get: "/api/v1/user/all", + get: "/api/v1/user/auto", }; } + //有 "user:list" rpc loginWithList(request)returns(response){ option (google.api.http) = {