Skip to content

Commit

Permalink
Add template which uses the functional framework (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat authored Nov 27, 2023
1 parent 0dd9b02 commit 0a1cae2
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 1 deletion.
2 changes: 1 addition & 1 deletion GenHTTP.Templates.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>

<PackageType>Template</PackageType>
<PackageVersion>8.0</PackageVersion>
<PackageVersion>8.1</PackageVersion>
<PackageId>GenHTTP.Templates</PackageId>

<Title>GenHTTP Templates</Title>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>

<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<Version>0.1.0</Version>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="GenHTTP.Core" Version="8.0.0" />

<PackageReference Include="GenHTTP.Modules.Security" Version="8.0.0" />
<PackageReference Include="GenHTTP.Modules.Functional" Version="8.0.0" />

</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions Templates/Webservice-Minimal/$safeprojectname$/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;

using GenHTTP.Engine;

using GenHTTP.Modules.Functional;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Practices;
using GenHTTP.Modules.Security;

// run this project and fetch a list of book records via http://localhost:8080/books/
var books = new List<Book>()
{
new Book(1, "Lord of the Rings")
};

// see https://genhttp.org/documentation/content/functional
var bookApi = Inline.Create()
.Get(() => books)
.Put((Book book) => books.Add(book));

var content = Layout.Create()
.Add("books", bookApi)
.Add(CorsPolicy.Permissive());

return Host.Create()
.Handler(content)
.Defaults()
.Console()
//-:cnd:noEmit
#if DEBUG
.Development()
#endif
//+:cnd:noEmit
.Run();

record class Book(int ID, string Title);
14 changes: 14 additions & 0 deletions Templates/Webservice-Minimal/.template.config/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Andreas Nägeli",
"classifications": [ "GenHTTP", "Webservices", "REST", "Web API" ],
"identity": "GenHTTP.Templates.Webservice.Minimal",
"name": "GenHTTP: Webservice (Minimal)",
"shortName": "genhttp-webservice-minimal",
"sourceName": "$safeprojectname$",
"description": "A project template to host a REST webservice from a single code file using the GenHTTP webserver",
"tags": {
"language": "C#",
"type": "project"
}
}
19 changes: 19 additions & 0 deletions Templates/Webservice-Minimal/Dockerfile.linux-arm32
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY $safeprojectname$/*.csproj .
RUN dotnet restore -r linux-arm

# copy and publish app and libraries
COPY $safeprojectname$/ .
RUN dotnet publish -c release -o /app -r linux-arm --no-restore /p:PublishTrimmed=true /p:TrimMode=Link

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine-arm32v7
WORKDIR /app
COPY --from=build /app .

ENTRYPOINT ["./$safeprojectname$"]

EXPOSE 8080
19 changes: 19 additions & 0 deletions Templates/Webservice-Minimal/Dockerfile.linux-arm64
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY $safeprojectname$/*.csproj .
RUN dotnet restore -r linux-arm64

# copy and publish app and libraries
COPY $safeprojectname$/ .
RUN dotnet publish -c release -o /app -r linux-arm64 --no-restore /p:PublishTrimmed=true /p:TrimMode=Link

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine-arm64v8
WORKDIR /app
COPY --from=build /app .

ENTRYPOINT ["./$safeprojectname$"]

EXPOSE 8080
19 changes: 19 additions & 0 deletions Templates/Webservice-Minimal/Dockerfile.linux-x64
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY $safeprojectname$/*.csproj .
RUN dotnet restore -r linux-musl-x64

# copy and publish app and libraries
COPY $safeprojectname$/ .
RUN dotnet publish -c release -o /app -r linux-musl-x64 --no-restore /p:PublishTrimmed=true /p:TrimMode=Link

# final stage/image
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0-alpine
WORKDIR /app
COPY --from=build /app .

ENTRYPOINT ["./$safeprojectname$"]

EXPOSE 8080
33 changes: 33 additions & 0 deletions Templates/Webservice-Minimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# $safeprojectname$

*Add your project description here.*

## Development

To build this project from source, checkout this repository and execute
the following commands in your terminal. This requires the
[.NET SDK](https://dotnet.microsoft.com/download) to be installed.

```
cd $safeprojectname$
dotnet run
```

This will make the service available at http://localhost:8080/.

## Deployment

To run this project with [Docker](https://www.docker.com/), run the
following commands in your terminal (and adjust the first line
depending on your platform).

```
docker build -f Dockerfile.linux-x64 -t $safeprojectname$ .
docker run -p 8080:8080 $safeprojectname$
```

## About

This project uses the [GenHTTP webserver](https://genhttp.org/) to
implement it's functionality.

0 comments on commit 0a1cae2

Please sign in to comment.