|
1 | 1 | using System.Text; |
2 | 2 | using EffinitiveFramework.Core; |
3 | 3 | using EffinitiveFramework.Core.Http; |
| 4 | +using EffinitiveServer.Endpoints; |
4 | 5 |
|
5 | 6 | var port = args.Length > 0 && int.TryParse(args[0], out var p) ? p : 8080; |
6 | 7 |
|
|
13 | 14 | Console.WriteLine($"Effinitive listening on http://localhost:{port}"); |
14 | 15 | await app.RunAsync(); |
15 | 16 |
|
16 | | -// ── GET / ────────────────────────────────────────────────────── |
17 | | - |
18 | | -sealed class GetRoot : NoRequestEndpointBase<string> |
| 17 | +namespace EffinitiveServer.Endpoints |
19 | 18 | { |
20 | | - protected override string Method => "GET"; |
21 | | - protected override string Route => "/"; |
22 | | - protected override string ContentType => "text/plain"; |
| 19 | + // ── GET / ────────────────────────────────────────────────────── |
23 | 20 |
|
24 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
25 | | - => ValueTask.FromResult("OK"); |
26 | | -} |
| 21 | + sealed class GetRoot : NoRequestEndpointBase<string> |
| 22 | + { |
| 23 | + protected override string Method => "GET"; |
| 24 | + protected override string Route => "/"; |
| 25 | + protected override string ContentType => Helpers.TextPlain; |
27 | 26 |
|
28 | | -// ── POST / ───────────────────────────────────────────────────── |
| 27 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 28 | + => ValueTask.FromResult("OK"); |
| 29 | + } |
29 | 30 |
|
30 | | -sealed class PostRoot : NoRequestEndpointBase<string> |
31 | | -{ |
32 | | - protected override string Method => "POST"; |
33 | | - protected override string Route => "/"; |
34 | | - protected override string ContentType => "text/plain"; |
| 31 | + // ── POST / ───────────────────────────────────────────────────── |
35 | 32 |
|
36 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 33 | + sealed class PostRoot : NoRequestEndpointBase<string> |
37 | 34 | { |
38 | | - var body = HttpContext?.Body; |
39 | | - return ValueTask.FromResult(body is { Length: > 0 } ? Encoding.UTF8.GetString(body) : ""); |
| 35 | + protected override string Method => "POST"; |
| 36 | + protected override string Route => "/"; |
| 37 | + protected override string ContentType => Helpers.TextPlain; |
| 38 | + |
| 39 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 40 | + { |
| 41 | + var body = HttpContext?.Body; |
| 42 | + return ValueTask.FromResult(body is { Length: > 0 } ? Encoding.UTF8.GetString(body) : ""); |
| 43 | + } |
40 | 44 | } |
41 | | -} |
42 | 45 |
|
43 | | -// ── GET/POST /echo ──────────────────────────────────────────── |
| 46 | + // ── GET/POST /echo ──────────────────────────────────────────── |
44 | 47 |
|
45 | | -sealed class EchoGet : NoRequestEndpointBase<string> |
46 | | -{ |
47 | | - protected override string Method => "GET"; |
48 | | - protected override string Route => "/echo"; |
49 | | - protected override string ContentType => "text/plain"; |
50 | | - |
51 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
52 | | - => ValueTask.FromResult(Helpers.EchoHeaders(HttpContext)); |
53 | | -} |
54 | | - |
55 | | -sealed class EchoPost : NoRequestEndpointBase<string> |
56 | | -{ |
57 | | - protected override string Method => "POST"; |
58 | | - protected override string Route => "/echo"; |
59 | | - protected override string ContentType => "text/plain"; |
60 | | - |
61 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
62 | | - => ValueTask.FromResult(Helpers.EchoHeaders(HttpContext)); |
63 | | -} |
| 48 | + sealed class EchoGet : NoRequestEndpointBase<string> |
| 49 | + { |
| 50 | + protected override string Method => "GET"; |
| 51 | + protected override string Route => "/echo"; |
| 52 | + protected override string ContentType => Helpers.TextPlain; |
64 | 53 |
|
65 | | -// ── GET/POST /cookie ────────────────────────────────────────── |
| 54 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 55 | + => ValueTask.FromResult(Helpers.EchoHeaders(HttpContext)); |
| 56 | + } |
66 | 57 |
|
67 | | -sealed class CookieGet : NoRequestEndpointBase<string> |
68 | | -{ |
69 | | - protected override string Method => "GET"; |
70 | | - protected override string Route => "/cookie"; |
71 | | - protected override string ContentType => "text/plain"; |
| 58 | + sealed class EchoPost : NoRequestEndpointBase<string> |
| 59 | + { |
| 60 | + protected override string Method => "POST"; |
| 61 | + protected override string Route => "/echo"; |
| 62 | + protected override string ContentType => Helpers.TextPlain; |
72 | 63 |
|
73 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
74 | | - => ValueTask.FromResult(Helpers.ParseCookies(HttpContext)); |
75 | | -} |
| 64 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 65 | + => ValueTask.FromResult(Helpers.EchoHeaders(HttpContext)); |
| 66 | + } |
76 | 67 |
|
77 | | -sealed class CookiePost : NoRequestEndpointBase<string> |
78 | | -{ |
79 | | - protected override string Method => "POST"; |
80 | | - protected override string Route => "/cookie"; |
81 | | - protected override string ContentType => "text/plain"; |
| 68 | + // ── GET/POST /cookie ────────────────────────────────────────── |
82 | 69 |
|
83 | | - public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
84 | | - => ValueTask.FromResult(Helpers.ParseCookies(HttpContext)); |
85 | | -} |
| 70 | + sealed class CookieGet : NoRequestEndpointBase<string> |
| 71 | + { |
| 72 | + protected override string Method => "GET"; |
| 73 | + protected override string Route => "/cookie"; |
| 74 | + protected override string ContentType => Helpers.TextPlain; |
86 | 75 |
|
87 | | -// ── Shared helpers ──────────────────────────────────────────── |
| 76 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 77 | + => ValueTask.FromResult(Helpers.ParseCookies(HttpContext)); |
| 78 | + } |
88 | 79 |
|
89 | | -static class Helpers |
90 | | -{ |
91 | | - public static string EchoHeaders(HttpRequest? ctx) |
| 80 | + sealed class CookiePost : NoRequestEndpointBase<string> |
92 | 81 | { |
93 | | - if (ctx?.Headers is null) return ""; |
94 | | - var sb = new StringBuilder(); |
95 | | - foreach (var h in ctx.Headers) |
96 | | - sb.Append(h.Key).Append(": ").Append(h.Value).Append("\r\n"); |
97 | | - return sb.ToString(); |
| 82 | + protected override string Method => "POST"; |
| 83 | + protected override string Route => "/cookie"; |
| 84 | + protected override string ContentType => Helpers.TextPlain; |
| 85 | + |
| 86 | + public override ValueTask<string> HandleAsync(CancellationToken ct = default) |
| 87 | + => ValueTask.FromResult(Helpers.ParseCookies(HttpContext)); |
98 | 88 | } |
99 | 89 |
|
100 | | - public static string ParseCookies(HttpRequest? ctx) |
| 90 | + // ── Shared helpers ──────────────────────────────────────────── |
| 91 | + |
| 92 | + static class Helpers |
101 | 93 | { |
102 | | - if (ctx is null) return ""; |
103 | | - var sb = new StringBuilder(); |
104 | | - foreach (var c in ctx.Cookies) |
105 | | - sb.Append(c.Key).Append('=').Append(c.Value).Append("\r\n"); |
106 | | - return sb.ToString(); |
| 94 | + public const string TextPlain = "text/plain"; |
| 95 | + |
| 96 | + public static string EchoHeaders(HttpRequest? ctx) |
| 97 | + { |
| 98 | + if (ctx?.Headers is null) return ""; |
| 99 | + var sb = new StringBuilder(); |
| 100 | + foreach (var h in ctx.Headers) |
| 101 | + sb.Append(h.Key).Append(": ").Append(h.Value).Append("\r\n"); |
| 102 | + return sb.ToString(); |
| 103 | + } |
| 104 | + |
| 105 | + public static string ParseCookies(HttpRequest? ctx) |
| 106 | + { |
| 107 | + if (ctx is null) return ""; |
| 108 | + var sb = new StringBuilder(); |
| 109 | + foreach (var c in ctx.Cookies) |
| 110 | + sb.Append(c.Key).Append('=').Append(c.Value).Append("\r\n"); |
| 111 | + return sb.ToString(); |
| 112 | + } |
107 | 113 | } |
108 | 114 | } |
0 commit comments