112 lines
3.9 KiB
C#
112 lines
3.9 KiB
C#
using AuthorBuddy.Web.Components;
|
|
using AuthorBuddy.Web.Data;
|
|
using AuthorBuddy.Web.Models;
|
|
using AuthorBuddy.Web.Services;
|
|
using LiteDB;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.WebHost.UseWebRoot(Path.Combine(builder.Environment.ContentRootPath, "wwwroot"));
|
|
|
|
builder.Services.AddRazorComponents()
|
|
.AddInteractiveServerComponents();
|
|
|
|
var dbDir = OperatingSystem.IsLinux()
|
|
? Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"author-buddy")
|
|
: AppContext.BaseDirectory;
|
|
Directory.CreateDirectory(dbDir);
|
|
var dbPath = Path.Combine(dbDir, Constants.DbFile);
|
|
var db = new LiteDatabase($"Filename={dbPath};Connection=direct");
|
|
MigrateUserRoles(db);
|
|
builder.Services.AddSingleton<ILiteDatabase>(db);
|
|
|
|
builder.Services.AddSingleton<OllamaSettings>();
|
|
builder.Services.AddSingleton<IOllamaService, OllamaService>();
|
|
builder.Services.AddSingleton<ILLMBackend, OllamaBackend>();
|
|
builder.Services.AddSingleton<ILLMBackend, LlamaCppBackend>();
|
|
builder.Services.AddSingleton<IOllamaSetupService, OllamaSetupService>();
|
|
builder.Services.AddSingleton<IFileScannerService, FileScannerService>();
|
|
builder.Services.AddScoped<ISettingsService, SettingsService>();
|
|
builder.Services.AddScoped<IThemeService, ThemeService>();
|
|
builder.Services.AddSingleton<IProjectService, ProjectService>();
|
|
builder.Services.AddSingleton<IExportService, ExportService>();
|
|
builder.Services.AddSingleton<IRagService, RagService>();
|
|
builder.Services.AddSingleton<IWikipediaService, WikipediaService>();
|
|
builder.Services.AddSingleton<OperationState>();
|
|
builder.Services.AddSingleton<IDbAdminService, DbAdminService>();
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddSingleton<IEmailService, EmailService>();
|
|
builder.Services.AddSingleton<IChatService, ChatService>();
|
|
builder.Services.AddSingleton<IQuickMarkdownService, QuickMarkdownService>();
|
|
|
|
var smtpSettings = new SmtpSettings();
|
|
builder.Configuration.GetSection("Smtp").Bind(smtpSettings);
|
|
builder.Services.AddSingleton(smtpSettings);
|
|
builder.Services.AddScoped<ILocalizationService, LocalizationService>();
|
|
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);
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
app.UseExceptionHandler("/Error", createScopeForErrors: true);
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
|
|
app.UseAntiforgery();
|
|
|
|
var assetsDir = Path.Combine(app.Environment.ContentRootPath, "Assets");
|
|
if (Directory.Exists(assetsDir))
|
|
{
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
FileProvider = new PhysicalFileProvider(assetsDir),
|
|
RequestPath = "/Assets"
|
|
});
|
|
}
|
|
|
|
app.MapStaticAssets();
|
|
app.MapRazorComponents<App>()
|
|
.AddInteractiveServerRenderMode();
|
|
|
|
app.Run();
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|