galaeth-draft/Galaeth.ApiServer/Program.cs
2024-11-17 09:31:01 +00:00

92 lines
3.5 KiB
C#

using Galaeth.ApiServer.Extensions;
using Galaeth.ApiServer.HostedServices;
using Galaeth.ApiServer.Middleware;
using Galaeth.Core.Configuration;
using Galaeth.Services.Configuration;
using Galaeth.Services.Profiles;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using MyCSharp.HttpUserAgentParser.AspNetCore.DependencyInjection;
using MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection;
using Serilog;
using Serilog.Events;
namespace Galaeth.ApiServer;
/// <summary>
/// Entry point for service.
/// </summary>
internal static class Program
{
/// <summary>
/// Entry point method.
/// </summary>
/// <param name="args">Service arguments.</param>
public static async Task Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Configuration
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.MinimumLevel.Override("Default", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.MinimumLevel.Override("System", LogEventLevel.Warning)
.WriteTo.Console()
.CreateLogger();
builder.Services.AddSerilog();
builder.Services.AddHttpUserAgentMemoryCachedParser(opt =>
{
opt.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(30);
opt.CacheOptions.SizeLimit = 1024;
}).AddHttpUserAgentParserAccessor();
builder.Services.AddHostedService<StartupService>();
builder.Services.AddGalaethCore();
builder.Services.AddGalaethApiServer();
builder.Services.AddGalaethServices();
builder.Services.AddGalaethDAL();
builder.Services.AddHttpContextAccessor();
builder.Services.AddControllers();
// Setup authentication.
var jwtConfig = new JwtConfiguration();
builder.Configuration.Bind("JWT", jwtConfig);
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options => options.TokenValidationParameters = jwtConfig.ToTokenValidationParameters());
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
// Setup automapping.
builder.Services.AddAutoMapper(typeof(AutomapperProfile).Assembly);
// Setup configuration.
builder.Services.Configure<DatabaseConfiguration>(builder.Configuration.GetSection("Database"));
builder.Services.Configure<PwdHashConfiguration>(builder.Configuration.GetSection("PwdHash"));
builder.Services.Configure<InitialUserConfiguration>(builder.Configuration.GetSection("InitialUser"));
builder.Services.Configure<RegistrationConfiguration>(builder.Configuration.GetSection("Registration"));
builder.Services.Configure<EmailDomainBlacklistConfiguration>(
builder.Configuration.GetSection("EmailDomainBlacklist"));
builder.Services.Configure<JwtConfiguration>(builder.Configuration.GetSection("JWT"));
var app = builder.Build();
// Error handling middleware.
app.UseMiddleware<ExceptionMiddleware>();
app.UseMiddleware<StatusCodeMiddleware>();
app.UseAuthorization();
app.MapControllers();
await app.RunAsync();
}
}