Skip to content

Commit e6593fb

Browse files
authored
Automatically complete catch-all route parameter (#23)
1 parent 2ff64ca commit e6593fb

File tree

7 files changed

+22
-25
lines changed

7 files changed

+22
-25
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,9 @@ public class Function1 : HttpFunctionBase
184184
ILogger log)
185185
{
186186
#if USE_PROXY
187-
return ProxyStaticApp("https://example.com/{path}", fallbackExclude: $"^/_nuxt/.*");
187+
return ProxyStaticApp("https://example.com", fallbackExclude: $"^/_nuxt/.*");
188188
#else
189-
return LocalStaticApp($"{path}", fallbackExclude: $"^/_nuxt/.*");
189+
return LocalStaticApp(fallbackPath: "404.html", fallbackExclude: $"^/_nuxt/.*");
190190
#endif
191191
}
192192
}

samples/ProxySample/ReverseProxy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public IActionResult Run(
1919
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
2020
HttpRequest req)
2121
{
22-
return Proxy("https://shibayan.jp/{path}");
22+
return Proxy("https://shibayan.jp");
2323
}
2424
}
2525
}

samples/WebsiteSample/StaticWebsite.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ public StaticWebsite(IHttpContextAccessor httpContextAccessor)
1717
[FunctionName(nameof(StaticWebsite))]
1818
public IActionResult Run(
1919
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
20-
HttpRequest req,
21-
string path)
20+
HttpRequest req)
2221
{
23-
//return ProxyStaticApp("https://ststaticwebsiteproxy.z11.web.core.windows.net/{path}", fallbackExclude: $"^/_nuxt/.*");
22+
//return ProxyStaticApp("https://ststaticwebsiteproxy.z11.web.core.windows.net", fallbackExclude: $"^/_nuxt/.*");
2423

25-
return LocalStaticApp(path, fallbackPath: "200.html", fallbackExclude: $"^/_nuxt/.*");
24+
return LocalStaticApp(fallbackPath: "200.html", fallbackExclude: $"^/_nuxt/.*");
2625
}
2726
}
2827
}

src/Azure.WebJobs.Extensions.HttpApi/HttpFunctionBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,9 @@ protected IActionResult ProxyStaticApp(string backendUri, string fallbackExclude
233233
return new ProxyStaticAppResult(backendUri) { FallbackExclude = fallbackExclude };
234234
}
235235

236-
protected IActionResult LocalStaticApp(string virtualPath, string defaultFile = "index.html", string fallbackPath = "404.html", string fallbackExclude = null)
236+
protected IActionResult LocalStaticApp(string defaultFile = "index.html", string fallbackPath = "404.html", string fallbackExclude = null)
237237
{
238-
return new LocalStaticAppResult(virtualPath) { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
238+
return new LocalStaticAppResult { DefaultFile = defaultFile, FallbackPath = fallbackPath, FallbackExclude = fallbackExclude };
239239
}
240240

241241
#endregion

src/Azure.WebJobs.Extensions.HttpApi/Proxy/LocalStaticAppResult.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ namespace Azure.WebJobs.Extensions.HttpApi.Proxy
77
{
88
internal class LocalStaticAppResult : IActionResult
99
{
10-
public LocalStaticAppResult(string virtualPath)
11-
{
12-
VirtualPath = virtualPath;
13-
}
14-
15-
public string VirtualPath { get; set; }
16-
1710
public string DefaultFile { get; set; }
1811

1912
public string FallbackPath { get; set; }

src/Azure.WebJobs.Extensions.HttpApi/Proxy/LocalStaticAppResultExecutor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Linq;
23
using System.Net;
34
using System.Text.RegularExpressions;
45
using System.Threading.Tasks;
@@ -22,7 +23,9 @@ internal class LocalStaticAppResultExecutor : IActionResultExecutor<LocalStaticA
2223

2324
public async Task ExecuteAsync(ActionContext context, LocalStaticAppResult result)
2425
{
25-
var virtualPath = result.VirtualPath ?? "/";
26+
var (_, value) = context.RouteData.Values.Single();
27+
28+
var virtualPath = $"/{value?.ToString()}";
2629

2730
var contents = _fileProvider.GetDirectoryContents(virtualPath);
2831

src/Azure.WebJobs.Extensions.HttpApi/Proxy/ProxyResultExecutor.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net;
1+
using System.Linq;
2+
using System.Net;
23
using System.Text.RegularExpressions;
34
using System.Threading.Tasks;
45

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

33-
return _templateRegex.Replace(backendUri, match =>
34+
var generatedBackendUri = _templateRegex.Replace(backendUri, m => routeValues[m.Groups[1].Value]?.ToString() ?? "");
35+
36+
if (generatedBackendUri == backendUri)
3437
{
35-
if (routeValues.TryGetValue(match.Groups[1].Value, out var value) && value != null)
36-
{
37-
return value.ToString();
38-
}
38+
var (_, value) = routeValues.Single();
39+
40+
generatedBackendUri += generatedBackendUri.EndsWith("/") ? value : $"/{value}";
41+
}
3942

40-
return "";
41-
});
43+
return generatedBackendUri;
4244
}
4345
}
4446
}

0 commit comments

Comments
 (0)