diff --git a/Dockerfile b/Dockerfile index b5595bc..84f24fe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,11 +13,16 @@ RUN dotnet publish -c Release RUN find . -#CMD ["/source/bin/Release/net8.0/publish/regexplanet-dotnet.dll"] # # final image # FROM mcr.microsoft.com/dotnet/aspnet:8.0-noble-chiseled-extra + +ARG COMMIT="(not set)" +ARG LASTMOD="(not set)" +ENV COMMIT=$COMMIT +ENV LASTMOD=$LASTMOD + WORKDIR /app COPY --from=builder /source/bin/Release/net8.0/publish /app COPY ./wwwroot /app/wwwroot diff --git a/Program.cs b/Program.cs index 73523dc..08930b7 100644 --- a/Program.cs +++ b/Program.cs @@ -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 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); +