From 1b4b90b2362cbd0fd26bb38342e807ff77022291 Mon Sep 17 00:00:00 2001 From: Ronny Gunawan <3048897+ronnygunawan@users.noreply.github.com> Date: Sun, 26 Nov 2023 18:24:20 +0700 Subject: [PATCH] Remove MVC --- BotNet/Controllers/DecimalClockController.cs | 13 ---- BotNet/Controllers/HealthController.cs | 10 --- BotNet/Controllers/ImageRendererController.cs | 31 -------- .../MemeGeneratorTestController.cs | 23 ------ BotNet/Controllers/PSEController.cs | 11 --- BotNet/Controllers/WebhookController.cs | 33 -------- BotNet/Program.cs | 75 +++++++++++++----- .../DecimalClock/DecimalClockSvgBuilder.cs | 76 +++++++++++++++++++ 8 files changed, 134 insertions(+), 138 deletions(-) delete mode 100644 BotNet/Controllers/DecimalClockController.cs delete mode 100644 BotNet/Controllers/HealthController.cs delete mode 100644 BotNet/Controllers/ImageRendererController.cs delete mode 100644 BotNet/Controllers/MemeGeneratorTestController.cs delete mode 100644 BotNet/Controllers/PSEController.cs delete mode 100644 BotNet/Controllers/WebhookController.cs create mode 100644 BotNet/Views/DecimalClock/DecimalClockSvgBuilder.cs diff --git a/BotNet/Controllers/DecimalClockController.cs b/BotNet/Controllers/DecimalClockController.cs deleted file mode 100644 index 290cde9..0000000 --- a/BotNet/Controllers/DecimalClockController.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace BotNet.Controllers { - [Route("decimalclock")] - public class DecimalClockController : Controller { - [Route("svg")] - [ResponseCache(Duration = 0, NoStore = true)] - public IActionResult Svg() { - Response.ContentType = "image/svg+xml"; - return View(); - } - } -} diff --git a/BotNet/Controllers/HealthController.cs b/BotNet/Controllers/HealthController.cs deleted file mode 100644 index 220eab4..0000000 --- a/BotNet/Controllers/HealthController.cs +++ /dev/null @@ -1,10 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace BotNet.Controllers { - [Route("")] - public class HealthController : Controller { - public IActionResult Index() { - return Content("https://t.me/teknologi_umum_v2"); - } - } -} \ No newline at end of file diff --git a/BotNet/Controllers/ImageRendererController.cs b/BotNet/Controllers/ImageRendererController.cs deleted file mode 100644 index 6e72d32..0000000 --- a/BotNet/Controllers/ImageRendererController.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using BotNet.Services.ColorCard; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Logging; - -namespace BotNet.Controllers { - [Route("renderer")] - public class ImageRendererController : Controller { - private readonly ColorCardRenderer _colorCardRenderer; - private readonly ILogger _logger; - - public ImageRendererController( - ColorCardRenderer colorCardRenderer, - ILogger logger - ) { - _colorCardRenderer = colorCardRenderer; - _logger = logger; - } - - [HttpGet("color")] - public IActionResult RenderColorCard(string name) { - try { - byte[] colorCardPng = _colorCardRenderer.RenderColorCard(name); - return File(colorCardPng, "image/png", true); - } catch (Exception exc) { - _logger.LogError(exc, "Cannot render color card."); - return NotFound(); - } - } - } -} diff --git a/BotNet/Controllers/MemeGeneratorTestController.cs b/BotNet/Controllers/MemeGeneratorTestController.cs deleted file mode 100644 index 6082388..0000000 --- a/BotNet/Controllers/MemeGeneratorTestController.cs +++ /dev/null @@ -1,23 +0,0 @@ -#if DEBUG -using BotNet.Services.Meme; -using Microsoft.AspNetCore.Mvc; - -namespace BotNet.Controllers { - [Route("meme")] - public class MemeGeneratorTestController : Controller { - private readonly MemeGenerator _memeGenerator; - - public MemeGeneratorTestController( - MemeGenerator memeGenerator - ) { - _memeGenerator = memeGenerator; - } - - [Route("ramad")] - public IActionResult RenderRamad() { - byte[] memePng = _memeGenerator.CaptionRamad("Melakukan TDD, meski situasi sulit"); - return File(memePng, "image/png", true); - } - } -} -#endif diff --git a/BotNet/Controllers/PSEController.cs b/BotNet/Controllers/PSEController.cs deleted file mode 100644 index 4cfaf17..0000000 --- a/BotNet/Controllers/PSEController.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace BotNet.Controllers { - [Route("pse")] - public class PSEController : Controller { - [HttpGet] - public IActionResult Index() { - return View(); - } - } -} diff --git a/BotNet/Controllers/WebhookController.cs b/BotNet/Controllers/WebhookController.cs deleted file mode 100644 index 319d74d..0000000 --- a/BotNet/Controllers/WebhookController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; -using BotNet.Bot; -using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; -using Telegram.Bot; -using Telegram.Bot.Types; - -namespace BotNet.Controllers { - [Route("webhook")] - public class WebhookController : ControllerBase { - private readonly ITelegramBotClient _telegramBotClient; - private readonly string _secretPath; - private readonly UpdateHandler _updateHandler; - - public WebhookController( - UpdateHandler updateHandler, - IOptions botOptionsAccessor, - ITelegramBotClient telegramBotClient - ) { - _updateHandler = updateHandler; - _secretPath = botOptionsAccessor.Value.AccessToken!.Split(':')[1]; - _telegramBotClient = telegramBotClient; - } - - [HttpPost("{secretPath}")] - public async Task PostAsync(string secretPath, [FromBody] Update update, CancellationToken cancellationToken) { - if (secretPath != _secretPath) return NotFound(); - await _updateHandler.HandleUpdateAsync(_telegramBotClient, update, cancellationToken); - return Ok(); - } - } -} diff --git a/BotNet/Program.cs b/BotNet/Program.cs index a043de1..b000b07 100644 --- a/BotNet/Program.cs +++ b/BotNet/Program.cs @@ -1,4 +1,5 @@ -using BotNet.Bot; +using System.Threading; +using BotNet.Bot; using BotNet.Services.BMKG; using BotNet.Services.Brainfuck; using BotNet.Services.ClearScript; @@ -22,13 +23,22 @@ using BotNet.Services.Tokopedia; using BotNet.Services.Typography; using BotNet.Services.Weather; +using BotNet.Views.DecimalClock; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Options; using Orleans.Hosting; +using Telegram.Bot; +using Telegram.Bot.Types; -WebApplicationBuilder builder = WebApplication.CreateBuilder(args); +WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(args); + +// HTTPS support +builder.WebHost.UseKestrelHttpsConfiguration(); // DI Services builder.Services.Configure(builder.Configuration.GetSection("HostingOptions")); @@ -77,23 +87,54 @@ siloBuilder.UseLocalhostClustering(); }); -// Web -builder.Services.AddControllersWithViews().AddNewtonsoftJson(); -builder.Services.AddResponseCaching(); -builder.Services.AddResponseCompression(); - WebApplication app = builder.Build(); -if (app.Environment.IsDevelopment()) { - app.UseDeveloperExceptionPage(); -} else { - app.UseHsts(); -} +// Healthcheck endpoint +app.MapGet("/", () => "https://t.me/teknologi_umum_v2"); + +// Webhook +app.MapPost("/webhook/{secretPath}", async ( + string secretPath, + [FromBody] Update update, + [FromServices] ITelegramBotClient telegramBotClient, + [FromServices] UpdateHandler updateHandler, + [FromServices] IOptions botOptionsAccessor, + CancellationToken cancellationToken +) => { + if (secretPath != botOptionsAccessor.Value.AccessToken!.Split(':')[1]) return Results.NotFound(); + await updateHandler.HandleUpdateAsync(telegramBotClient, update, cancellationToken); + return Results.Ok(); +}); + +// Decimal clock renderer +app.MapGet("/decimalclock/svg", () => Results.Content( + content: DecimalClockSvgBuilder.GenerateSvg(), + contentType: "image/svg+xml" +)); + +// Color card renderer +app.MapGet("/renderer/color", ( + string name, + [FromServices] ColorCardRenderer colorCardRenderer +) => { + try { + return Results.File( + fileContents: colorCardRenderer.RenderColorCard(name), + contentType: "image/png", + enableRangeProcessing: true + ); + } catch { + return Results.NotFound(); + } +}); -app.UseHttpsRedirection(); -app.UseRouting(); -app.UseResponseCaching(); -app.UseResponseCompression(); -app.MapDefaultControllerRoute(); +// Meme generator test +#if DEBUG +app.MapGet("/meme", ([FromServices] MemeGenerator memeGenerator) => Results.File( + fileContents: memeGenerator.CaptionRamad("Melakukan TDD, meski situasi sulit"), + contentType: "image/png", + enableRangeProcessing: true +)); +#endif app.Run(); diff --git a/BotNet/Views/DecimalClock/DecimalClockSvgBuilder.cs b/BotNet/Views/DecimalClock/DecimalClockSvgBuilder.cs new file mode 100644 index 0000000..1ea6160 --- /dev/null +++ b/BotNet/Views/DecimalClock/DecimalClockSvgBuilder.cs @@ -0,0 +1,76 @@ +using System; +using System.Text; + +namespace BotNet.Views.DecimalClock { + public static class DecimalClockSvgBuilder { + public static string GenerateSvg() { + TimeSpan timeOfDay = DateTime.UtcNow.AddHours(7).TimeOfDay; + double fractionOfDay = timeOfDay / TimeSpan.FromDays(1); + + StringBuilder digitsBuilder = new(); + for (int i = 0; i < 10; i++) { + double x = 400 - Math.Sin(Math.PI * i / 5) * 320; + double y = 400 + Math.Cos(Math.PI * i / 5) * 320; + digitsBuilder.Append($$""" + {{i}} + + """); + } + + StringBuilder ticksBuilder = new(); + for (int i = 0; i < 100; i++) { + ticksBuilder.Append($$""" + + + """); + } + + return $$""" + + + + + + {{digitsBuilder.ToString().Trim()}} + {{ticksBuilder.ToString().Trim()}} + + + + + + + + + + + + + + + + + + + + """; + } + } +}