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 Microsoft.OpenApi.Models; using MyCSharp.HttpUserAgentParser.AspNetCore.DependencyInjection; using MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection; using Serilog; namespace Galaeth.ApiServer; /// /// Entry point for service. /// internal static class Program { /// /// Entry point method. /// /// Service arguments. 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() .WriteTo.Console() .CreateLogger(); builder.Services.AddSerilog(); builder.Services.AddHttpUserAgentMemoryCachedParser(opt => { opt.CacheEntryOptions.SlidingExpiration = TimeSpan.FromMinutes(30); opt.CacheOptions.SizeLimit = 1024; }).AddHttpUserAgentParserAccessor(); builder.Services.AddHostedService(); builder.Services.AddGalaethCore(); builder.Services.AddGalaethApiServer(); builder.Services.AddGalaethServices(); builder.Services.AddGalaethDAL(); builder.Services.AddHttpContextAccessor(); builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(setup => { var jwtSecurityScheme = new OpenApiSecurityScheme { BearerFormat = "JWT", Name = "JWT Authentication", In = ParameterLocation.Header, Type = SecuritySchemeType.Http, Scheme = JwtBearerDefaults.AuthenticationScheme, Description = "Paste your access token below", Reference = new OpenApiReference { Id = JwtBearerDefaults.AuthenticationScheme, Type = ReferenceType.SecurityScheme, }, }; setup.AddSecurityDefinition(jwtSecurityScheme.Reference.Id, jwtSecurityScheme); setup.AddSecurityRequirement(new OpenApiSecurityRequirement { { jwtSecurityScheme, Array.Empty() }, }); }); // 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(builder.Configuration.GetSection("Database")); builder.Services.Configure(builder.Configuration.GetSection("PwdHash")); builder.Services.Configure(builder.Configuration.GetSection("InitialUser")); builder.Services.Configure(builder.Configuration.GetSection("Registration")); builder.Services.Configure( builder.Configuration.GetSection("EmailDomainBlacklist")); builder.Services.Configure(builder.Configuration.GetSection("JWT")); var app = builder.Build(); // Error handling middleware. app.UseMiddleware(); app.UseMiddleware(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseAuthorization(); app.MapControllers(); await app.RunAsync(); } }