2024-12-11 21:36:30 +01:00
|
|
|
// <copyright file="Program.cs" company="alveus.dev">
|
|
|
|
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
using Astral.ApiServer.Extensions;
|
|
|
|
using Astral.ApiServer.HostedService;
|
|
|
|
using Astral.ApiServer.Middleware;
|
|
|
|
using Astral.Core.Options;
|
|
|
|
using Astral.Services.Options;
|
|
|
|
using Astral.Services.Profiles;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
|
using MyCSharp.HttpUserAgentParser.AspNetCore.DependencyInjection;
|
|
|
|
using MyCSharp.HttpUserAgentParser.MemoryCache.DependencyInjection;
|
|
|
|
using Serilog;
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
builder.Configuration
|
|
|
|
.AddJsonFile("appsettings.json", true, true)
|
|
|
|
.AddJsonFile($"appsettings.{builder.Environment.EnvironmentName}.json", 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<StartupService>();
|
|
|
|
|
2024-12-14 18:48:03 +01:00
|
|
|
builder.Services.AddAstralApiServer();
|
2024-12-11 21:36:30 +01:00
|
|
|
builder.Services.AddAstralCore();
|
|
|
|
builder.Services.AddAstralServices();
|
|
|
|
builder.Services.AddAstralDAL();
|
|
|
|
|
|
|
|
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<string>() }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Setup authentication.
|
|
|
|
var jwtConfig = new JwtOptions();
|
|
|
|
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<DatabaseOptions>(builder.Configuration.GetSection("Database"));
|
|
|
|
builder.Services.Configure<PwdHashOptions>(builder.Configuration.GetSection("PwdHash"));
|
|
|
|
builder.Services.Configure<InitialUserOptions>(builder.Configuration.GetSection("InitialUser"));
|
|
|
|
builder.Services.Configure<RegistrationOptions>(builder.Configuration.GetSection("Registration"));
|
|
|
|
builder.Services.Configure<EmailDomainBlacklistOptions>(
|
|
|
|
builder.Configuration.GetSection("EmailDomainBlacklist"));
|
|
|
|
builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("JWT"));
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Error handling middleware.
|
|
|
|
app.UseMiddleware<ExceptionMiddleware>();
|
|
|
|
app.UseMiddleware<StatusCodeMiddleware>();
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
{
|
|
|
|
app.UseSwagger();
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
}
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
|
2024-12-14 18:48:03 +01:00
|
|
|
await app.RunAsync();
|