-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d48c77b
commit 5b8778a
Showing
2 changed files
with
83 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,77 @@ | ||
using System.Text.Json; | ||
using Microsoft.AspNetCore.HttpLogging; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services.AddHttpLogging(logging => | ||
{ | ||
logging.LoggingFields = HttpLoggingFields.All; | ||
logging.CombineLogs = true; | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseHttpLogging(); | ||
|
||
app.UseStaticFiles(); | ||
|
||
app.MapGet("/", () => "Running!"); | ||
app.MapGet("/status.json", IResult (HttpContext theContext) => | ||
{ | ||
var statusResult = new StatusResult(true, | ||
$".NET {Environment.Version}", | ||
Environment.Version.ToString(), | ||
DateTime.UtcNow.ToString("o"), | ||
Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)", | ||
Environment.GetEnvironmentVariable("COMMIT") ?? "(local)" | ||
); | ||
|
||
var callback = theContext.Request.Query["callback"]; | ||
if (string.IsNullOrEmpty(callback)) { | ||
return Results.Json(statusResult); | ||
} | ||
string json = JsonSerializer.Serialize(statusResult); | ||
return Results.Content($"{callback}({json});", "application/javascript"); | ||
}); | ||
|
||
app.MapPost("/test.json", static async (HttpContext theContext) => | ||
{ | ||
// read form variables | ||
var form = await theContext.Request.ReadFormAsync(); | ||
var regex = form["regex"].FirstOrDefault(); | ||
var replacement = form["replacement"].FirstOrDefault(); | ||
var input = form["input"].ToArray() ?? new string[] { }; | ||
|
||
var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}"; | ||
|
||
var testOutput = new TestOutput(true, html); | ||
|
||
var callback = theContext.Request.Query["callback"]; | ||
if (string.IsNullOrEmpty(callback)) { | ||
return Results.Json(testOutput); | ||
} | ||
string json = JsonSerializer.Serialize(testOutput); | ||
return Results.Content($"{callback}({json});", "application/javascript"); | ||
}); | ||
|
||
var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0"; | ||
var port = Environment.GetEnvironmentVariable("PORT") ?? "4000"; | ||
var url = $"http://{hostname}:{port}"; | ||
|
||
app.Logger.LogInformation($"App started on {url}", url); | ||
|
||
app.Run(url); | ||
|
||
record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit); | ||
record TestOutput(Boolean success, string html); | ||
|
||
using System.Text.Json; | ||
using Microsoft.AspNetCore.HttpLogging; | ||
|
||
static async Task<bool> handleJsonp(HttpContext theContext, object data) | ||
{ | ||
string jsonStr = JsonSerializer.Serialize(data); | ||
|
||
var response = theContext.Response; | ||
var callback = theContext.Request.Query["callback"]; | ||
theContext.Response.Clear(); | ||
if (string.IsNullOrEmpty(callback)) | ||
{ | ||
response.ContentType = "application/json"; | ||
response.Headers.Append("Access-Control-Allow-Origin", "*"); | ||
response.Headers.Append("Access-Control-Allow-Methods", "GET"); | ||
response.Headers.Append("Access-Control-Max-Age", "604800"); | ||
await response.WriteAsync(jsonStr); | ||
} else { | ||
response.ContentType = "text/javascript; charset=utf-8"; | ||
await response.WriteAsync($"{callback}({jsonStr});"); | ||
} | ||
return true; | ||
} | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
builder.Services.AddHttpLogging(logging => | ||
{ | ||
logging.LoggingFields = HttpLoggingFields.All; | ||
logging.CombineLogs = true; | ||
}); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseHttpLogging(); | ||
|
||
app.UseStaticFiles(); | ||
|
||
app.MapGet("/", () => "Running!"); | ||
app.MapGet("/status.json", static async (HttpContext theContext) => | ||
{ | ||
var statusResult = new StatusResult(true, | ||
$".NET {Environment.Version}", | ||
Environment.Version.ToString(), | ||
DateTime.UtcNow.ToString("o"), | ||
Environment.GetEnvironmentVariable("LASTMOD") ?? "(local)", | ||
Environment.GetEnvironmentVariable("COMMIT") ?? "(local)" | ||
); | ||
|
||
await handleJsonp(theContext, statusResult); | ||
}); | ||
|
||
app.MapPost("/test.json", static async (HttpContext theContext) => | ||
{ | ||
// read form variables | ||
var form = await theContext.Request.ReadFormAsync(); | ||
var regex = form["regex"].FirstOrDefault(); | ||
var replacement = form["replacement"].FirstOrDefault(); | ||
var input = form["input"].ToArray() ?? new string[] { }; | ||
|
||
var html = $"{regex} {replacement} {input.Length} {input.FirstOrDefault()}"; | ||
|
||
var testOutput = new TestOutput(true, html); | ||
|
||
await handleJsonp(theContext, testOutput); | ||
}); | ||
|
||
var hostname = Environment.GetEnvironmentVariable("HOSTNAME") ?? "0.0.0.0"; | ||
var port = Environment.GetEnvironmentVariable("PORT") ?? "4000"; | ||
var url = $"http://{hostname}:{port}"; | ||
|
||
app.Logger.LogInformation($"App started on {url}", url); | ||
|
||
app.Run(url); | ||
|
||
record StatusResult(Boolean success, string tech, string version, string timestamp, string lastmod, string commit); | ||
record TestOutput(Boolean success, string html); | ||
|