AuthorBuddy/Program.cs

113 lines
3.9 KiB
C#
Raw Permalink Normal View History

2026-05-14 12:31:35 +02:00
using AuthorBuddy.Web.Components;
2026-06-21 07:52:03 +02:00
using AuthorBuddy.Web.Data;
2026-05-14 12:31:35 +02:00
using AuthorBuddy.Web.Models;
using AuthorBuddy.Web.Services;
2026-05-20 06:12:54 +02:00
using LiteDB;
2026-05-21 11:04:42 +02:00
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Components.Authorization;
2026-05-25 06:38:16 +02:00
using Microsoft.Extensions.FileProviders;
2026-05-14 12:31:35 +02:00
var builder = WebApplication.CreateBuilder(args);
2026-06-09 19:11:25 +02:00
builder.WebHost.UseWebRoot(Path.Combine(builder.Environment.ContentRootPath, "wwwroot"));
2026-05-14 12:31:35 +02:00
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
2026-05-31 19:31:27 +02:00
var dbDir = OperatingSystem.IsLinux()
2026-06-09 19:11:25 +02:00
? Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"author-buddy")
2026-05-31 19:31:27 +02:00
: AppContext.BaseDirectory;
Directory.CreateDirectory(dbDir);
var dbPath = Path.Combine(dbDir, Constants.DbFile);
2026-06-21 07:52:03 +02:00
var db = new LiteDatabase($"Filename={dbPath};Connection=direct");
MigrateUserRoles(db);
builder.Services.AddSingleton<ILiteDatabase>(db);
2026-05-20 06:12:54 +02:00
2026-05-14 12:31:35 +02:00
builder.Services.AddSingleton<OllamaSettings>();
builder.Services.AddSingleton<IOllamaService, OllamaService>();
2026-05-31 19:31:27 +02:00
builder.Services.AddSingleton<ILLMBackend, OllamaBackend>();
builder.Services.AddSingleton<ILLMBackend, LlamaCppBackend>();
builder.Services.AddSingleton<IOllamaSetupService, OllamaSetupService>();
2026-05-14 12:31:35 +02:00
builder.Services.AddSingleton<IFileScannerService, FileScannerService>();
2026-05-25 06:38:16 +02:00
builder.Services.AddScoped<ISettingsService, SettingsService>();
2026-05-16 16:13:35 +02:00
builder.Services.AddScoped<IThemeService, ThemeService>();
2026-05-14 12:31:35 +02:00
builder.Services.AddSingleton<IProjectService, ProjectService>();
2026-05-31 19:31:27 +02:00
builder.Services.AddSingleton<IExportService, ExportService>();
2026-05-20 06:12:54 +02:00
builder.Services.AddSingleton<IRagService, RagService>();
builder.Services.AddSingleton<IWikipediaService, WikipediaService>();
2026-05-31 19:31:27 +02:00
builder.Services.AddSingleton<OperationState>();
builder.Services.AddSingleton<IDbAdminService, DbAdminService>();
2026-05-21 11:04:42 +02:00
builder.Services.AddScoped<IAuthService, AuthService>();
2026-06-21 07:52:03 +02:00
builder.Services.AddSingleton<IEmailService, EmailService>();
builder.Services.AddSingleton<IChatService, ChatService>();
2026-07-04 10:32:35 +02:00
builder.Services.AddSingleton<IQuickMarkdownService, QuickMarkdownService>();
2026-06-21 07:52:03 +02:00
var smtpSettings = new SmtpSettings();
builder.Configuration.GetSection("Smtp").Bind(smtpSettings);
builder.Services.AddSingleton(smtpSettings);
2026-05-25 06:38:16 +02:00
builder.Services.AddScoped<ILocalizationService, LocalizationService>();
2026-05-21 11:04:42 +02:00
builder.Services.AddScoped<CustomAuthStateProvider>();
builder.Services.AddScoped<AuthenticationStateProvider>(sp =>
sp.GetRequiredService<CustomAuthStateProvider>());
builder.Services.AddAuthorizationCore();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/login";
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromDays(30);
});
2026-05-14 12:31:35 +02:00
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}
2026-05-21 11:04:42 +02:00
2026-05-14 12:31:35 +02:00
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.UseAntiforgery();
2026-06-09 19:11:25 +02:00
var assetsDir = Path.Combine(app.Environment.ContentRootPath, "Assets");
if (Directory.Exists(assetsDir))
2026-05-25 06:38:16 +02:00
{
2026-06-09 19:11:25 +02:00
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(assetsDir),
RequestPath = "/Assets"
});
}
2026-05-25 06:38:16 +02:00
2026-05-14 12:31:35 +02:00
app.MapStaticAssets();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
2026-05-21 11:04:42 +02:00
2026-06-21 07:52:03 +02:00
static void MigrateUserRoles(ILiteDatabase db)
{
var col = db.GetCollection("users");
var docs = col.FindAll().ToList();
foreach (var doc in docs)
{
if (doc.ContainsKey("Role") && !doc.ContainsKey("Roles"))
{
var oldRole = (Int32)UserRole.Administrator;
doc.Remove("Role");
doc["Roles"] = new BsonArray { new BsonValue(oldRole) };
col.Update(doc);
}
else if (!doc.ContainsKey("Roles") && !doc.ContainsKey("Role"))
{
doc["Roles"] = new BsonArray { new BsonValue(0) };
col.Update(doc);
}
}
}