Layouting

Live on this server

Routes requests to the handlers an application is made of.

Most applications are made of several parts, such as an API, static files and a website. A layout routes every request to the part it belongs to by its path: here, /assets/ serves files and /api/ a small API.

Layouts can be nested to build deeper structures, and what you add to a layout - such as authentication or CORS - applies to everything below it. Put some files into a files folder next to the program to have something to serve.

To run it, create a console app (dotnet new console), add GenHTTP (dotnet add package GenHTTP.Full), replace Program.cs with the code below and start it with dotnet run - it answers at http://localhost:8080/.

Program.cs21 lines
using GenHTTP.Engine.Internal;

using GenHTTP.Modules.Files;
using GenHTTP.Modules.Functional;
using GenHTTP.Modules.IO;
using GenHTTP.Modules.Layouting;
using GenHTTP.Modules.Practices;

var assets = Assets.From(ResourceTree.FromDirectory("./files"));

var api = Inline.Create()
                .Get(() => "Hello World!");

var app = Layout.Create()
                .Add("assets", assets) // GET /assets/README.md
                .Add("api", api); // GET /api/

await Host.Create()
          .Handler(app)
          .Defaults()
          .RunAsync();

Try it

Send the requests to the sample running on this server - change the path, headers or body as you like, or copy them as curl commands.

  1. Call the API

    GET
    curl -i 'https://genhttp.org/demo/layouting/api/'
  2. Fetch a file

    GET
    curl -i 'https://genhttp.org/demo/layouting/assets/README.md'