Skip to content

Automatically complete catch-all route parameter #23

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
Nov 10, 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ public class Function1 : HttpFunctionBase
ILogger log)
{
#if USE_PROXY
return ProxyStaticApp("https://example.com/{path}", fallbackExclude: $"^/_nuxt/.*");
return ProxyStaticApp("https://example.com", fallbackExclude: $"^/_nuxt/.*");
#else
return LocalStaticApp($"{path}", fallbackExclude: $"^/_nuxt/.*");
return LocalStaticApp(fallbackPath: "404.html", fallbackExclude: $"^/_nuxt/.*");
#endif
}
}
Expand Down
2 changes: 1 addition & 1 deletion samples/ProxySample/ReverseProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
HttpRequest req)
{
return Proxy("https://shibayan.jp/{path}");
return Proxy("https://shibayan.jp");
}
}
}
7 changes: 3 additions & 4 deletions samples/WebsiteSample/StaticWebsite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ public StaticWebsite(IHttpContextAccessor httpContextAccessor)
[FunctionName(nameof(StaticWebsite))]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
HttpRequest req,
string path)
HttpRequest req)
{
//return ProxyStaticApp("https://ststaticwebsiteproxy.z11.web.core.windows.net/{path}", fallbackExclude: $"^/_nuxt/.*");
//return ProxyStaticApp("https://ststaticwebsiteproxy.z11.web.core.windows.net", fallbackExclude: $"^/_nuxt/.*");

return LocalStaticApp(path, fallbackPath: "200.html", fallbackExclude: $"^/_nuxt/.*");
return LocalStaticApp(fallbackPath: "200.html", fallbackExclude: $"^/_nuxt/.*");
}
}
}
4 changes: 2 additions & 2 deletions src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ protected IActionResult ProxyStaticApp(string backendUri, string fallbackExclude
return new ProxyStaticAppResult(backendUri) { FallbackExclude = fallbackExclude };
}

protected IActionResult LocalStaticApp(string virtualPath, string defaultFile = "index.html", string fallbackPath = "404.html", string fallbackExclude = null)
protected IActionResult LocalStaticApp(string defaultFile = "index.html", string fallbackPath = "404.html", string fallbackExclude = null)
{
return new LocalStaticAppResult(virtualPath) { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
return new LocalStaticAppResult { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ namespace Azure.WebJobs.Extensions.HttpApi.Proxy
{
internal class LocalStaticAppResult : IActionResult
{
public LocalStaticAppResult(string virtualPath)
{
VirtualPath = virtualPath;
}

public string VirtualPath { get; set; }

public string DefaultFile { get; set; }

public string FallbackPath { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand All @@ -22,7 +23,9 @@ internal class LocalStaticAppResultExecutor : IActionResultExecutor<LocalStaticA

public async Task ExecuteAsync(ActionContext context, LocalStaticAppResult result)
{
var virtualPath = result.VirtualPath ?? "/";
var (_, value) = context.RouteData.Values.Single();

var virtualPath = $"/{value?.ToString()}";

var contents = _fileProvider.GetDirectoryContents(virtualPath);

Expand Down
18 changes: 10 additions & 8 deletions src/Azure.WebJobs.Extensions.HttpApi/Proxy/ProxyResultExecutor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Net;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down Expand Up @@ -30,15 +31,16 @@ private static string MakeBackendUri(string backendUri, ActionContext context)
{
var routeValues = context.RouteData.Values;

return _templateRegex.Replace(backendUri, match =>
var generatedBackendUri = _templateRegex.Replace(backendUri, m => routeValues[m.Groups[1].Value]?.ToString() ?? "");

if (generatedBackendUri == backendUri)
{
if (routeValues.TryGetValue(match.Groups[1].Value, out var value) && value != null)
{
return value.ToString();
}
var (_, value) = routeValues.Single();

generatedBackendUri += generatedBackendUri.EndsWith("/") ? value : $"/{value}";
}

return "";
});
return generatedBackendUri;
}
}
}