Exemplo n.º 1
0
        public async Task <ExtractedResponse> RouteRequest(ExtractedRequest request)
        {
            // Organise the path to the final endpoint
            string basePath = '/' + request.Path.Split('/')[1];

            ExtractedResponse response = new ExtractedResponse();

            // Get the correct endpoint for the route requested
            Route route;

            try
            {
                route = Endpoints.First(r => r.Endpoint.Equals(basePath));
            }
            catch
            {
                throw new NotFoundCustomException("Route not found", $"Route matching {basePath} not found");
            }

            if (route.Destination.RequiresAuthentication)
            {
                // Auth stuff would go here, throwing exception for now
                throw new NotAuthorizedCustomException("Unauthorized", "You do not have permission to access this resource");
            }

            if (route.TransportType == TransportType.Http)
            {
                response = await httpClient.SendRequest(request, route);
            }
            else if (route.TransportType == TransportType.MessageQueue)
            {
                messageClient.PushMessageIntoQueue(request.GetJsonBytes(), route.Destination.MessageQueue);

                response.Body       = "{message: \"Message client does not support responses at the moment\"}";
                response.StatusCode = HttpStatusCode.OK;
            }

            return(response);
        }
Exemplo n.º 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IMessageClient messageClient, IHttpClient httpClient)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            Router router = new Router(messageClient, httpClient)
            {
                // Configuration included to help with HTTP API calls
                Configuration = Configuration
            };


            // Ignore requests to specified endpoints
            var ignoredRoutes = new List <string>()
            {
                "favicon.ico"
            };

            app.UseErrorHandler();
            app.UseMiddleware <IgnoreRoute>(ignoredRoutes);
            app.UseMiddleware <AddHeaders>();

            app.Run(async(context) =>
            {
                var extractedRequest = new ExtractedRequest(context.Request);
                messageClient.PushAuditMessage(extractedRequest.GetJsonBytes());

                var content = await router.RouteRequest(extractedRequest);
                context.Response.StatusCode = (int)content.StatusCode;
                content.Headers             = context.Response.Headers;

                messageClient.PushAuditMessage(content.GetJsonBytes());
                await context.Response.WriteAsync(content.Body);
            });
        }