92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
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<AppDbContext>(options =>
|
|
options.UseSqlite($"Data Source={dbPath}"));
|
|
|
|
builder.Services.AddControllersWithViews();
|
|
builder.Services.AddHttpClient();
|
|
|
|
// Cookie authentication for admin panel
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/account/login";
|
|
options.LogoutPath = "/account/logout";
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(12);
|
|
options.SlidingExpiration = true;
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Auto-create database on startup
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
db.Database.EnsureCreated();
|
|
}
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/error");
|
|
app.UseHsts();
|
|
}
|
|
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
// Ensure uploads directory exists
|
|
var uploadsPath = Path.Combine(app.Environment.WebRootPath, "uploads");
|
|
if (!Directory.Exists(uploadsPath))
|
|
Directory.CreateDirectory(uploadsPath);
|
|
|
|
app.MapControllerRoute(
|
|
name: "account",
|
|
pattern: "account/{action=Login}",
|
|
defaults: new { controller = "Account" });
|
|
|
|
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" });
|
|
|
|
app.MapControllerRoute(
|
|
name: "display",
|
|
pattern: "d/{slug}",
|
|
defaults: new { controller = "Display", action = "Show" });
|
|
|
|
app.MapControllerRoute(
|
|
name: "display-root",
|
|
pattern: "{slug}",
|
|
defaults: new { controller = "Display", action = "Show" },
|
|
constraints: new { slug = new NoticeBoard.Routing.DeviceSlugConstraint() });
|
|
|
|
app.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "",
|
|
defaults: new { controller = "Admin", action = "Index" });
|
|
|
|
app.Run();
|