Newer
Older
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
* Lizenziert unter der EUPL, Version 1.2 oder - sobald
* diese von der Europäischen Kommission genehmigt wurden -
* Folgeversionen der EUPL ("Lizenz");
* Sie dürfen dieses Werk ausschließlich gemäß
* dieser Lizenz nutzen.
* Eine Kopie der Lizenz finden Sie hier:
*
* https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* Sofern nicht durch anwendbare Rechtsvorschriften
* gefordert oder in schriftlicher Form vereinbart, wird
* die unter der Lizenz verbreitete Software "so wie sie
* ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
* ausdrücklich oder stillschweigend - verbreitet.
* Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen.
*/
package server
import (
pb "antragsraum-proxy/gen/go"
"antragsraum-proxy/internal/config"
"context"
"errors"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"net"
)
type rueckfrageRouter struct {
pb.UnimplementedAntragraumServiceServer
}
func (s *rueckfrageRouter) FindRueckfragen(ctx context.Context, in *pb.GrpcFindRueckfragenRequest) (*pb.GrpcFindRueckfragenResponse, error) {
response, err := s.handleRequest(ctx, func(client pb.AntragraumServiceClient, ctx context.Context) (interface{}, error) {
return client.FindRueckfragen(ctx, in)
})
return response.(*pb.GrpcFindRueckfragenResponse), err
}
func (s *rueckfrageRouter) GetRueckfrage(ctx context.Context, in *pb.GrpcGetRueckfrageRequest) (*pb.GrpcGetRueckfrageResponse, error) {
response, err := s.handleRequest(ctx, func(client pb.AntragraumServiceClient, ctx context.Context) (interface{}, error) {
return client.GetRueckfrage(ctx, in)
})
return response.(*pb.GrpcGetRueckfrageResponse), err
}
func (s *rueckfrageRouter) SendRueckfrageAnswer(ctx context.Context, in *pb.GrpcSendRueckfrageAnswerRequest) (*pb.GrpcSendRueckfrageAnswerResponse, error) {
response, err := s.handleRequest(ctx, func(client pb.AntragraumServiceClient, ctx context.Context) (interface{}, error) {
return client.SendRueckfrageAnswer(ctx, in)
})
return response.(*pb.GrpcSendRueckfrageAnswerResponse), err
}
func (s *rueckfrageRouter) handleRequest(ctx context.Context, handler func(pb.AntragraumServiceClient, context.Context) (interface{}, error)) (interface{}, error) {
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
return handleRequest(ctx, createRueckfrageClient, handler)
}
func createRueckfrageClient(grpcAddress string) (pb.AntragraumServiceClient, func() error, error) {
target := getGrpcServerUrl(grpcAddress, conf)
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Error("rueckfrage router failed to route: %v", err)
return nil, nil, errors.New("rueckfrage router failed to route")
}
return pb.NewAntragraumServiceClient(conn), conn.Close, nil
}
type commandRouter struct {
pb.UnimplementedCommandServiceServer
}
func (s *commandRouter) GetCommand(ctx context.Context, in *pb.GrpcGetCommandRequest) (*pb.GrpcCommand, error) {
response, err := s.handleRequest(ctx, func(client pb.CommandServiceClient, ctx context.Context) (interface{}, error) {
return client.GetCommand(ctx, in)
})
return response.(*pb.GrpcCommand), err
}
func (s *commandRouter) handleRequest(ctx context.Context, handler func(pb.CommandServiceClient, context.Context) (interface{}, error)) (interface{}, error) {
return handleRequest(ctx, createCommandClient, handler)
}
func createCommandClient(grpcAddress string) (pb.CommandServiceClient, func() error, error) {
target := getGrpcServerUrl(grpcAddress, conf)
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Error("command router failed to route: %v", err)
return nil, nil, errors.New("command router failed to route")
}
return pb.NewCommandServiceClient(conn), conn.Close, nil
}
type binaryFileRouter struct {
pb.UnimplementedBinaryFileServiceServer
}
func (s *binaryFileRouter) FindBinaryFilesMetaData(ctx context.Context, in *pb.GrpcBinaryFilesRequest) (*pb.GrpcFindFilesResponse, error) {
response, err := s.handleRequest(ctx, func(client pb.BinaryFileServiceClient, ctx context.Context) (interface{}, error) {
return client.FindBinaryFilesMetaData(ctx, in)
})
return response.(*pb.GrpcFindFilesResponse), err
}
func (s *binaryFileRouter) handleRequest(ctx context.Context, handler func(pb.BinaryFileServiceClient, context.Context) (interface{}, error)) (interface{}, error) {
return handleRequest(ctx, createBinaryFileClient, handler)
}
func createBinaryFileClient(grpcAddress string) (pb.BinaryFileServiceClient, func() error, error) {
target := getGrpcServerUrl(grpcAddress, conf)
conn, err := grpc.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Error("binary file router failed to route: %v", err)
return nil, nil, errors.New("binary file router failed to route")
}
return pb.NewBinaryFileServiceClient(conn), conn.Close, nil
}
func getGrpcServerUrl(grpcAddress string, c config.Config) string {
return fmt.Sprintf("%v:%d", grpcAddress, c.Grpc.Server.Port)
}
func handleRequest[T any](
ctx context.Context,
createClientFunc func(grpcAddress string) (T, func() error, error),
handler func(T, context.Context) (interface{}, error),
) (interface{}, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, status.Error(codes.InvalidArgument, "unable to retrieve metadata")
}
if len(grpcAddress) == 0 {
return nil, status.Error(codes.InvalidArgument, "grpc address is missing")
}
client, connCleanUp, err := createClientFunc(grpcAddress[0])
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
defer connCleanUp()
return handler(client, ctx)
}
func StartGrpcRouter() *grpc.Server {
s := grpc.NewServer()
pb.RegisterAntragraumServiceServer(s, &rueckfrageRouter{})
pb.RegisterCommandServiceServer(s, &commandRouter{})
pb.RegisterBinaryFileServiceServer(s, &binaryFileRouter{})
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.Grpc.Router.Port))
logger.Fatal("gRPC router failed to listen: %v", err)
logger.Info("gRPC router listening on port %d", conf.Grpc.Router.Port)
if err := s.Serve(lis); err != nil {
logger.Fatal("gRPC router failed to serve: %v", err)