File size: 2,260 Bytes
13240d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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<DatabaseInitializer>();
builder.Services.AddScoped<DashboardRepository>();
builder.Services.AddScoped<SupplierRepository>();
builder.Services.AddScoped<ItemRepository>();
builder.Services.AddScoped<StoreRepository>();
builder.Services.AddScoped<WarehouseRepository>();
builder.Services.AddScoped<MasterDataLookupRepository>();
builder.Services.AddScoped<OcrDocumentRepository>();
builder.Services.AddScoped<InventoryRepository>();
builder.Services.AddScoped<PurchaseRepository>();
builder.Services.AddScoped<PettyCashRepository>();
builder.Services.AddScoped<TransferRepository>();
builder.Services.AddScoped<StoreReturnRepository>();
builder.Services.AddScoped<ReportCenterRepository>();
builder.Services.AddScoped<OcrSmartParser>();
builder.Services.AddHttpClient<OpenAiVisionOcrService>();
builder.Services.AddSingleton(new AppRuntimeInfo(databasePath));

var app = builder.Build();

using (var scope = app.Services.CreateScope())
{
    scope.ServiceProvider.GetRequiredService<DatabaseInitializer>().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();