using System.Security.Cryptography.X509Certificates; using Uwaa.HTTP; using Uwaa.HTTP.Routing; using Uwaa.PNG; namespace GeneratorServer; internal class Program { static void Main(string[] args) { Console.WriteLine("Creating router"); Router router = CreateRouter(); Console.WriteLine("Starting HTTP"); HttpServer http = new HttpServer(80, null, router); http.Start(); if (!File.Exists("certs/certificate.crt") || !File.Exists("certs/private.key")) { #if RELEASE Console.WriteLine("Warning: No HTTPS, \"certificate.crt\" and/or \"private.key\" are missing in \"certs\" directory."); #endif } else { Console.WriteLine("Starting HTTPS"); HttpServer https = new HttpServer(443, X509Certificate2.CreateFromPemFile("certs/certificate.crt", "certs/private.key"), router); https.Start(); } Console.WriteLine("Ready"); Task.Delay(-1).Wait(); } static Router CreateRouter() { var router = new Router(); router.Add("background", new BGGenerator()); router.Add(new StaticEndpoint(HttpResponse.NotFound())); return router; } } class BGGenerator : RouterBase { public override HttpMethod Method => HttpMethod.GET; public override int Arguments => 0; protected override Task GetResponseInner(HttpRequest req, HttpClientInfo info, ArraySegment path) { MIMEType contentType = new MIMEType("image", "png"); if (!req.CanAccept(contentType)) return Task.FromResult(HttpResponse.NotAcceptable())!; const int width = 100; const int height = 100; PngBuilder png = PngBuilder.Create(width, height, false); Pixel left = new Pixel((byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256)); Pixel right = new Pixel((byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256)); Pixel top = new Pixel((byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256)); Pixel bottom = new Pixel((byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256), (byte)Random.Shared.Next(0, 256)); for (int y = 0; y < height; y++) for (int x = 0; x < width; x++) { Pixel px = Lerp(Lerp(left, right, (float)x / width), Lerp(top, bottom, (float)y / height), 0.5f); png.SetPixel(px, x, y); } return Task.FromResult(HttpResponse.OK(new HttpContent(contentType, png.Save())))!; } static Pixel Lerp(Pixel min, Pixel max, float factor) { return new Pixel(Lerp(min.R, max.R, factor), Lerp(min.G, max.G, factor), Lerp(min.B, max.B, factor)); } static byte Lerp(byte min, byte max, float factor) { return (byte)(min + (max - min) * factor); } static float Lerp(float min, float max, float factor) { return min + (max - min) * factor; } }