137 lines
4.8 KiB
C#
137 lines
4.8 KiB
C#
using MessengerApi.Configuration.Sources.Environment;
|
|
using MessengerBroker.Configuration.Model;
|
|
using MessengerBroker.Factories;
|
|
using MessengerBroker.Handlers;
|
|
using MessengerBroker.Handlers.Endpoint;
|
|
using MessengerBroker.Model.Http;
|
|
using MessengerBroker.Models.Scoped;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Net;
|
|
|
|
namespace MessengerBroker
|
|
{
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
BrokerConfiguration configuration = null;
|
|
|
|
try
|
|
{
|
|
configuration = new BrokerConfiguration(new EnvironmentConfigurationSource());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine("Can't load settings.", ex);
|
|
throw;
|
|
}
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddMemoryCache();
|
|
builder.Services.AddSingleton<ILogger, ConsoleLogger>();
|
|
builder.Services.AddSingleton<BrokerConfiguration>(configuration);
|
|
builder.Services.AddSingleton<BrokerDbContextFactory>();
|
|
builder.Services.AddSingleton<MessengerApi.Factories.DbContextFactory>((sp) =>
|
|
{
|
|
return new MessengerApi.Factories.DbContextFactory(configuration.ApiPersistenceConfiguration);
|
|
});
|
|
builder.Services.AddSingleton<HousekeepingHandler>();
|
|
builder.Services.AddSingleton<MasterServerSynchronizationHandler>();
|
|
|
|
builder.Services.AddScoped<Identity>();
|
|
builder.Services.AddScoped<UsersEndpointHandler>();
|
|
builder.Services.AddScoped<MessagesEndpointHandler>();
|
|
|
|
// Authentication.
|
|
builder.Services
|
|
.AddAuthentication("Bearer")
|
|
.AddScheme<AuthenticationSchemeOptions, CustomBearerAuthenticationHandler>("Bearer", null);
|
|
|
|
// Proxy registration to forward real client IPs.
|
|
builder.Services.Configure<ForwardedHeadersOptions>(options =>
|
|
{
|
|
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
|
|
|
|
foreach (var proxy in configuration.Proxies)
|
|
{
|
|
options.KnownProxies.Add(IPAddress.Parse(proxy));
|
|
}
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// DB Migrations
|
|
using (var ctx = app.Services.GetRequiredService<BrokerDbContextFactory>().CreateDbContext())
|
|
{
|
|
var migrationLogger = app.Services.GetRequiredService<ILogger>();
|
|
|
|
try
|
|
{
|
|
if (ctx.Database.GetPendingMigrations().Any())
|
|
{
|
|
migrationLogger.Info("Applying migrations.");
|
|
ctx.Database.Migrate();
|
|
}
|
|
else
|
|
{
|
|
migrationLogger.Info("No migrations pending.");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
migrationLogger.Error("Can't run migrations successfully.", ex);
|
|
throw;
|
|
}
|
|
}
|
|
|
|
// Housekeeping.
|
|
if (configuration.HousekeepingEnabled)
|
|
{
|
|
_ = app.Services.GetRequiredService<HousekeepingHandler>().BeginHousekeeping();
|
|
}
|
|
|
|
//// Run pull sync from masters.
|
|
if(configuration.MasterServers.Any())
|
|
{
|
|
var handler = app.Services.GetRequiredService<MasterServerSynchronizationHandler>();
|
|
var logger = app.Services.GetRequiredService<ILogger>();
|
|
|
|
foreach (var server in configuration.MasterServers)
|
|
{
|
|
logger.Info($"Starting sync task for {server}.");
|
|
_ = handler.BeginSyncing(server);
|
|
}
|
|
}
|
|
|
|
app.UseStaticFiles();
|
|
app.UseRouting();
|
|
app.UseAuthentication();
|
|
app.UseForwardedHeaders();
|
|
|
|
app.MapGet("/users", async (UsersEndpointHandler handler) =>
|
|
{
|
|
var response = await handler.GetUsers();
|
|
return Results.Json(response);
|
|
});
|
|
|
|
app.MapGet("/messages", async (
|
|
MessagesEndpointHandler handler,
|
|
[FromQuery] Guid ownerBrokerId, DateTime fromUtc) =>
|
|
{
|
|
var response = await handler.GetMessages(new Messages.MessagesRequest
|
|
{
|
|
OwnerBrokerId = ownerBrokerId,
|
|
SinceUtc = DateTime.MinValue
|
|
});
|
|
return Results.Json(response);
|
|
});
|
|
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|