Skip to content

Commit

Permalink
Remove MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnygunawan committed Nov 26, 2023
1 parent 7acea04 commit 1b4b90b
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 138 deletions.
13 changes: 0 additions & 13 deletions BotNet/Controllers/DecimalClockController.cs

This file was deleted.

10 changes: 0 additions & 10 deletions BotNet/Controllers/HealthController.cs

This file was deleted.

31 changes: 0 additions & 31 deletions BotNet/Controllers/ImageRendererController.cs

This file was deleted.

23 changes: 0 additions & 23 deletions BotNet/Controllers/MemeGeneratorTestController.cs

This file was deleted.

11 changes: 0 additions & 11 deletions BotNet/Controllers/PSEController.cs

This file was deleted.

33 changes: 0 additions & 33 deletions BotNet/Controllers/WebhookController.cs

This file was deleted.

75 changes: 58 additions & 17 deletions BotNet/Program.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<HostingOptions>(builder.Configuration.GetSection("HostingOptions"));
Expand Down Expand Up @@ -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<BotOptions> 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();
76 changes: 76 additions & 0 deletions BotNet/Views/DecimalClock/DecimalClockSvgBuilder.cs
Original file line number Diff line number Diff line change
@@ -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($$"""
<text x="{{x}}" y="{{y}}">{{i}}</text>
""");
}

StringBuilder ticksBuilder = new();
for (int i = 0; i < 100; i++) {
ticksBuilder.Append($$"""
<rect x="398.8" y="0" width="2.4" class="{{(i % 10 == 0 ? "large tick" : "tick")}}" transform="rotate({{i * 3.6}})" transform-origin="400 400" fill="#888"></rect>
""");
}

return $$"""
<svg viewBox="0 0 1000 1000" xmlns="http://www.w3.org/2000/svg">
<style>
text {
font: 80px sans-serif;
text-anchor: middle;
alignment-baseline: middle;
}
.logo {
font: 20px sans-serif;
font-weight: 500;
alignment-baseline: top;
letter-spacing: 2.4px;
}
.tick {
height: 16px;
}
.large.tick {
height: 32px;
}
</style>
<rect x="88.75" y="88.75" width="822.5" height="822.5" rx="422.5" stroke="#888" stroke-width="22.5" fill="#fff"></rect>
<g transform="translate(100 100)">
<text x="400" y="228" class="logo" fill="#bbb">TEKNUM</text>
{{digitsBuilder.ToString().Trim()}}
{{ticksBuilder.ToString().Trim()}}
<g transform="rotate({{fractionOfDay * 360 + 180}})" transform-origin="400 400">
<rect x="396" y="216" width="8" height="184" fill="#000" transform-origin="400 400">
<animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="24h" repeatCount="indefinite" />
</rect>
</g>
<g transform="rotate({{fractionOfDay * 10 % 1 * 360 + 180}})" transform-origin="400 400">
<rect x="396" y="160" width="8" height="240" fill="#000" transform-origin="400 400">
<animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="2.4h" repeatCount="indefinite" />
</rect>
</g>
<g transform="rotate({{fractionOfDay * 1000 % 1 * 360 + 180}})" transform-origin="400 400">
<rect x="398.8" y="144" width="2.4" height="256" fill="#000" transform-origin="400 400">
<animateTransform attributeName="transform" attributeType="XML" type="rotate" from="0" to="360" dur="0.024h" repeatCount="indefinite" />
</rect>
</g>
<rect x="384" y="384" width="32" height="32" rx="16" fill="#000"></rect>
</g>
</svg>
""";
}
}
}

0 comments on commit 1b4b90b

Please sign in to comment.