using System.Text; using AvParser.ImgTestService; var builder = WebApplication.CreateBuilder(args); var options = builder.Configuration.GetSection("ImgTest").Get() ?? new ImgTestOptions(); var endpoints = options.BuildLookup(); var app = builder.Build(); // A plain index so opening the host in a browser explains what is here. app.MapGet( "/", () => { var text = new StringBuilder(); text.AppendLine("AvParser image test service"); text.AppendLine(); text.AppendLine($"miss rate: {options.MissRate:0.###}"); text.AppendLine("endpoints:"); foreach (var endpoint in endpoints.Values) { var ext = endpoint.Extensions.Length == 0 ? "(none)" : string.Join(" ", endpoint.Extensions); text.AppendLine( $" /{endpoint.Path}/{{id}} length {endpoint.MinLength}-{endpoint.MaxLength}, " + $"{endpoint.Allowed.Count} chars, ext {ext}" ); } return Results.Text(text.ToString(), "text/plain; charset=utf-8"); } ); app.MapGet("/healthz", () => Results.Text("ok", "text/plain")); // One handler for every configured endpoint: the first segment selects the config, the second is // the id (optionally with an extension). A miss and an unknown endpoint are both a plain 404. app.MapGet( "/{endpoint}/{id}", (string endpoint, string id) => { if (!endpoints.TryGetValue(endpoint, out var config)) { return Results.NotFound(); } var core = config.StripExtension(id); if (!config.Matches(core) || ImgTestOptions.IsMiss(core, options.MissRate)) { return Results.NotFound(); } return Results.Bytes(Identicon.Render(core), "image/png"); } ); app.Run();