Skip to content

Commit 2ff64ca

Browse files
authored
Support for SPA / SSG hosting (#20)
* Support for Single Page App hosting * Update README.md * Improvement proxy invoker * Rename to HttpForwarder class * Update Azure Functions SDK version * Update README.md * Adding serve local SPA / SSG application * Update sample code * Refactoring `ProxyResult` implementation * Adding draft `StaticFileResult` impl * Adding local.settings.json * Refine class name and structure * Update README.md
1 parent 60e9c5a commit 2ff64ca

34 files changed

+751
-92
lines changed

README.md

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
# HTTP API Extensions for Azure Functions
22

33
[![Build](https://github.com/shibayan/azure-functions-http-api/workflows/Build/badge.svg)](https://github.com/shibayan/azure-functions-http-api/actions/workflows/build.yml)
4-
[![Downloads](https://img.shields.io/nuget/dt/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
5-
[![NuGet](https://img.shields.io/nuget/v/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
6-
[![License](https://img.shields.io/github/license/shibayan/azure-functions-http-api)](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)
4+
[![Downloads](https://badgen.net/nuget/dt/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
5+
[![NuGet](https://badgen.net/nuget/v/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
6+
[![License](https://badgen.net/github/license/shibayan/azure-functions-http-api)](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)
77

88
## Features
99

10+
- Better route precedence
1011
- Model validation
1112
- ASP.NET Core like helpers
1213
- Support URL generation
1314
- Handle static files
15+
- Simple reverse proxy
16+
- Streamlined SPA / SSG hosting
1417

15-
## Basic usage
18+
## Installation
19+
20+
## Examples
21+
22+
```
23+
Install-Package WebJobs.Extensions.HttpApi
24+
```
25+
26+
```
27+
dotnet add package WebJobs.Extensions.HttpApi
28+
```
29+
30+
```csharp
31+
// Inherits from `HttpFunctionBase` class
32+
public class Function1 : HttpFunctionBase
33+
{
34+
public Function1(IHttpContextAccessor httpContextAccessor)
35+
: base(httpContextAccessor)
36+
{
37+
}
38+
39+
[FunctionName("Function1")]
40+
public IActionResult Run(
41+
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
42+
ILogger log)
43+
{
44+
return Ok($"Hello, {req.Query["name"]}");
45+
}
46+
}
47+
```
1648

1749
### Model validation
1850

@@ -116,6 +148,50 @@ public class Function1 : HttpFunctionBase
116148
}
117149
```
118150

151+
### Simple reverse proxy
152+
153+
```csharp
154+
public class Function1 : HttpFunctionBase
155+
{
156+
public Function1(IHttpContextAccessor httpContextAccessor)
157+
: base(httpContextAccessor)
158+
{
159+
}
160+
161+
[FunctionName("Function1")]
162+
public IActionResult Run(
163+
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "{*path}"})] HttpRequest req,
164+
ILogger log)
165+
{
166+
return Proxy("https://example.com/{path}");
167+
}
168+
}
169+
```
170+
171+
### Streamlined SPA / SSG hosting
172+
173+
```csharp
174+
public class Function1 : HttpFunctionBase
175+
{
176+
public Function1(IHttpContextAccessor httpContextAccessor)
177+
: base(httpContextAccessor)
178+
{
179+
}
180+
181+
[FunctionName("Function1")]
182+
public IActionResult Run(
183+
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "{*path}"})] HttpRequest req,
184+
ILogger log)
185+
{
186+
#if USE_PROXY
187+
return ProxyStaticApp("https://example.com/{path}", fallbackExclude: $"^/_nuxt/.*");
188+
#else
189+
return LocalStaticApp($"{path}", fallbackExclude: $"^/_nuxt/.*");
190+
#endif
191+
}
192+
}
193+
```
194+
119195
## License
120196

121197
This project is licensed under the [MIT License](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)

samples/BasicSample/BasicSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
7+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
88
</ItemGroup>
99
<ItemGroup>
1010
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
6+
}
7+
}

samples/ProxySample/ProxySample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
7+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
88
</ItemGroup>
99
<ItemGroup>
1010
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />

samples/ProxySample/ReverseProxy.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Azure.WebJobs.Extensions.HttpApi;
2+
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
using Microsoft.Azure.WebJobs;
6+
using Microsoft.Azure.WebJobs.Extensions.Http;
7+
8+
namespace ProxySample
9+
{
10+
public class ReverseProxy : HttpFunctionBase
11+
{
12+
public ReverseProxy(IHttpContextAccessor httpContextAccessor)
13+
: base(httpContextAccessor)
14+
{
15+
}
16+
17+
[FunctionName(nameof(ReverseProxy))]
18+
public IActionResult Run(
19+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
20+
HttpRequest req)
21+
{
22+
return Proxy("https://shibayan.jp/{path}");
23+
}
24+
}
25+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"IsEncrypted": false,
3+
"Values": {
4+
"AzureWebJobsStorage": "",
5+
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
6+
}
7+
}

samples/VirtualPathSample/VirtualPathSample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
55
</PropertyGroup>
66
<ItemGroup>
7-
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
7+
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
88
</ItemGroup>
99
<ItemGroup>
1010
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />

0 commit comments

Comments
 (0)