|
| 1 | +--- |
| 2 | +title: "FastEndpoints" |
| 3 | +toc: false |
| 4 | +breadcrumbs: false |
| 5 | +--- |
| 6 | + |
| 7 | +**Language:** C# · [View source on GitHub](https://github.com/MDA2AV/Http11Probe/tree/main/src/Servers/FastEndpointsServer) |
| 8 | + |
| 9 | +## Dockerfile |
| 10 | + |
| 11 | +```dockerfile |
| 12 | +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build |
| 13 | +WORKDIR /src |
| 14 | +COPY Directory.Build.props . |
| 15 | +COPY src/Servers/FastEndpointsServer/ src/Servers/FastEndpointsServer/ |
| 16 | +RUN dotnet restore src/Servers/FastEndpointsServer/FastEndpointsServer.csproj |
| 17 | +RUN dotnet publish src/Servers/FastEndpointsServer/FastEndpointsServer.csproj -c Release -o /app --no-restore |
| 18 | + |
| 19 | +FROM mcr.microsoft.com/dotnet/aspnet:10.0 |
| 20 | +WORKDIR /app |
| 21 | +COPY --from=build /app . |
| 22 | +ENTRYPOINT ["dotnet", "FastEndpointsServer.dll"] |
| 23 | +``` |
| 24 | + |
| 25 | +## Source — `Program.cs` |
| 26 | + |
| 27 | +```csharp |
| 28 | +using FastEndpoints; |
| 29 | + |
| 30 | +var builder = WebApplication.CreateBuilder(args); |
| 31 | + |
| 32 | +builder.WebHost.UseUrls("http://+:8080"); |
| 33 | +builder.Services.AddFastEndpoints(o => o.Assemblies = [typeof(GetRoot).Assembly]); |
| 34 | + |
| 35 | +var app = builder.Build(); |
| 36 | + |
| 37 | +app.UseFastEndpoints(); |
| 38 | + |
| 39 | +app.Run(); |
| 40 | + |
| 41 | +// ── GET / ────────────────────────────────────────────────────── |
| 42 | +
|
| 43 | +sealed class GetRoot : EndpointWithoutRequest |
| 44 | +{ |
| 45 | + public override void Configure() |
| 46 | + { |
| 47 | + Get("/"); |
| 48 | + AllowAnonymous(); |
| 49 | + } |
| 50 | + |
| 51 | + public override async Task HandleAsync(CancellationToken ct) |
| 52 | + { |
| 53 | + await HttpContext.Response.WriteAsync("OK", ct); |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// ── HEAD / ───────────────────────────────────────────────────── |
| 58 | +
|
| 59 | +sealed class HeadRoot : EndpointWithoutRequest |
| 60 | +{ |
| 61 | + public override void Configure() |
| 62 | + { |
| 63 | + Verbs("HEAD"); |
| 64 | + Routes("/"); |
| 65 | + AllowAnonymous(); |
| 66 | + } |
| 67 | + |
| 68 | + public override async Task HandleAsync(CancellationToken ct) |
| 69 | + { |
| 70 | + HttpContext.Response.StatusCode = 200; |
| 71 | + await HttpContext.Response.WriteAsync("", ct); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// ── POST / ───────────────────────────────────────────────────── |
| 76 | +
|
| 77 | +sealed class PostRoot : EndpointWithoutRequest |
| 78 | +{ |
| 79 | + public override void Configure() |
| 80 | + { |
| 81 | + Post("/"); |
| 82 | + AllowAnonymous(); |
| 83 | + } |
| 84 | + |
| 85 | + public override async Task HandleAsync(CancellationToken ct) |
| 86 | + { |
| 87 | + using var reader = new StreamReader(HttpContext.Request.Body); |
| 88 | + var body = await reader.ReadToEndAsync(ct); |
| 89 | + await HttpContext.Response.WriteAsync(body, ct); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// ── OPTIONS / ────────────────────────────────────────────────── |
| 94 | +
|
| 95 | +sealed class OptionsRoot : EndpointWithoutRequest |
| 96 | +{ |
| 97 | + public override void Configure() |
| 98 | + { |
| 99 | + Verbs("OPTIONS"); |
| 100 | + Routes("/"); |
| 101 | + AllowAnonymous(); |
| 102 | + } |
| 103 | + |
| 104 | + public override async Task HandleAsync(CancellationToken ct) |
| 105 | + { |
| 106 | + HttpContext.Response.Headers["Allow"] = "GET, HEAD, POST, OPTIONS"; |
| 107 | + HttpContext.Response.StatusCode = 200; |
| 108 | + await HttpContext.Response.WriteAsync("", ct); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// ── POST /echo ───────────────────────────────────────────────── |
| 113 | +
|
| 114 | +sealed class PostEcho : EndpointWithoutRequest |
| 115 | +{ |
| 116 | + public override void Configure() |
| 117 | + { |
| 118 | + Post("/echo"); |
| 119 | + AllowAnonymous(); |
| 120 | + } |
| 121 | + |
| 122 | + public override async Task HandleAsync(CancellationToken ct) |
| 123 | + { |
| 124 | + var sb = new System.Text.StringBuilder(); |
| 125 | + foreach (var h in HttpContext.Request.Headers) |
| 126 | + foreach (var v in h.Value) |
| 127 | + sb.AppendLine($"{h.Key}: {v}"); |
| 128 | + await HttpContext.Response.WriteAsync(sb.ToString(), ct); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## Source — `FastEndpointsServer.csproj` |
| 134 | + |
| 135 | +```xml |
| 136 | +<Project Sdk="Microsoft.NET.Sdk.Web"> |
| 137 | + |
| 138 | + <PropertyGroup> |
| 139 | + <TargetFramework>net10.0</TargetFramework> |
| 140 | + <Nullable>enable</Nullable> |
| 141 | + <ImplicitUsings>enable</ImplicitUsings> |
| 142 | + <IsPackable>false</IsPackable> |
| 143 | + </PropertyGroup> |
| 144 | + |
| 145 | + <ItemGroup> |
| 146 | + <PackageReference Include="FastEndpoints" Version="7.2.0" /> |
| 147 | + </ItemGroup> |
| 148 | + |
| 149 | +</Project> |
| 150 | +``` |
0 commit comments