mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 17:08:15 +08:00
Added some wrapper around the http context so i can at least pretend to access things from it without casting them
This commit is contained in:
@ -0,0 +1,11 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Services
|
||||
{
|
||||
public class CannotAddDataError : Error
|
||||
{
|
||||
public CannotAddDataError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Services
|
||||
{
|
||||
public class CannotFindDataError : Error
|
||||
{
|
||||
public CannotFindDataError(string message) : base(message)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Services
|
||||
{
|
||||
public interface IRequestDataService
|
||||
{
|
||||
Response Add<T>(string key, T value);
|
||||
Response<T> Get<T>(string key);
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.Responses;
|
||||
|
||||
namespace Ocelot.Library.Infrastructure.Services
|
||||
{
|
||||
public class RequestDataService : IRequestDataService
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public RequestDataService(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
public Response Add<T>(string key, T value)
|
||||
{
|
||||
try
|
||||
{
|
||||
_httpContextAccessor.HttpContext.Items.Add(key, value);
|
||||
return new OkResponse();
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new ErrorResponse(new List<Error>
|
||||
{
|
||||
new CannotAddDataError(string.Format($"Unable to add data for key: {key}, exception: {exception.Message}"))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public Response<T> Get<T>(string key)
|
||||
{
|
||||
object obj;
|
||||
|
||||
if(_httpContextAccessor.HttpContext.Items.TryGetValue(key, out obj))
|
||||
{
|
||||
var data = (T) obj;
|
||||
return new OkResponse<T>(data);
|
||||
}
|
||||
|
||||
return new ErrorResponse<T>(new List<Error>
|
||||
{
|
||||
new CannotFindDataError($"Unable to find data for key: {key}")
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.Responder;
|
||||
using Ocelot.Library.Infrastructure.Services;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
@ -10,14 +11,17 @@ namespace Ocelot.Library.Middleware
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IDownstreamRouteFinder _downstreamRouteFinder;
|
||||
private readonly IHttpResponder _responder;
|
||||
private readonly IRequestDataService _requestDataService;
|
||||
|
||||
public DownstreamRouteFinderMiddleware(RequestDelegate next,
|
||||
IDownstreamRouteFinder downstreamRouteFinder,
|
||||
IHttpResponder responder)
|
||||
IHttpResponder responder,
|
||||
IRequestDataService requestDataService)
|
||||
{
|
||||
_next = next;
|
||||
_downstreamRouteFinder = downstreamRouteFinder;
|
||||
_responder = responder;
|
||||
_requestDataService = requestDataService;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
@ -32,7 +36,7 @@ namespace Ocelot.Library.Middleware
|
||||
return;
|
||||
}
|
||||
|
||||
context.Items.Add("DownstreamRoute", downstreamRoute.Data);
|
||||
_requestDataService.Add("DownstreamRoute", downstreamRoute.Data);
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.DownstreamRouteFinder;
|
||||
using Ocelot.Library.Infrastructure.Responder;
|
||||
using Ocelot.Library.Infrastructure.Services;
|
||||
using Ocelot.Library.Infrastructure.UrlTemplateReplacer;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
@ -9,34 +11,35 @@ namespace Ocelot.Library.Middleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IDownstreamUrlTemplateVariableReplacer _urlReplacer;
|
||||
private readonly IRequestDataService _requestDataService;
|
||||
private readonly IHttpResponder _responder;
|
||||
|
||||
public DownstreamUrlCreatorMiddleware(RequestDelegate next,
|
||||
IDownstreamUrlTemplateVariableReplacer urlReplacer)
|
||||
IDownstreamUrlTemplateVariableReplacer urlReplacer,
|
||||
IRequestDataService requestDataService,
|
||||
IHttpResponder responder)
|
||||
{
|
||||
_next = next;
|
||||
_urlReplacer = urlReplacer;
|
||||
_requestDataService = requestDataService;
|
||||
_responder = responder;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
var downstreamRoute = GetDownstreamRouteFromOwinItems(context);
|
||||
var downstreamRoute = _requestDataService.Get<DownstreamRoute>("DownstreamRoute");
|
||||
|
||||
var downstreamUrl = _urlReplacer.ReplaceTemplateVariables(downstreamRoute);
|
||||
|
||||
context.Items.Add("DownstreamUrl", downstreamUrl);
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
|
||||
private DownstreamRoute GetDownstreamRouteFromOwinItems(HttpContext context)
|
||||
{
|
||||
object obj;
|
||||
DownstreamRoute downstreamRoute = null;
|
||||
if (context.Items.TryGetValue("DownstreamRoute", out obj))
|
||||
if (downstreamRoute.IsError)
|
||||
{
|
||||
downstreamRoute = (DownstreamRoute) obj;
|
||||
await _responder.CreateNotFoundResponse(context);
|
||||
return;
|
||||
}
|
||||
return downstreamRoute;
|
||||
|
||||
var downstreamUrl = _urlReplacer.ReplaceTemplateVariables(downstreamRoute.Data);
|
||||
|
||||
_requestDataService.Add("DownstreamUrl", downstreamUrl);
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Ocelot.Library.Infrastructure.Requester;
|
||||
using Ocelot.Library.Infrastructure.Responder;
|
||||
using Ocelot.Library.Infrastructure.Services;
|
||||
|
||||
namespace Ocelot.Library.Middleware
|
||||
{
|
||||
@ -10,38 +11,36 @@ namespace Ocelot.Library.Middleware
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IHttpRequester _requester;
|
||||
private readonly IHttpResponder _responder;
|
||||
private readonly IRequestDataService _requestDataService;
|
||||
|
||||
public HttpRequesterMiddleware(RequestDelegate next,
|
||||
IHttpRequester requester,
|
||||
IHttpResponder responder)
|
||||
IHttpResponder responder,
|
||||
IRequestDataService requestDataService)
|
||||
{
|
||||
_next = next;
|
||||
_requester = requester;
|
||||
_responder = responder;
|
||||
_requestDataService = requestDataService;
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
var downstreamUrl = GetDownstreamUrlFromOwinItems(context);
|
||||
var downstreamUrl = _requestDataService.Get<string>("DownstreamUrl");
|
||||
|
||||
if (downstreamUrl.IsError)
|
||||
{
|
||||
await _responder.CreateNotFoundResponse(context);
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await _requester
|
||||
.GetResponse(context.Request.Method, downstreamUrl, context.Request.Body,
|
||||
.GetResponse(context.Request.Method, downstreamUrl.Data, context.Request.Body,
|
||||
context.Request.Headers, context.Request.Cookies, context.Request.Query, context.Request.ContentType);
|
||||
|
||||
await _responder.CreateResponse(context, response);
|
||||
|
||||
await _next.Invoke(context);
|
||||
}
|
||||
|
||||
private string GetDownstreamUrlFromOwinItems(HttpContext context)
|
||||
{
|
||||
object obj;
|
||||
string downstreamUrl = null;
|
||||
if (context.Items.TryGetValue("DownstreamUrl", out obj))
|
||||
{
|
||||
downstreamUrl = (string) obj;
|
||||
}
|
||||
return downstreamUrl;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user