✨ More and more api was included
This commit is contained in:
		| @@ -2,64 +2,37 @@ package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/hyper" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/proto" | ||||
| 	"google.golang.org/grpc" | ||||
| ) | ||||
|  | ||||
| func (v *Server) Authenticate(ctx context.Context, request *proto.AuthRequest) (*proto.AuthReply, error) { | ||||
| 	instance := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider) | ||||
| 	if instance == nil { | ||||
| 		return &proto.AuthReply{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider) | ||||
| 	} | ||||
|  | ||||
| 	conn, err := instance.GetGrpcConn() | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service is down: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	out, err := proto.NewAuthClient(conn).Authenticate(ctx, request) | ||||
| 	return out, err | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.AuthReply, error) { | ||||
| 			out, err := proto.NewAuthClient(conn).Authenticate(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) EnsurePermGranted(ctx context.Context, request *proto.CheckPermRequest) (*proto.CheckPermResponse, error) { | ||||
| 	instance := directory.GetServiceInstanceByType(hyper.ServiceTypeAuthProvider) | ||||
| 	if instance == nil { | ||||
| 		return &proto.CheckPermResponse{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider) | ||||
| 	} | ||||
|  | ||||
| 	conn, err := instance.GetGrpcConn() | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service is down: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	out, err := proto.NewAuthClient(conn).EnsurePermGranted(ctx, request) | ||||
| 	return out, err | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.CheckPermResponse, error) { | ||||
| 			out, err := proto.NewAuthClient(conn).EnsurePermGranted(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) EnsureUserPermGranted(ctx context.Context, request *proto.CheckUserPermRequest) (*proto.CheckUserPermResponse, error) { | ||||
| 	instance := directory.GetServiceInstance(hyper.ServiceTypeAuthProvider) | ||||
| 	if instance == nil { | ||||
| 		return &proto.CheckUserPermResponse{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider) | ||||
| 	} | ||||
|  | ||||
| 	conn, err := instance.GetGrpcConn() | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service is down: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	out, err := proto.NewAuthClient(conn).EnsureUserPermGranted(ctx, request) | ||||
| 	return out, err | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.CheckUserPermResponse, error) { | ||||
| 			out, err := proto.NewAuthClient(conn).EnsureUserPermGranted(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|   | ||||
							
								
								
									
										28
									
								
								pkg/internal/grpc/forward.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								pkg/internal/grpc/forward.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/hyper" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory" | ||||
| 	"google.golang.org/grpc" | ||||
| 	"time" | ||||
| ) | ||||
|  | ||||
| func forwardInvokeRequest[T any](serviceType string, executor func(context.Context, *grpc.ClientConn) (T, error)) (T, error) { | ||||
| 	var emptyResult T | ||||
| 	instance := directory.GetServiceInstance(serviceType) | ||||
| 	if instance == nil { | ||||
| 		return emptyResult, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider) | ||||
| 	} | ||||
|  | ||||
| 	conn, err := instance.GetGrpcConn() | ||||
| 	if err != nil { | ||||
| 		return emptyResult, fmt.Errorf("service is down: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	return executor(ctx, conn) | ||||
| } | ||||
							
								
								
									
										28
									
								
								pkg/internal/grpc/notifier.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								pkg/internal/grpc/notifier.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/hyper" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/proto" | ||||
| 	"google.golang.org/grpc" | ||||
| ) | ||||
|  | ||||
| func (v *Server) NotifyUser(ctx context.Context, request *proto.NotifyUserRequest) (*proto.NotifyResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.NotifyResponse, error) { | ||||
| 			out, err := proto.NewNotifierClient(conn).NotifyUser(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) NotifyAllUser(ctx context.Context, request *proto.NotifyRequest) (*proto.NotifyResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.NotifyResponse, error) { | ||||
| 			out, err := proto.NewNotifierClient(conn).NotifyAllUser(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
							
								
								
									
										78
									
								
								pkg/internal/grpc/realm.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								pkg/internal/grpc/realm.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,78 @@ | ||||
| package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/hyper" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/proto" | ||||
| 	"google.golang.org/grpc" | ||||
| ) | ||||
|  | ||||
| func (v *Server) ListCommunityRealm(ctx context.Context, request *proto.ListRealmRequest) (*proto.ListRealmResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.ListRealmResponse, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).ListCommunityRealm(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) ListAvailableRealm(ctx context.Context, request *proto.LookupUserRealmRequest) (*proto.ListRealmResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.ListRealmResponse, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).ListAvailableRealm(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) ListOwnedRealm(ctx context.Context, request *proto.LookupUserRealmRequest) (*proto.ListRealmResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.ListRealmResponse, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).ListOwnedRealm(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) GetRealm(ctx context.Context, request *proto.LookupRealmRequest) (*proto.RealmInfo, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.RealmInfo, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).GetRealm(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) ListRealmMember(ctx context.Context, request *proto.RealmMemberLookupRequest) (*proto.ListRealmMemberResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.ListRealmMemberResponse, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).ListRealmMember(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) GetRealmMember(ctx context.Context, request *proto.RealmMemberLookupRequest) (*proto.MemberInfo, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.MemberInfo, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).GetRealmMember(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|  | ||||
| func (v *Server) CheckRealmMemberPerm(ctx context.Context, request *proto.CheckRealmPermRequest) (*proto.CheckRealmPermResponse, error) { | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.CheckRealmPermResponse, error) { | ||||
| 			out, err := proto.NewRealmClient(conn).CheckRealmMemberPerm(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
| @@ -2,28 +2,17 @@ package grpc | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/hyper" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/internal/directory" | ||||
| 	"git.solsynth.dev/hydrogen/dealer/pkg/proto" | ||||
| 	"google.golang.org/grpc" | ||||
| ) | ||||
|  | ||||
| func (v *Server) RecordEvent(ctx context.Context, request *proto.RecordEventRequest) (*proto.RecordEventResponse, error) { | ||||
| 	instance := directory.GetServiceInstance(hyper.ServiceTypeAuthProvider) | ||||
| 	if instance == nil { | ||||
| 		return &proto.RecordEventResponse{}, fmt.Errorf("no available service %s found", hyper.ServiceTypeAuthProvider) | ||||
| 	} | ||||
|  | ||||
| 	conn, err := instance.GetGrpcConn() | ||||
| 	if err != nil { | ||||
| 		return nil, fmt.Errorf("service is down: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||||
| 	defer cancel() | ||||
|  | ||||
| 	out, err := proto.NewEventRecorderClient(conn).RecordEvent(ctx, request) | ||||
| 	return out, err | ||||
| 	return forwardInvokeRequest( | ||||
| 		hyper.ServiceTypeAuthProvider, | ||||
| 		func(ctx context.Context, conn *grpc.ClientConn) (*proto.RecordEventResponse, error) { | ||||
| 			out, err := proto.NewEventRecorderClient(conn).RecordEvent(ctx, request) | ||||
| 			return out, err | ||||
| 		}, | ||||
| 	) | ||||
| } | ||||
|   | ||||
| @@ -17,6 +17,8 @@ type Server struct { | ||||
| 	proto.UnimplementedServiceDirectoryServer | ||||
| 	proto.UnimplementedStreamControllerServer | ||||
| 	proto.UnimplementedEventRecorderServer | ||||
| 	proto.UnimplementedNotifierServer | ||||
| 	proto.UnimplementedRealmServer | ||||
| 	proto.UnimplementedAuthServer | ||||
|  | ||||
| 	srv *grpc.Server | ||||
| @@ -30,6 +32,8 @@ func NewServer() *Server { | ||||
| 	proto.RegisterServiceDirectoryServer(server.srv, server) | ||||
| 	proto.RegisterStreamControllerServer(server.srv, server) | ||||
| 	proto.RegisterEventRecorderServer(server.srv, server) | ||||
| 	proto.RegisterNotifierServer(server.srv, server) | ||||
| 	proto.RegisterRealmServer(server.srv, server) | ||||
| 	proto.RegisterAuthServer(server.srv, server) | ||||
| 	health.RegisterHealthServer(server.srv, server) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user