using BakeryErp.Infrastructure.Database; using BakeryErp.Infrastructure.Repositories; using BakeryErp.Infrastructure.Services; using BakeryErp.Web; var builder = WebApplication.CreateBuilder(args); var databaseDirectory = builder.Configuration["BakeryErp:DataDirectory"] ?? Environment.GetEnvironmentVariable("BAKERY_ERP_DATA_DIR") ?? Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".bakery-erp"); Directory.CreateDirectory(databaseDirectory); var databasePath = Path.Combine(databaseDirectory, "bakery-erp.local.json"); var attachmentDirectory = Path.Combine(databaseDirectory, "attachments"); Directory.CreateDirectory(attachmentDirectory); var databaseOptions = new DatabaseOptions { DatabasePath = databasePath }; builder.Services.AddRazorPages(); builder.Services.AddSingleton(databaseOptions); builder.Services.AddSingleton(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddSingleton(new AppRuntimeInfo(databasePath)); var app = builder.Build(); using (var scope = app.Services.CreateScope()) { scope.ServiceProvider.GetRequiredService().Initialize(); } if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseStaticFiles(new StaticFileOptions { FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider( attachmentDirectory), RequestPath = "/attachments" }); app.UseRouting(); app.MapRazorPages(); app.Run();