-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
96 lines (83 loc) · 3.71 KB
/
Program.cs
File metadata and controls
96 lines (83 loc) · 3.71 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using ModuWeb.Cors;
using ModuWeb.Extensions;
using ModuWeb.SessionSystem;
using ModuWeb.Storage;
using ModuWeb.ViewEngine;
namespace ModuWeb;
internal class Program
{
internal static void Main(string[] args)
{
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.AddSingleton<ICorsPolicyProvider, DynamicCorsPolicyProvider>();
builder.Services.AddSingleton<IStorageService>(provider =>
{
var baseDbPathConfig = builder.Configuration["BaseDbPath"] ?? "db";
var normalizedPath = baseDbPathConfig.Replace('/', Path.DirectorySeparatorChar)
.Replace('\\', Path.DirectorySeparatorChar)
.Trim(Path.DirectorySeparatorChar);
var dbPath = Path.Combine(builder.Environment.ContentRootPath, normalizedPath,
"storage.db");
return new LiteDbStorageService(dbPath);
});
builder.Services.AddSingleton<ISessionService>(provider =>
{
var storage = provider.GetRequiredService<IStorageService>();
var config = provider.GetRequiredService<IConfiguration>();
return new LiteDbSessionService(storage, config);
});
builder.Services.AddSingleton<IModuleViewEngine, ModuleViewEngine>();
builder.Services.AddCors();
var url = builder.Configuration["Url"] ?? "http://*:5000";
builder.WebHost.UseUrls(url);
if (builder.Configuration.GetValue<bool>("UseHttps"))
{
builder.WebHost.UseKestrelHttpsConfiguration();
builder.WebHost.ConfigureKestrel(options =>
{
var certPath = builder.Configuration["Certificate:Path"];
var keyPath = builder.Configuration["Certificate:KeyPath"];
if (!string.IsNullOrEmpty(certPath) && !string.IsNullOrEmpty(keyPath))
{
options.ConfigureHttpsDefaults(https =>
{
https.ServerCertificate = X509Certificate2.CreateFromPemFile(certPath, keyPath);
});
}
});
}
builder.Services.Configure<JsonOptions>(options => options.JsonSerializerOptions());
builder.Services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = builder.Configuration.GetValue<int>("MaxRequestBodySize") * 1024 * 1024;
});
builder.Services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = builder.Configuration.GetValue<int>("MaxRequestBodySize") * 1024 * 1024;
});
var app = builder.Build();
app.UseCors();
var modulesPath = Path.Combine(builder.Environment.ContentRootPath, "modules");
ModuleManager.Instance = new(modulesPath,
builder.Configuration.GetSection("LoadOrder").Get<string[]>() ?? Array.Empty<string>(), app.Services);
Logger.Info($"Module base path: `{builder.Configuration["BaseApiPath"]}`");
app.UseStaticFiles();
app.UseMiddleware<ModuleCorsGuardMiddleware>();
app.UseMiddleware<ModuleMiddleware>(builder.Configuration["BaseApiPath"]);
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404)
{
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Module not found");
}
});
app.Run();
}
}