Skip to content
Snippets Groups Projects
Commit f0727e45 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6730 Refactoring

parent 67b0aff0
No related branches found
No related tags found
No related merge requests found
http:
server:
port: 8383
port: 8082
grpc:
server:
port: 9093
mock: true
port: 50054
router:
rueckfragen:
port: 50051
......
......@@ -37,8 +37,8 @@ func TestLoadConfig(t *testing.T) {
config := LoadConfig()
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 8082
expectedConfig.Grpc.Server.Mock = false
expectedConfig.Http.Server.Port = 8082
expectedConfig.Grpc.Server.Port = 50054
expectedConfig.Grpc.Router.Rueckfragen.Port = 50051
expectedConfig.Grpc.Router.Commands.Port = 50052
......@@ -69,8 +69,8 @@ func TestLoadConfig(t *testing.T) {
}
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Server.Mock = false
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Server.Port = 1234
expectedConfig.Grpc.Router.Rueckfragen.Port = 5678
expectedConfig.Grpc.Router.Commands.Port = 1357
......@@ -98,8 +98,8 @@ func TestLoadConfig(t *testing.T) {
}
expectedConfig := Config{}
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Server.Mock = false
expectedConfig.Http.Server.Port = 9090
expectedConfig.Grpc.Server.Port = 1234
expectedConfig.Grpc.Router.Rueckfragen.Port = 5678
expectedConfig.Grpc.Router.Commands.Port = 50052
......
package mock
import "antragsraum-proxy/internal/logging"
import (
"antragsraum-proxy/internal/config"
"antragsraum-proxy/internal/logging"
)
var conf = config.LoadConfig()
var logger = logging.GetLogger()
......@@ -35,8 +35,6 @@ import (
"net"
)
const GrpcMockPort = 50054
type server struct {
pb.UnimplementedAntragraumServiceServer
}
......@@ -61,12 +59,12 @@ func StartGrpcServer() *grpc.Server {
s := grpc.NewServer()
pb.RegisterAntragraumServiceServer(s, &server{})
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GrpcMockPort))
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", conf.Grpc.Server.Port))
if err != nil {
logger.Fatal("gRPC server failed to listen: %v", err)
}
logger.Info("gRPC server listening on port %v", GrpcMockPort)
logger.Info("gRPC server listening on port %v", conf.Grpc.Server.Port)
if err := s.Serve(lis); err != nil {
logger.Fatal("gRPC server failed to serve: %v", err)
}
......
......@@ -41,7 +41,7 @@ func TestStartGrpcServer(t *testing.T) {
t.Run("should start gRPC server", func(t *testing.T) {
SetUpGrpcServer()
conn, err := net.DialTimeout("tcp", fmt.Sprintf("localhost:%d", GrpcMockPort), 2*time.Second)
conn, err := net.DialTimeout("tcp", fmt.Sprintf("localhost:%d", conf.Grpc.Server.Port), 2*time.Second)
assert.NoError(t, err)
......@@ -51,7 +51,7 @@ func TestStartGrpcServer(t *testing.T) {
setUpGrpcEnv := func() (pb.AntragraumServiceClient, func()) {
SetUpGrpcServer()
conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", GrpcMockPort), grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.NewClient(fmt.Sprintf("localhost:%d", conf.Grpc.Server.Port), grpc.WithTransportCredentials(insecure.NewCredentials()))
assert.NoError(t, err)
cleanup := func() {
......
......@@ -3,7 +3,7 @@ http:
port: 8082
grpc:
server:
port: 50052
port: 50054
router:
rueckfragen:
port: 50051
......
......@@ -28,7 +28,6 @@ package server
import (
pb "antragsraum-proxy/gen/go"
"antragsraum-proxy/internal/config"
"antragsraum-proxy/internal/mock"
"context"
"errors"
"fmt"
......@@ -41,8 +40,6 @@ import (
"net"
)
const GrpcAddressMetadata = "x-grpc-address"
type binaryFileRouter struct {
pb.UnimplementedBinaryFileServiceServer
}
......@@ -124,12 +121,12 @@ func (s *binaryFileRouter) getClientFromContext(ctx context.Context) (pb.BinaryF
return nil, nil, status.Error(codes.InvalidArgument, "unable to retrieve metadata")
}
grpcAddress := md.Get("x-grpc-address")
grpcAddress := md.Get(GrpcAddressMetadata)
if len(grpcAddress) == 0 {
return nil, nil, status.Error(codes.InvalidArgument, "grpc address is missing")
}
return createBinaryFileClient(md.Get(GrpcAddressMetadata)[0])
return createBinaryFileClient(grpcAddress[0])
}
func createBinaryFileClient(binaryFileServerAddress string) (pb.BinaryFileServiceClient, func() error, error) {
......@@ -145,12 +142,7 @@ func createBinaryFileClient(binaryFileServerAddress string) (pb.BinaryFileServic
}
func GetBinaryFileServerUrl(binaryFileServerAddress string, c config.Config) string {
binaryFileServerPort := c.Grpc.Server.Port
if c.Grpc.Server.Mock {
binaryFileServerPort = mock.GrpcMockPort
}
return fmt.Sprintf("%v:%d", binaryFileServerAddress, binaryFileServerPort)
return fmt.Sprintf("%v:%d", binaryFileServerAddress, c.Grpc.Server.Port)
}
func StartBinaryFileRouter() *grpc.Server {
......
......@@ -28,7 +28,6 @@ package server
import (
pb "antragsraum-proxy/gen/go"
"antragsraum-proxy/internal/config"
"antragsraum-proxy/internal/mock"
"context"
"errors"
"fmt"
......@@ -58,7 +57,7 @@ func (s *commandRouter) handleRequest(ctx context.Context, handler func(pb.Comma
return nil, status.Error(codes.InvalidArgument, "unable to retrieve metadata")
}
grpcAddress := md.Get("x-grpc-address")
grpcAddress := md.Get(GrpcAddressMetadata)
if len(grpcAddress) == 0 {
return nil, status.Error(codes.InvalidArgument, "grpc address is missing")
}
......@@ -85,12 +84,7 @@ func createCommandClient(commandServerAddress string) (pb.CommandServiceClient,
}
func GetCommandServerUrl(commandServerAddress string, c config.Config) string {
rueckfrageServerPort := c.Grpc.Server.Port
if c.Grpc.Server.Mock {
rueckfrageServerPort = mock.GrpcMockPort
}
return fmt.Sprintf("%v:%d", commandServerAddress, rueckfrageServerPort)
return fmt.Sprintf("%v:%d", commandServerAddress, c.Grpc.Server.Port)
}
func StartCommandRouter() *grpc.Server {
......
......@@ -5,5 +5,10 @@ import (
"antragsraum-proxy/internal/logging"
)
const (
GrpcAddressHeader = "X-Grpc-Address"
GrpcAddressMetadata = "x-grpc-address"
)
var conf = config.LoadConfig()
var logger = logging.GetLogger()
......@@ -38,8 +38,6 @@ import (
pb "antragsraum-proxy/gen/go"
)
const GrpcAddressHeader = "X-Grpc-Address"
func HeaderMatcher(header string) (string, bool) {
if header == GrpcAddressHeader {
return header, true
......@@ -54,23 +52,22 @@ func StartHttpGateway() *http.Server {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
rueckfrageRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Rueckfragen.Port)
commandRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Commands.Port)
binaryFileRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Binaryfiles.Port)
mux := runtime.NewServeMux(runtime.WithErrorHandler(ErrorHandler), runtime.WithIncomingHeaderMatcher(HeaderMatcher))
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
rueckfrageRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Rueckfragen.Port)
err := pb.RegisterAntragraumServiceHandlerFromEndpoint(ctx, mux, rueckfrageRouterUrl, opts)
if err != nil {
logger.Fatal("failed to start HTTP gateway: %v", err)
}
commandRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Commands.Port)
err = pb.RegisterCommandServiceHandlerFromEndpoint(ctx, mux, commandRouterUrl, opts)
if err != nil {
logger.Fatal("failed to start HTTP gateway: %v", err)
}
binaryFileRouterUrl := fmt.Sprintf("localhost:%d", conf.Grpc.Router.Binaryfiles.Port)
err = pb.RegisterBinaryFileServiceHandlerFromEndpoint(ctx, mux, binaryFileRouterUrl, opts)
if err != nil {
logger.Fatal("failed to start HTTP gateway: %v", err)
......@@ -84,7 +81,7 @@ func StartHttpGateway() *http.Server {
}
logger.Info("HTTP gateway listening on port %d", conf.Http.Server.Port)
if err := httpServer.ListenAndServe(); err != nil {
if err = httpServer.ListenAndServe(); err != nil {
logger.Fatal("HTTP gateway failed to serve: %v", err)
}
......
......@@ -28,7 +28,6 @@ package server
import (
pb "antragsraum-proxy/gen/go"
"antragsraum-proxy/internal/config"
"antragsraum-proxy/internal/mock"
"context"
"errors"
"fmt"
......@@ -74,7 +73,7 @@ func (s *rueckfrageRouter) handleRequest(ctx context.Context, handler func(pb.An
return nil, status.Error(codes.InvalidArgument, "unable to retrieve metadata")
}
grpcAddress := md.Get("x-grpc-address")
grpcAddress := md.Get(GrpcAddressMetadata)
if len(grpcAddress) == 0 {
return nil, status.Error(codes.InvalidArgument, "grpc address is missing")
}
......@@ -101,12 +100,7 @@ func createRueckfrageClient(rueckfrageServerAddress string) (pb.AntragraumServic
}
func GetRueckfrageServerUrl(rueckfrageServerAddress string, c config.Config) string {
rueckfrageServerPort := c.Grpc.Server.Port
if c.Grpc.Server.Mock {
rueckfrageServerPort = mock.GrpcMockPort
}
return fmt.Sprintf("%v:%d", rueckfrageServerAddress, rueckfrageServerPort)
return fmt.Sprintf("%v:%d", rueckfrageServerAddress, c.Grpc.Server.Port)
}
func StartRueckfrageRouter() *grpc.Server {
......
......@@ -2,7 +2,8 @@ http:
server:
port: 8080
grpc:
mock: true
server:
port: 50054
router:
rueckfragen:
port: 50051
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment