Skip to content

Adding ProxyResultBase for shared code #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 4 additions & 42 deletions src/Azure.WebJobs.Extensions.HttpApi/Proxy/ProxyResult.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,21 @@
using System;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

namespace Azure.WebJobs.Extensions.HttpApi.Proxy
{
internal class ProxyResult : IActionResult
internal class ProxyResult : ProxyResultBase
{
public ProxyResult(string backendUri)
: base(backendUri)
{
_backendUri = backendUri;
}

private readonly string _backendUri;

private static readonly Regex _templateRegex = new Regex(@"\{([^\{\}]+)\}", RegexOptions.Compiled);

public ProxyInvoker ProxyInvoker { get; set; }

public Action<HttpRequestMessage> Before { get; set; }

public Action<HttpResponseMessage> After { get; set; }

public async Task ExecuteResultAsync(ActionContext context)
{
try
{
var backendUri = MakeBackendUri(context);

await ProxyInvoker.SendAsync(backendUri, context.HttpContext, Before, After);
}
catch
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
}

private string MakeBackendUri(ActionContext context)
{
var routeValues = context.RouteData.Values;
protected override void BeforeSend(HttpRequestMessage request) => Before?.Invoke(request);

var backend = _templateRegex.Replace(_backendUri, match =>
{
if (routeValues.TryGetValue(match.Groups[1].Value, out var value) && value != null)
{
return value.ToString();
}

return "";
});

return backend;
}
protected override void AfterSend(HttpResponseMessage response) => After?.Invoke(response);
}
}
62 changes: 62 additions & 0 deletions src/Azure.WebJobs.Extensions.HttpApi/Proxy/ProxyResultBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc;

namespace Azure.WebJobs.Extensions.HttpApi.Proxy
{
internal class ProxyResultBase : IActionResult
{
protected ProxyResultBase(string backendUri)
{
_backendUri = backendUri;
}

private readonly string _backendUri;

private static readonly Regex _templateRegex = new Regex(@"\{([^\{\}]+)\}", RegexOptions.Compiled);

public ProxyInvoker ProxyInvoker { get; set; }

public async Task ExecuteResultAsync(ActionContext context)
{
try
{
var backendUri = MakeBackendUri(context);

await ProxyInvoker.SendAsync(backendUri, context.HttpContext, BeforeSend, AfterSend);
}
catch
{
context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
}
}

protected virtual void BeforeSend(HttpRequestMessage request)
{
}

protected virtual void AfterSend(HttpResponseMessage response)
{
}

private string MakeBackendUri(ActionContext context)
{
var routeValues = context.RouteData.Values;

var backend = _templateRegex.Replace(_backendUri, match =>
{
if (routeValues.TryGetValue(match.Groups[1].Value, out var value) && value != null)
{
return value.ToString();
}

return "";
});

return backend;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

namespace Azure.WebJobs.Extensions.HttpApi.Proxy
{
internal class StaticWebsiteResult : ProxyResult
internal class StaticWebsiteResult : ProxyResultBase
{
public StaticWebsiteResult(string backendUri)
: base(backendUri)
{
After = HandleAfter;
}

public string FallbackExclude { get; set; }

private void HandleAfter(HttpResponseMessage response)
protected override void AfterSend(HttpResponseMessage response)
{
var request = response.RequestMessage;

Expand Down