Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1 from goncalo-oliveira/develop
Browse files Browse the repository at this point in the history
Added aspnet-controller template
  • Loading branch information
goncalo-oliveira authored Feb 3, 2021
2 parents 518a15f + 9e202b2 commit bc07896
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 4 deletions.
53 changes: 53 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,59 @@ namespace OpenFaaS
}
```

## Controller-based template

Sometimes we want to create a more complex workload, with multiple methods or even with multiple routes. For these scenarios, we wan us the `aspnet-controller` template. When we create a new function with this template, instead of having a `Function.cs` we now have a `Controller.cs` file.

```csharp
namespace OpenFaaS
{
[ApiController]
[Route("/")]
public class Controller : ControllerBase
{
[HttpGet]
public Task<IActionResult> GetAsync()
{
var result = new
{
Message = "Hello!"
};

return Task.FromResult<IActionResult>( Ok( result ) );
}
}
}
```

This controller class is exactly the same as we would have when creating a Web API project with ASPNET. We also still have access to a `Startup.cs` file. Additionally, we have access to the HTTP request pipeline configuration.

```csharp
namespace OpenFaaS
{
public class Startup
{
public Startup( IConfiguration configuration )
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices( IServiceCollection services )
{
// add your services here.
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, bool isDevelopmentEnv )
{
}
}
}
```

## Debugging and running locally

It is possible to run a function locally with [FaaS Runner](https://github.com/goncalo-oliveira/faas-run). This also adds the option to attach to the process when running, to be able to debug the function. A configuration file can be passed to the runner. The CLI takes the assembly pat as argument.
Expand Down
51 changes: 51 additions & 0 deletions template/aspnet-controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM openfaas/of-watchdog:0.8.1 as watchdog

FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim as builder
ARG PACKAGE_SOURCE
ARG PACKAGE_SOURCE_USERNAME
ARG PACKAGE_SOURCE_PASSWORD

# suppress data collection
ENV DOTNET_CLI_TELEMETRY_OPTOUT 1

# caches restore result by copying csproj file separately
WORKDIR /source/faas/function
COPY function/function.csproj .

# restore packages
COPY add-package-source.sh /tmp/
RUN chmod u+x /tmp/add-package-source.sh \
&& /tmp/add-package-source.sh $PACKAGE_SOURCE $PACKAGE_SOURCE_USERNAME $PACKAGE_SOURCE_PASSWORD \
&& dotnet restore

# Copies the rest of the code
WORKDIR /source/faas
COPY . .

# build and publish
RUN dotnet publish -c release -o published function/function.csproj

# runner
FROM docker.pkg.github.com/goncalo-oliveira/faas-run/faas-run:1.6

COPY --from=watchdog /fwatchdog /usr/bin/

# Create a non-root user
RUN addgroup --system app \
&& adduser --system --ingroup app app

WORKDIR /home/app/
COPY --from=builder /source/faas/published .
RUN chown app:app -R /home/app

USER app

ENV fprocess="faas-run ./function.dll"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://localhost:9000"
EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
11 changes: 11 additions & 0 deletions template/aspnet-controller/add-package-source.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# $1: package source
# $2: username
# $3: password

if [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; then
echo "--source $1 --username $2 --password $3"
# dotnet nuget add source
dotnet nuget add source -n private -u $2 -p $3 --store-password-in-clear-text $1
fi
22 changes: 22 additions & 0 deletions template/aspnet-controller/function/Controller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace OpenFaaS
{
[ApiController]
[Route("/")]
public class Controller : ControllerBase
{
[HttpGet]
public Task<IActionResult> GetAsync()
{
var result = new
{
Message = "Hello!"
};

return Task.FromResult<IActionResult>( Ok( result ) );
}
}
}
28 changes: 28 additions & 0 deletions template/aspnet-controller/function/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace OpenFaaS
{
public class Startup
{
public Startup( IConfiguration configuration )
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices( IServiceCollection services )
{
// add your services here.
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure( IApplicationBuilder app, bool isDevelopmentEnv )
{
}
}
}
16 changes: 16 additions & 0 deletions template/aspnet-controller/function/function.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>OpenFaaS</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="OpenFaaS.Functions" Version="1.3.0" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions template/aspnet-controller/template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
language: aspnet-controller
fprocess: faas-run ./function.dll

4 changes: 2 additions & 2 deletions template/aspnet-fsharp/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM openfaas/of-watchdog:0.8.1 as watchdog

FROM mcr.microsoft.com/dotnet/sdk:5.0 as builder
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim as builder
ARG PACKAGE_SOURCE
ARG PACKAGE_SOURCE_USERNAME
ARG PACKAGE_SOURCE_PASSWORD
Expand All @@ -26,7 +26,7 @@ COPY . .
RUN dotnet publish -c release -o published function/function.fsproj

# runner
FROM docker.pkg.github.com/goncalo-oliveira/faas-run/faas-run:1.5
FROM docker.pkg.github.com/goncalo-oliveira/faas-run/faas-run:1.6

COPY --from=watchdog /fwatchdog /usr/bin/

Expand Down
4 changes: 2 additions & 2 deletions template/aspnet/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM openfaas/of-watchdog:0.8.1 as watchdog

FROM mcr.microsoft.com/dotnet/sdk:5.0 as builder
FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim as builder
ARG PACKAGE_SOURCE
ARG PACKAGE_SOURCE_USERNAME
ARG PACKAGE_SOURCE_PASSWORD
Expand All @@ -26,7 +26,7 @@ COPY . .
RUN dotnet publish -c release -o published function/function.csproj

# runner
FROM docker.pkg.github.com/goncalo-oliveira/faas-run/faas-run:1.5
FROM docker.pkg.github.com/goncalo-oliveira/faas-run/faas-run:1.6

COPY --from=watchdog /fwatchdog /usr/bin/

Expand Down

0 comments on commit bc07896

Please sign in to comment.