Skip to content

Support for SPA / SSG hosting #20

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 14 commits 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
84 changes: 80 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,50 @@
# HTTP API Extensions for Azure Functions

[![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)
[![Downloads](https://img.shields.io/nuget/dt/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
[![NuGet](https://img.shields.io/nuget/v/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
[![License](https://img.shields.io/github/license/shibayan/azure-functions-http-api)](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)
[![Downloads](https://badgen.net/nuget/dt/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
[![NuGet](https://badgen.net/nuget/v/WebJobs.Extensions.HttpApi)](https://www.nuget.org/packages/WebJobs.Extensions.HttpApi/)
[![License](https://badgen.net/github/license/shibayan/azure-functions-http-api)](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)

## Features

- Better route precedence
- Model validation
- ASP.NET Core like helpers
- Support URL generation
- Handle static files
- Simple reverse proxy
- Streamlined SPA / SSG hosting

## Basic usage
## Installation

## Examples

```
Install-Package WebJobs.Extensions.HttpApi
```

```
dotnet add package WebJobs.Extensions.HttpApi
```

```csharp
// Inherits from `HttpFunctionBase` class
public class Function1 : HttpFunctionBase
{
public Function1(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName("Function1")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get")] HttpRequest req,
ILogger log)
{
return Ok($"Hello, {req.Query["name"]}");
}
}
```

### Model validation

Expand Down Expand Up @@ -116,6 +148,50 @@ public class Function1 : HttpFunctionBase
}
```

### Simple reverse proxy

```csharp
public class Function1 : HttpFunctionBase
{
public Function1(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName("Function1")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "{*path}"})] HttpRequest req,
ILogger log)
{
return Proxy("https://example.com/{path}");
}
}
```

### Streamlined SPA / SSG hosting

```csharp
public class Function1 : HttpFunctionBase
{
public Function1(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName("Function1")]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = "{*path}"})] HttpRequest req,
ILogger log)
{
#if USE_PROXY
return ProxyStaticApp("https://example.com/{path}", fallbackExclude: $"^/_nuxt/.*");
#else
return LocalStaticApp($"{path}", fallbackExclude: $"^/_nuxt/.*");
#endif
}
}
```

## License

This project is licensed under the [MIT License](https://github.com/shibayan/azure-functions-http-api/blob/master/LICENSE)
2 changes: 1 addition & 1 deletion samples/BasicSample/BasicSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />
Expand Down
7 changes: 7 additions & 0 deletions samples/BasicSample/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
2 changes: 1 addition & 1 deletion samples/ProxySample/ProxySample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />
Expand Down
25 changes: 25 additions & 0 deletions samples/ProxySample/ReverseProxy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Azure.WebJobs.Extensions.HttpApi;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;

namespace ProxySample
{
public class ReverseProxy : HttpFunctionBase
{
public ReverseProxy(IHttpContextAccessor httpContextAccessor)
: base(httpContextAccessor)
{
}

[FunctionName(nameof(ReverseProxy))]
public IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", "put", "delete", "options", "head", "patch", Route = "{*path}")]
HttpRequest req)
{
return Proxy("https://shibayan.jp/{path}");
}
}
}
7 changes: 7 additions & 0 deletions samples/ProxySample/local.settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
2 changes: 1 addition & 1 deletion samples/VirtualPathSample/VirtualPathSample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<AzureFunctionsVersion>v4</AzureFunctionsVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Azure.WebJobs.Extensions.HttpApi\Azure.WebJobs.Extensions.HttpApi.csproj" />
Expand Down
Loading