2024-05-14 22:24:53 +02:00
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
using Serilog;
|
|
|
|
using Foxchat.Core;
|
|
|
|
using Foxchat.Chat;
|
|
|
|
using Foxchat.Chat.Database;
|
2024-05-21 17:45:35 +02:00
|
|
|
using Foxchat.Chat.Extensions;
|
2024-05-22 02:31:05 +02:00
|
|
|
using Foxchat.Core.Extensions;
|
2024-05-21 16:41:01 +02:00
|
|
|
using Newtonsoft.Json;
|
2024-05-14 22:24:53 +02:00
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
var config = builder.AddConfiguration<InstanceConfig>("chat.ini");
|
|
|
|
|
2024-05-22 02:31:05 +02:00
|
|
|
builder.AddSerilog();
|
2024-05-14 22:24:53 +02:00
|
|
|
|
|
|
|
await BuildInfo.ReadBuildInfo();
|
|
|
|
Log.Information("Starting Foxchat.Chat {Version} ({Hash})", BuildInfo.Version, BuildInfo.Hash);
|
|
|
|
|
2024-05-21 16:41:01 +02:00
|
|
|
// Set the default converter to snake case as we use it in a couple places.
|
|
|
|
JsonConvert.DefaultSettings = () => new JsonSerializerSettings
|
|
|
|
{
|
|
|
|
ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-05-14 22:24:53 +02:00
|
|
|
builder.Services
|
|
|
|
.AddControllers()
|
|
|
|
.AddNewtonsoftJson(options =>
|
|
|
|
options.SerializerSettings.ContractResolver = new DefaultContractResolver
|
|
|
|
{
|
|
|
|
NamingStrategy = new SnakeCaseNamingStrategy()
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.Services
|
|
|
|
.AddCoreServices<ChatContext>()
|
2024-05-21 20:14:52 +02:00
|
|
|
.AddChatServices()
|
2024-05-21 17:45:35 +02:00
|
|
|
.AddCustomMiddleware()
|
2024-05-14 22:24:53 +02:00
|
|
|
.AddEndpointsApiExplorer()
|
|
|
|
.AddSwaggerGen();
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
app.UseSerilogRequestLogging();
|
|
|
|
app.UseRouting();
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
app.UseCors();
|
2024-05-21 17:45:35 +02:00
|
|
|
app.UseCustomMiddleware();
|
2024-05-14 22:24:53 +02:00
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
using (var scope = app.Services.CreateScope())
|
2024-05-21 16:41:01 +02:00
|
|
|
await using (var context = scope.ServiceProvider.GetRequiredService<ChatContext>())
|
2024-05-14 22:24:53 +02:00
|
|
|
{
|
|
|
|
Log.Information("Initializing instance keypair...");
|
|
|
|
if (await context.InitializeInstanceAsync())
|
|
|
|
{
|
|
|
|
Log.Information("Initialized instance keypair");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
app.Urls.Clear();
|
|
|
|
app.Urls.Add(config.Address);
|
|
|
|
|
|
|
|
app.Run();
|