Exemplo n.º 1
0
        internal static void UseHubs(this IApplicationBuilder app, IApiOrchestrator apiOrchestrator)
        {
            if (apiOrchestrator.StartGatewayHub)
            {
                var gatewayConn = new HubConnectionBuilder()
                                  .WithUrl(apiOrchestrator.GatewayHubUrl)
                                  .WithAutomaticReconnect()
                                  .AddNewtonsoftJsonProtocol()
                                  .Build();

                gatewayConn.StartAsync().ConfigureAwait(false);

                apiOrchestrator.Hubs.ToList().ForEach(async hub =>
                {
                    var connection = hub.Value.Connection;

                    await connection.StartAsync();

                    hub.Value.Mediator.Paths.ToList().ForEach(path =>
                    {
                        var route = hub.Value.Mediator.GetRoute(path.Key).HubRoute;

                        connection.On(route.ReceiveMethod, route.ReceiveParameterTypes, async(arg1, arg2) =>
                        {
                            await gatewayConn.InvokeAsync("Receive", new HubReceiveAuth {
                                Hub = hub.Key, ReceiveKey = hub.Value.ReceiveKey
                            }, route, arg1, arg2);
                        }, new object());
                    });
                });
            }
        }
Exemplo n.º 2
0
        public async Task Invoke(HttpContext context, IApiOrchestrator orchestrator, ILogger <ApiGatewayLog> logger)
        {
            ApiInfo          apiInfo   = null;
            HubInfo          hubInfo   = null;
            GatewayRouteInfo routeInfo = null;

            try
            {
                var path = context.Request.Path.Value;

                var segmentsMatch = Regex.Match(path, "^/?api/Gateway(/(?!orchestration)(hub/)?(?<api>.*?)/(?<key>.*?)(/.*?)?)?$",
                                                RegexOptions.IgnoreCase | RegexOptions.Compiled);

                if (segmentsMatch.Success)
                {
                    var api = segmentsMatch.Groups["api"].Captures[0].Value;
                    var key = segmentsMatch.Groups["key"].Captures[0].Value;

                    if (path.ToLower().Contains("api/gateway/hub"))
                    {
                        hubInfo = orchestrator.GetHub(api);

                        routeInfo = hubInfo.Mediator.GetRoute(key);
                    }
                    else
                    {
                        apiInfo = orchestrator.GetApi(api.ToString());

                        routeInfo = apiInfo.Mediator.GetRoute(key.ToString());
                    }

                    if (routeInfo.Verb.ToString() != context.Request.Method.ToUpper())
                    {
                        throw new Exception("Invalid verb");
                    }
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Api Gateway Orchestration api/key error.");

                context.Response.StatusCode = StatusCodes.Status404NotFound;

                await context.Response.CompleteAsync();

                return;
            }

            try
            {
                await _next(context);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Api Gateway error.");

                await HandleExceptionAsync(context, ex);
            }
        }
        public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
        {
            var serviceProvider = app.ApplicationServices;

            var weatherService = serviceProvider.GetService <IWeatherService>();

            var weatherApiClientConfig = weatherService.GetClientConfig();

            orchestrator.StartGatewayHub = false;
            orchestrator.GatewayHubUrl   = "https://localhost:44360/GatewayHub";

            orchestrator.AddApi("weatherservice", "http://localhost:63969/")
            //Get
            .AddRoute("forecast", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/forecast", ResponseType = typeof(IEnumerable <WeatherForecast>)
            })
            //Head
            .AddRoute("forecasthead", GatewayVerb.HEAD, new RouteInfo {
                Path = "weatherforecast/forecast"
            })
            //Get using custom HttpClient
            .AddRoute("types", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/types", ResponseType = typeof(string[]), HttpClientConfig = weatherApiClientConfig
            })
            //Get with param using custom HttpClient
            .AddRoute("type", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/types/", ResponseType = typeof(WeatherTypeResponse), HttpClientConfig = weatherApiClientConfig
            })
            //Get using custom implementation
            .AddRoute("typescustom", GatewayVerb.GET, weatherService.GetTypes)
            //Post
            .AddRoute("add", GatewayVerb.POST, new RouteInfo {
                Path = "weatherforecast/types/add", RequestType = typeof(AddWeatherTypeRequest), ResponseType = typeof(string[])
            })
            //Put
            .AddRoute("update", GatewayVerb.PUT, new RouteInfo {
                Path = "weatherforecast/types/update", RequestType = typeof(UpdateWeatherTypeRequest), ResponseType = typeof(string[])
            })
            //Patch
            .AddRoute("patch", GatewayVerb.PATCH, new RouteInfo {
                Path = "weatherforecast/forecast/patch", ResponseType = typeof(WeatherForecast)
            })
            //Delete
            .AddRoute("remove", GatewayVerb.DELETE, new RouteInfo {
                Path = "weatherforecast/types/remove/", ResponseType = typeof(string[])
            })
            .AddApi("stockservice", "http://localhost:63967/")
            .AddRoute("stocks", GatewayVerb.GET, new RouteInfo {
                Path = "stock", ResponseType = typeof(IEnumerable <StockQuote>)
            })
            .AddRoute("stock", GatewayVerb.GET, new RouteInfo {
                Path = "stock/", ResponseType = typeof(StockQuote)
            })
            .AddHub("chatservice", BuildHubConnection, "2f85e3c6-66d2-48a3-8ff7-31a65073558b")
            .AddRoute("room", new HubRouteInfo {
                InvokeMethod = "SendMessage", ReceiveMethod = "ReceiveMessage", ReceiveParameterTypes = new Type[] { typeof(string), typeof(string) }
            });
        }
Exemplo n.º 4
0
 public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
 {
     orchestrator.AddApi("weatherservice", "http://localhost:58262/")
     .AddRoute("forecast", new RouteInfo {
         Path = "weatherforecast/forecast", ResponseType = typeof(IEnumerable <WeatherForecast>)
     })
     .ToOrchestrator()
     .AddApi("stockservice", "http://localhost:58352/")
     .AddRoute("stocks", new RouteInfo {
         Path = "stock", ResponseType = typeof(IEnumerable <StockQuote>)
     });
 }
        public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
        {
            var serviceProvider = app.ApplicationServices;

            var weatherService = serviceProvider.GetService <IWeatherService>();

            var weatherApiClientConfig = weatherService.GetClientConfig();

            orchestrator.AddApi("weatherservice", "http://localhost:63969/")
            //Get
            .AddRoute("forecast", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/forecast", ResponseType = typeof(IEnumerable <WeatherForecast>)
            })
            //Head
            .AddRoute("forecasthead", GatewayVerb.HEAD, new RouteInfo {
                Path = "weatherforecast/forecast"
            })
            //Get using custom HttpClient
            .AddRoute("types", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/types", ResponseType = typeof(string[]), HttpClientConfig = weatherApiClientConfig
            })
            //Get with param using custom HttpClient
            .AddRoute("type", GatewayVerb.GET, new RouteInfo {
                Path = "weatherforecast/types/", ResponseType = typeof(WeatherTypeResponse), HttpClientConfig = weatherApiClientConfig
            })
            //Get using custom implementation
            .AddRoute("typescustom", GatewayVerb.GET, weatherService.GetTypes)
            //Post
            .AddRoute("add", GatewayVerb.POST, new RouteInfo {
                Path = "weatherforecast/types/add", RequestType = typeof(AddWeatherTypeRequest), ResponseType = typeof(string[])
            })
            //Put
            .AddRoute("update", GatewayVerb.PUT, new RouteInfo {
                Path = "weatherforecast/types/update", RequestType = typeof(UpdateWeatherTypeRequest), ResponseType = typeof(string[])
            })
            //Patch
            .AddRoute("patch", GatewayVerb.PATCH, new RouteInfo {
                Path = "weatherforecast/forecast/patch", ResponseType = typeof(WeatherForecast)
            })
            //Delete
            .AddRoute("remove", GatewayVerb.DELETE, new RouteInfo {
                Path = "weatherforecast/types/remove/", ResponseType = typeof(string[])
            })
            .AddApi("stockservice", "http://localhost:63967/")
            .AddRoute("stocks", GatewayVerb.GET, new RouteInfo {
                Path = "stock", ResponseType = typeof(IEnumerable <StockQuote>)
            })
            .AddRoute("stock", GatewayVerb.GET, new RouteInfo {
                Path = "stock/", ResponseType = typeof(StockQuote)
            });
        }
Exemplo n.º 6
0
 public StudentController(IApiOrchestrator apiOrchestrator)
 {
     _orchestrator = apiOrchestrator;
 }
Exemplo n.º 7
0
 public Mediator(IApiOrchestrator apiOrchestrator)
 {
     _apiOrchestrator = apiOrchestrator;
 }
Exemplo n.º 8
0
 public OrderController(IApiOrchestrator apiOrchestrator, ILogger <ApiGatewayLog> logger, IApiClient apiClient)
 {
     _apiOrchestrator = apiOrchestrator;
     _logger          = logger;
     _apiClient       = apiClient;
 }
 public GatewayController(IApiOrchestrator apiOrchestrator, ILogger <ApiGatewayLog> logger)
 {
     _apiOrchestrator = apiOrchestrator;
     _logger          = logger;
 }
 public GatewayController(IApiOrchestrator apiOrchestrator, ILogger <ApiGatewayLog> logger, IHttpService httpService)
 {
     _apiOrchestrator = apiOrchestrator;
     _logger          = logger;
     _httpService     = httpService;
 }
Exemplo n.º 11
0
 public static void Create(IApiOrchestrator orchestrator, IApplicationBuilder app)
 {
     orchestrator
     //ClassificationService
     .AddApi("ClassificationService", "http://localhost:5002/")
     //category
     .AddRoute("category", GatewayVerb.GET,
               new RouteInfo
     {
         Path = "ClassificationService/category"
     })
     .AddRoute("category", GatewayVerb.POST,
               new RouteInfo
     {
         Path = "ClassificationService/category"
     })
     .AddRoute("category", GatewayVerb.DELETE,
               new RouteInfo
     {
         Path = "ClassificationService/category/{0}"
     })
     .AddRoute("category", GatewayVerb.PUT,
               new RouteInfo
     {
         Path = "ClassificationService/category/{0}"
     })
     //supplier
     .AddRoute("Supplier", GatewayVerb.GET,
               new RouteInfo
     {
         Path = "ClassificationService/Supplier"
     })
     .AddRoute("Supplier", GatewayVerb.POST,
               new RouteInfo
     {
         Path = "ClassificationService/Supplier"
     })
     .AddRoute("Supplier", GatewayVerb.DELETE,
               new RouteInfo
     {
         Path = "ClassificationService/Supplier/{0}"
     })
     .AddRoute("Supplier", GatewayVerb.PUT,
               new RouteInfo
     {
         Path = "ClassificationService/Supplier/{0}"
     })
     //ProductService
     .AddApi("ProductService", "http://localhost:5003/")
     //product
     .AddRoute("product", GatewayVerb.GET,
               new RouteInfo
     {
         Path = "ProductService/product"
     })
     .AddRoute("product", GatewayVerb.POST,
               new RouteInfo
     {
         Path = "ProductService/product"
     })
     .AddRoute("product", GatewayVerb.DELETE,
               new RouteInfo
     {
         Path = "ProductService/product/{0}"
     })
     .AddRoute("product", GatewayVerb.PUT,
               new RouteInfo
     {
         Path = "ProductService/product/{0}"
     })
     //ProductService
     .AddApi("OrderService", "http://localhost:5004/")
     //order
     .AddRoute("order", GatewayVerb.GET,
               new RouteInfo
     {
         Path = "OrderService/order"
     })
     .AddRoute("order", GatewayVerb.POST,
               new RouteInfo
     {
         Path = "OrderService/order"
     })
     .AddRoute("order", GatewayVerb.DELETE,
               new RouteInfo
     {
         Path = "OrderService/order"
     })
     .AddRoute("order", GatewayVerb.PUT,
               new RouteInfo
     {
         Path = "OrderService/order/{0}"
     });
 }
 public GatewayHub(IApiOrchestrator apiOrchestrator)
 {
     _apiOrchestrator = apiOrchestrator;
 }
Exemplo n.º 13
0
 public EnrolmentController(IApiOrchestrator apiOrchestrator)
 {
     _orchestrator = apiOrchestrator;
 }