Files
Aberwyn/Aberwyn/Program.cs
Elias Jansson 9e3b7a079e
All checks were successful
continuous-integration/drone/push Build is passing
Pizzaorder
2025-05-24 16:24:17 +02:00

141 lines
4.1 KiB
C#
Raw Blame History

using Microsoft.EntityFrameworkCore;
using Aberwyn.Data;
using System.Text;
using System.Globalization;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Aberwyn.Models;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddJsonOptions(opts =>
{
// Beh<65>ll propertynamn som i C#-klassen (PascalCase)
opts.JsonSerializerOptions.PropertyNamingPolicy = null;
// Ignorera null-v<>rden vid serialisering
opts.JsonSerializerOptions.IgnoreNullValues = true;
});
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromHours(1);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
builder.Services.AddRazorPages();
builder.Services.AddHttpClient();
// Configure your DbContext with MySQLs
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
builder.Configuration.GetConnectionString("DefaultConnection"),
new MySqlServerVersion(new Version(8, 0, 36))
));
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
builder.Services.Configure<IdentityOptions>(options =>
{
options.Password.RequireDigit = false;
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
});
builder.Services.AddControllersWithViews();
// Register your services
builder.Services.AddScoped<MenuService>();
builder.Services.AddSingleton<PushNotificationService>(sp =>
{
var config = sp.GetRequiredService<IConfiguration>();
return new PushNotificationService(
config["VapidKeys:Subject"],
config["VapidKeys:PublicKey"],
config["VapidKeys:PrivateKey"]
);
});
builder.Services.Configure<VapidOptions>(builder.Configuration.GetSection("Vapid"));
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Identity/Account/Login"; // korrekt för ditt nuvarande upplägg
});
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
builder.Services.Configure<RequestLocalizationOptions>(options =>
{
var supportedCultures = new[] { new CultureInfo("sv-SE") };
options.DefaultRequestCulture = new RequestCulture("sv-SE");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
});
builder.Configuration
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddEnvironmentVariables();
builder.Services.AddSingleton<PushNotificationService>(sp =>
{
var config = sp.GetRequiredService<IConfiguration>();
return new PushNotificationService(
config["VapidKeys:Subject"],
config["VapidKeys:PublicKey"],
config["VapidKeys:PrivateKey"]
);
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseRouting();
app.UseSession();
app.UseAuthentication();;
app.UseAuthorization();
// Map controller endpoints (including AJAX JSON actions)
app.MapControllers();
// Conventional MVC route
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// Map Razor Pages
app.MapRazorPages();
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await IdentityDataInitializer.SeedData(services);
}
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
await IdentityDataInitializer.SeedData(services);
var context = services.GetRequiredService<ApplicationDbContext>();
await TestDataSeeder.SeedBudget(context);
}
app.Run();