From f5fd2002d80c9f4b7e8e88de1ab35d45114e5366 Mon Sep 17 00:00:00 2001 From: jessikitty Date: Wed, 20 May 2026 14:55:47 +1000 Subject: [PATCH] =?UTF-8?q?v1.0.0:=20Program.cs=20=E2=80=94=20routing,=20D?= =?UTF-8?q?I,=20auto-create=20DB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Program.cs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Program.cs diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..92d7475 --- /dev/null +++ b/Program.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore; +using NoticeBoard.Data; + +var builder = WebApplication.CreateBuilder(args); + +// SQLite database in app directory +var dbPath = Path.Combine(builder.Environment.ContentRootPath, "noticeboard.db"); +builder.Services.AddDbContext(options => + options.UseSqlite($"Data Source={dbPath}")); + +builder.Services.AddControllersWithViews(); +builder.Services.AddHttpClient(); + +var app = builder.Build(); + +// Auto-create database on startup (use Migrate() if using EF migrations) +using (var scope = app.Services.CreateScope()) +{ + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.EnsureCreated(); +} + +if (!app.Environment.IsDevelopment()) +{ + app.UseExceptionHandler("/error"); + app.UseHsts(); +} + +app.UseStaticFiles(); +app.UseRouting(); + +// Ensure uploads directory exists +var uploadsPath = Path.Combine(app.Environment.WebRootPath, "uploads"); +if (!Directory.Exists(uploadsPath)) + Directory.CreateDirectory(uploadsPath); + +app.MapControllerRoute( + name: "admin", + pattern: "admin/{action=Index}/{id?}", + defaults: new { controller = "Admin" }); + +app.MapControllerRoute( + name: "slides", + pattern: "admin/slides/{action=Index}/{id?}", + defaults: new { controller = "Slides" }); + +app.MapControllerRoute( + name: "devices", + pattern: "admin/devices/{action=Index}/{id?}", + defaults: new { controller = "Devices" }); + +app.MapControllerRoute( + name: "api", + pattern: "api/{action}/{id?}", + defaults: new { controller = "Api" }); + +// Display route: /{slug} — must be last to act as catch-all +app.MapControllerRoute( + name: "display", + pattern: "d/{slug}", + defaults: new { controller = "Display", action = "Show" }); + +// Also support root-level slugs +app.MapControllerRoute( + name: "display-root", + pattern: "{slug}", + defaults: new { controller = "Display", action = "Show" }, + constraints: new { slug = new NoticeBoard.Routing.DeviceSlugConstraint() }); + +// Default route goes to admin +app.MapControllerRoute( + name: "default", + pattern: "", + defaults: new { controller = "Admin", action = "Index" }); + +app.Run();