Template
mirror of
https://github.com/egmont11/ASP.Net-Core-MVC-Template.git
synced 2026-07-24 07:09:56 +02:00
76 lines
2.4 KiB
C#
76 lines
2.4 KiB
C#
using dotenv.net;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TemplateWeb.Data;
|
|
using TemplateWeb.Interfaces;
|
|
using TemplateWeb.Services;
|
|
|
|
namespace TemplateWeb;
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
DotEnv.Load();
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// gets the connection string from configuration
|
|
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
|
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
|
|
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseNpgsql(connectionString));
|
|
|
|
builder.Services.AddControllersWithViews();
|
|
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/Auth/Login";
|
|
options.AccessDeniedPath = "/Auth/AccessDenied";
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(2);
|
|
});
|
|
builder.Services.AddAuthorization();
|
|
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Apply migrations on startup
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
if (db.Database.GetPendingMigrations().Any())
|
|
{
|
|
db.Database.Migrate();
|
|
}
|
|
|
|
// Seed the initial admin user
|
|
DataSeeder.SeedAdminUser(db).GetAwaiter().GetResult();
|
|
}
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Home/Error");
|
|
}
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapStaticAssets();
|
|
app.MapControllerRoute(
|
|
name: "areas",
|
|
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
|
|
|
|
app.MapControllerRoute(
|
|
name: "default",
|
|
pattern: "{controller=Home}/{action=Index}/{id?}")
|
|
.WithStaticAssets();
|
|
|
|
app.Run();
|
|
}
|
|
} |