Skip to content

Commit

Permalink
Add template for Single Page Applications
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Apr 26, 2024
1 parent 17b5e2b commit 694fa48
Show file tree
Hide file tree
Showing 11 changed files with 249 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ To use one of the templates below, create a new folder and run `dotnet new <temp
| `genhttp-website-mvc-razor` | Dynamic website using the [MVC pattern](https://genhttp.org/documentation/content/controllers) and [Razor](https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-5.0) as a templating engine. |
| `genhttp-website-mvc-scriban` | Dynamic website using the [MVC pattern](https://genhttp.org/documentation/content/controllers) and [Scriban](https://github.com/scriban/scriban/) as a templating engine. |
| `genhttp-website-static` | Project to serve a [static website](https://genhttp.org/documentation/content/static-websites) from the file system. |
| `genhttp-spa` | Project to serve the distribution files of a [Single Page Application](https://genhttp.org/documentation/content/single-page-applications). |

## Template Development

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

<PropertyGroup>

<TargetFramework>net8.0</TargetFramework>

<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<IsPackable>false</IsPackable>

</PropertyGroup>

<ItemGroup>

<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />

<PackageReference Include="MSTest.TestAdapter" Version="3.1.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.1.1" />

<PackageReference Include="GenHTTP.Testing" Version="8.3.0" />

<ProjectReference Include="..\$safeprojectname$\$safeprojectname$.csproj" />

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Net;
using System.Threading.Tasks;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using GenHTTP.Testing;

namespace $safeprojectname$.Tests
{

[TestClass]
public class ApplicationTests
{

[TestMethod]
public async Task TestRoot()
{
using var runner = TestHost.Run(Project.Setup());

using var response = await runner.GetResponseAsync();

Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>

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

<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.3.1" />

<PackageReference Include="GenHTTP.Modules.SinglePageApplications" Version="8.3.0" />

</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions Templates/SinglePageApplication/$safeprojectname$/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using GenHTTP.Engine;

using GenHTTP.Modules.Practices;

using $safeprojectname$;

var project = Project.Setup();

// hosts your app on http://localhost:8080
return Host.Create()
.Handler(project)
.Defaults()
.Console()
//-:cnd:noEmit
#if DEBUG
.Development()
#endif
//+:cnd:noEmit
.Run();
46 changes: 46 additions & 0 deletions Templates/SinglePageApplication/$safeprojectname$/Project.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using GenHTTP.Api.Content;

using GenHTTP.Modules.IO;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.SinglePageApplications;

namespace $safeprojectname$
{

public static class Project
{

public static IHandlerBuilder Setup()
{
//
// depending on your needs, update this section to define where your
// single page application distribution files are loaded from
//
// if you would like to productively host your SPA somewhere, you might want to ...
//
// - use a debug directive to load another directory locally as in production
// - use an environment variable to pass the path to the application
// - ...
//
var distRoot = ResourceTree.FromDirectory(@"C:\Projects\some-app\dist\some-app\browser\");

var spa = SinglePageApplication.From(distRoot)
.ServerSideRouting();

//
// if you would like to host some API for your app, you can create
// services and add them to your app's layout here (e.g. via AddService<T>)
//
// for this purpose, there are various frameworks and nuget packages available:
//
// - web services (https://genhttp.org/documentation/content/webservices)
// - functional handlers (https://genhttp.org/documentation/content/functional)
// - controllers (https://genhttp.org/documentation/content/controllers)
//
return Layout.Create()
.Add(spa);
}

}

}
14 changes: 14 additions & 0 deletions Templates/SinglePageApplication/.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", "SPA", "Single Page Application" ],
"identity": "GenHTTP.Templates.SinglePageApplication",
"name": "GenHTTP: Single Page Application (SPA)",
"shortName": "genhttp-spa",
"sourceName": "$safeprojectname$",
"description": "A project template to host a Single Page Application using the GenHTTP webserver",
"tags": {
"language": "C#",
"type": "project"
}
}
19 changes: 19 additions & 0 deletions Templates/SinglePageApplication/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/SinglePageApplication/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/SinglePageApplication/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/SinglePageApplication/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 694fa48

Please sign in to comment.