Skip to content

Commit 18a1fb7

Browse files
committed
Add 404 handler, handle GET requests
1 parent 5b8778a commit 18a1fb7

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

Program.cs

+16-4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ static async Task<bool> handleJsonp(HttpContext theContext, object data)
3131

3232
var app = builder.Build();
3333

34+
app.MapFallback(async theContext =>
35+
{
36+
theContext.Response.StatusCode = 404;
37+
38+
var result = new TestOutput(false, "404 Not Found", "");
39+
40+
await handleJsonp(theContext, result);
41+
});
42+
3443
app.UseHttpLogging();
3544

3645
app.UseStaticFiles();
@@ -49,7 +58,10 @@ static async Task<bool> handleJsonp(HttpContext theContext, object data)
4958
await handleJsonp(theContext, statusResult);
5059
});
5160

52-
app.MapPost("/test.json", static async (HttpContext theContext) =>
61+
app.MapPost("/test.json", RunTest);
62+
app.MapGet("/test.json", RunTest);
63+
64+
static async Task RunTest(HttpContext theContext)
5365
{
5466
// read form variables
5567
var form = await theContext.Request.ReadFormAsync();
@@ -59,10 +71,10 @@ static async Task<bool> handleJsonp(HttpContext theContext, object data)
5971

6072
var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}";
6173

62-
var testOutput = new TestOutput(true, html);
74+
var testOutput = new TestOutput(true, "", html);
6375

6476
await handleJsonp(theContext, testOutput);
65-
});
77+
}
6678

6779
var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0";
6880
var port = Environment.GetEnvironmentVariable("PORT") ?? "4000";
@@ -73,5 +85,5 @@ static async Task<bool> handleJsonp(HttpContext theContext, object data)
7385
app.Run(url);
7486

7587
record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit);
76-
record TestOutput(Boolean success, string html);
88+
record TestOutput(Boolean success, string message, string html);
7789

0 commit comments

Comments
 (0)