-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProgram.cs
More file actions
103 lines (83 loc) · 3.02 KB
/
Program.cs
File metadata and controls
103 lines (83 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using FastEndpoints;
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls("http://+:8080");
builder.Services.AddFastEndpoints(o => o.Assemblies = [typeof(GetRoot).Assembly]);
var app = builder.Build();
app.UseFastEndpoints();
app.Run();
// ── GET / ──────────────────────────────────────────────────────
sealed class GetRoot : EndpointWithoutRequest
{
public override void Configure()
{
Get("/");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
await HttpContext.Response.WriteAsync("OK", ct);
}
}
// ── HEAD / ─────────────────────────────────────────────────────
sealed class HeadRoot : EndpointWithoutRequest
{
public override void Configure()
{
Verbs("HEAD");
Routes("/");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
HttpContext.Response.StatusCode = 200;
await HttpContext.Response.WriteAsync("", ct);
}
}
// ── POST / ─────────────────────────────────────────────────────
sealed class PostRoot : EndpointWithoutRequest
{
public override void Configure()
{
Post("/");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
using var reader = new StreamReader(HttpContext.Request.Body);
var body = await reader.ReadToEndAsync(ct);
await HttpContext.Response.WriteAsync(body, ct);
}
}
// ── OPTIONS / ──────────────────────────────────────────────────
sealed class OptionsRoot : EndpointWithoutRequest
{
public override void Configure()
{
Verbs("OPTIONS");
Routes("/");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
HttpContext.Response.Headers["Allow"] = "GET, HEAD, POST, OPTIONS";
HttpContext.Response.StatusCode = 200;
await HttpContext.Response.WriteAsync("", ct);
}
}
// ── POST /echo ─────────────────────────────────────────────────
sealed class PostEcho : EndpointWithoutRequest
{
public override void Configure()
{
Post("/echo");
AllowAnonymous();
}
public override async Task HandleAsync(CancellationToken ct)
{
var sb = new System.Text.StringBuilder();
foreach (var h in HttpContext.Request.Headers)
foreach (var v in h.Value)
sb.AppendLine($"{h.Key}: {v}");
await HttpContext.Response.WriteAsync(sb.ToString(), ct);
}
}