|
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 |
| - |
| 1 | +using System.Text.Json; |
| 2 | +using Microsoft.AspNetCore.HttpLogging; |
| 3 | + |
| 4 | +static async Task<bool> handleJsonp(HttpContext theContext, object data) |
| 5 | +{ |
| 6 | + string jsonStr = JsonSerializer.Serialize(data); |
| 7 | + |
| 8 | + var response = theContext.Response; |
| 9 | + var callback = theContext.Request.Query["callback"]; |
| 10 | + theContext.Response.Clear(); |
| 11 | + if (string.IsNullOrEmpty(callback)) |
| 12 | + { |
| 13 | + response.ContentType = "application/json"; |
| 14 | + response.Headers.Append("Access-Control-Allow-Origin", "*"); |
| 15 | + response.Headers.Append("Access-Control-Allow-Methods", "GET"); |
| 16 | + response.Headers.Append("Access-Control-Max-Age", "604800"); |
| 17 | + await response.WriteAsync(jsonStr); |
| 18 | + } else { |
| 19 | + response.ContentType = "text/javascript; charset=utf-8"; |
| 20 | + await response.WriteAsync($"{callback}({jsonStr});"); |
| 21 | + } |
| 22 | + return true; |
| 23 | +} |
| 24 | + |
| 25 | +var builder = WebApplication.CreateBuilder(args); |
| 26 | +builder.Services.AddHttpLogging(logging => |
| 27 | +{ |
| 28 | + logging.LoggingFields = HttpLoggingFields.All; |
| 29 | + logging.CombineLogs = true; |
| 30 | +}); |
| 31 | + |
| 32 | +var app = builder.Build(); |
| 33 | + |
| 34 | +app.UseHttpLogging(); |
| 35 | + |
| 36 | +app.UseStaticFiles(); |
| 37 | + |
| 38 | +app.MapGet("/", () => "Running!"); |
| 39 | +app.MapGet("/status.json", static async (HttpContext theContext) => |
| 40 | +{ |
| 41 | + var statusResult = new StatusResult(true, |
| 42 | + $".NET {Environment.Version}", |
| 43 | + Environment.Version.ToString(), |
| 44 | + DateTime.UtcNow.ToString("o"), |
| 45 | + Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)", |
| 46 | + Environment.GetEnvironmentVariable("COMMIT") ?? "(local)" |
| 47 | + ); |
| 48 | + |
| 49 | + await handleJsonp(theContext, statusResult); |
| 50 | +}); |
| 51 | + |
| 52 | +app.MapPost("/test.json", static async (HttpContext theContext) => |
| 53 | +{ |
| 54 | + // read form variables |
| 55 | + var form = await theContext.Request.ReadFormAsync(); |
| 56 | + var regex = form["regex"].FirstOrDefault(); |
| 57 | + var replacement = form["replacement"].FirstOrDefault(); |
| 58 | + var input = form["input"].ToArray() ?? new string[] { }; |
| 59 | + |
| 60 | + var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}"; |
| 61 | + |
| 62 | + var testOutput = new TestOutput(true, html); |
| 63 | + |
| 64 | + await handleJsonp(theContext, testOutput); |
| 65 | +}); |
| 66 | + |
| 67 | +var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0"; |
| 68 | +var port = Environment.GetEnvironmentVariable("PORT") ?? "4000"; |
| 69 | +var url = $"http://{hostname}:{port}"; |
| 70 | + |
| 71 | +app.Logger.LogInformation($"App started on {url}", url); |
| 72 | + |
| 73 | +app.Run(url); |
| 74 | + |
| 75 | +record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit); |
| 76 | +record TestOutput(Boolean success, string html); |
| 77 | + |
0 commit comments