|
| 1 | +using System.Text.Json; |
| 2 | +using Microsoft.AspNetCore.HttpLogging; |
| 3 | + |
| 4 | +var builder = WebApplication.CreateBuilder(args); |
| 5 | +builder.Services.AddHttpLogging(logging => |
| 6 | +{ |
| 7 | + logging.LoggingFields = HttpLoggingFields.All; |
| 8 | + logging.CombineLogs = true; |
| 9 | +}); |
| 10 | + |
| 11 | +var app = builder.Build(); |
| 12 | + |
| 13 | +app.UseHttpLogging(); |
| 14 | + |
| 15 | +app.UseStaticFiles(); |
| 16 | + |
| 17 | +app.MapGet("/", () => "Running!"); |
| 18 | +app.MapGet("/status.json", IResult (HttpContext theContext) => |
| 19 | +{ |
| 20 | + var statusResult = new StatusResult(true, |
| 21 | + $".NET {Environment.Version}", |
| 22 | + Environment.Version.ToString(), |
| 23 | + DateTime.UtcNow.ToString("o"), |
| 24 | + Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)", |
| 25 | + Environment.GetEnvironmentVariable("COMMIT") ?? "(local)" |
| 26 | + ); |
| 27 | + |
| 28 | + var callback = theContext.Request.Query["callback"]; |
| 29 | + if (string.IsNullOrEmpty(callback)) { |
| 30 | + return Results.Json(statusResult); |
| 31 | + } |
| 32 | + string json = JsonSerializer.Serialize(statusResult); |
| 33 | + return Results.Content($"{callback}({json});", "application/javascript"); |
| 34 | +}); |
| 35 | + |
| 36 | +app.MapPost("/test.json", static async (HttpContext theContext) => |
| 37 | +{ |
| 38 | + // read form variables |
| 39 | + var form = await theContext.Request.ReadFormAsync(); |
| 40 | + var regex = form["regex"].FirstOrDefault(); |
| 41 | + var replacement = form["replacement"].FirstOrDefault(); |
| 42 | + var input = form["input"].ToArray() ?? new string[] { }; |
| 43 | + |
| 44 | + var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}"; |
| 45 | + |
| 46 | + var testOutput = new TestOutput(true, html); |
| 47 | + |
| 48 | + var callback = theContext.Request.Query["callback"]; |
| 49 | + if (string.IsNullOrEmpty(callback)) { |
| 50 | + return Results.Json(testOutput); |
| 51 | + } |
| 52 | + string json = JsonSerializer.Serialize(testOutput); |
| 53 | + return Results.Content($"{callback}({json});", "application/javascript"); |
| 54 | +}); |
| 55 | + |
| 56 | +var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0"; |
| 57 | +var port = Environment.GetEnvironmentVariable("PORT") ?? "4000"; |
| 58 | +var url = $"http://{hostname}:{port}"; |
| 59 | + |
| 60 | +app.Logger.LogInformation($"App started on {url}", url); |
| 61 | + |
| 62 | +app.Run(url); |
| 63 | + |
| 64 | +record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit); |
| 65 | +record TestOutput(Boolean success, string html); |
| 66 | + |
0 commit comments