Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        public IMediator AddRoute(string key, GatewayVerb verb, RouteInfo routeInfo)
        {
            var gatewayRouteInfo = new GatewayRouteInfo
            {
                Verb  = verb,
                Route = routeInfo
            };

            paths.Add(key.ToLower(), gatewayRouteInfo);

            return(this);
        }
        public IHubMediator AddRoute(string key, HubRouteInfo routeInfo)
        {
            var gatewayRouteInfo = new GatewayRouteInfo
            {
                Verb     = GatewayVerb.POST,
                HubRoute = routeInfo
            };

            paths.Add(key.ToLower(), gatewayRouteInfo);

            return(this);
        }
Ejemplo n.º 4
0
        public IMediator AddRoute(string key, GatewayVerb verb, Func <ApiInfo, HttpRequest, Task <object> > exec)
        {
            var gatewayRouteInfo = new GatewayRouteInfo
            {
                Verb  = verb,
                Route = new RouteInfo
                {
                    Exec = exec
                }
            };

            paths.Add(key.ToLower(), gatewayRouteInfo);

            return(this);
        }