Created
July 7, 2015 23:37
-
-
Save beattyml1/e2baa5e64cc2c1f3d6d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace MicroGate | |
{ | |
public class Route | |
{ | |
public bool KeepResourceName { get; set; } | |
public string BaseUrl { get; set; } | |
public Func<string, string, HttpRequest, bool> Authenticate { get; set; } | |
public Func<string, string, HttpRequest, bool> Authorize { get; set; } | |
public IEnumerable<Func<string, string, bool>> AdditionalAllowFunctions { get; set; } | |
public Func<string, string, WebRequest, WebRequest> RequestTransfroms { get; set; } | |
public Func<string, string, HttpResonse, HttpResonse> ResonseTransfroms { get; set; } | |
} | |
public class IRouteCollection : IDictionary<string, Route> | |
{ | |
public AddRoute(string resourceName, string baseUrl, bool keepResourceName, | |
Func<string, string, HttpRequest, bool> authenticate, | |
Func<string, string, HttpRequest, bool> authorize, | |
Func<string, string, WebRequest, WebRequest> transformRequest, | |
Func<string, string, HttpResonse, HttpResonse> transformResponse | |
); | |
} | |
public class ApiGatewayController : ApiController | |
{ | |
private readonly IRouteCollection Routes; | |
public ApiGatewayController(ApiGatewaySettings settings) | |
{ | |
Routes = settings.Routes; | |
} | |
public HttpResonse Route(string resource, string path) | |
{ | |
var requestString = Request.Url.PathAndQuery; | |
var route = Routes[resource]; | |
var absolutePathOut = route.route.KeepResourceName ? String.Format("{0}/{1}", resource, path) : path; | |
var absoluteUrlOut = String.Format("{0}/{1}", route.BaseUrl, absolutePathOut); | |
if (!route.Authenticate(resource, path, Request)) | |
return new HttpResonse(); | |
if (!route.Authorize(resource, path, Request)) | |
return new HttpResonse(); | |
foreach (var func in route.AdditionalAllowFunctions) | |
if (!func()) | |
return new HttpResonse(); | |
var request = WebRequest.Create(absoluteUrlOut); | |
request.ContentLength = Request.ContentLength; | |
request.ContentType = Request.ContentType; | |
request.CreatorInstance = Request.; | |
request.Method = Request.HttpMethod; | |
request.Headers = Request.Headers; | |
var requestTransform = route.RequestTransfroms.Aggregate((acc, next) => next(resource, path, acc(resource, path, request))); | |
var finalRequest = requestTransform(request); | |
var webResponse = request.GetResonse(); | |
var response = // TODO | |
var responseTransform = route.ResponseTransforms.Aggregate((acc, next) => next(resource, path, acc(resource, path, response))); | |
var finalResponse = responseTransform(response); | |
return finalResponse; | |
} | |
} | |
} | |
routes.MapRoute("Name", String.Format("{0}/{resource}/{*path}", pathToGateway), new { controller = ..., action = ... }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment