1
0
Fork 0
netddi/Program.cs

92 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-01-30 00:14:20 +01:00
using DotNetDDI.Data;
using DotNetDDI.Integrations.Kea;
using DotNetDDI.Integrations.PowerDns;
using DotNetDDI.Options;
using DotNetDDI.Services.Dhcp;
using DotNetDDI.Services.Dns;
2025-01-29 23:26:50 +01:00
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
2025-01-30 00:14:20 +01:00
using Microsoft.AspNetCore.Connections;
2025-01-29 23:26:50 +01:00
var builder = WebApplication.CreateBuilder(args);
2025-01-30 00:14:20 +01:00
builder.Services.AddSystemd();
2025-01-29 23:26:50 +01:00
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection") ?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
2025-01-30 00:14:20 +01:00
options.UseSqlite(connectionString));
2025-01-29 23:26:50 +01:00
builder.Services.AddDatabaseDeveloperPageExceptionFilter();
2025-01-30 01:29:51 +01:00
bool adminAuthenticate = builder.Configuration.GetValue("Admin:Authentication", true);
if (adminAuthenticate)
{
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
}
2025-01-29 23:26:50 +01:00
builder.Services.AddRazorPages();
2025-01-30 00:14:20 +01:00
builder.Services.Configure<DhcpOptions>(builder.Configuration.GetRequiredSection("Dhcp"));
builder.Services.Configure<PowerDnsOptions>(builder.Configuration.GetRequiredSection("PowerDns"));
builder.Services.AddHostedService<DhcpWatcher>();
builder.Services.AddHostedService<DhcpQueueWorker>();
builder.Services.AddSingleton<DhcpLeaseQueue>();
builder.Services.AddSingleton<DnsRepository>();
builder.Services.AddDhcpWatcherFactory();
builder.Services.AddKeaFactory();
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.UseSystemd(_ => throw new InvalidOperationException("SystemD Socket Activation is not supported by Kestrel."));
var pdnsOptions = context.Configuration.GetRequiredSection("PowerDns:Listener").Get<PowerDnsListenerOptions>();
ArgumentNullException.ThrowIfNull(pdnsOptions);
var path = PathEx.ExpandPath(pdnsOptions.Socket);
FileInfo file = new(path);
file.Directory!.Create();
file.Delete();
options.ListenUnixSocket(path, options =>
{
options.UseConnectionHandler<PowerDnsHandler>();
});
});
2025-01-29 23:26:50 +01:00
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
2025-01-30 00:14:20 +01:00
app.UseMigrationsEndPoint();
2025-01-29 23:26:50 +01:00
}
else
{
2025-01-30 00:14:20 +01:00
app.UseExceptionHandler("/Error");
2025-01-29 23:26:50 +01:00
}
app.UseRouting();
2025-01-30 01:29:51 +01:00
if (adminAuthenticate)
2025-01-30 00:14:20 +01:00
{
app.UseAuthorization();
}
2025-01-29 23:26:50 +01:00
2025-01-29 23:53:57 +01:00
app.UseStaticFiles();
app.MapRazorPages();
2025-01-29 23:26:50 +01:00
2025-01-30 01:29:51 +01:00
using (var scope = app.Services.CreateScope())
{
using var appDb = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
appDb.Database.Migrate();
if (adminAuthenticate)
{
/* TODO: Generate Admin Account if None found */
}
}
2025-01-29 23:26:50 +01:00
app.Run();