PIAPS_CW/RestAPI/Program.cs
2024-06-22 18:42:11 +04:00

76 lines
2.3 KiB
C#

using BusinessLogic.BusinessLogic;
using BusinessLogic.Tools;
using BusinessLogic.Tools.Mail;
using Contracts.BusinessLogicContracts;
using Contracts.StorageContracts;
using DatabaseImplement.Implements;
using Microsoft.OpenApi.Models;
using System;
using System.Net.Mail;
const string VERSION = "v1";
const string TITLE = "21GunsRestAPI";
var builder = WebApplication.CreateBuilder(args);
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Logging.AddLog4Net("log4net.config");
#region DI
builder.Services.AddTransient<IRoleLogic, RoleLogic>();
builder.Services.AddTransient<IUserLogic, UserLogic>();
builder.Services.AddSingleton<ITwoFactorAuthService, TwoFactorAuthService>();
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
builder.Services.AddTransient<IUserStorage, UserStorage>();
builder.Services.AddSingleton<JwtProvider>();
builder.Services.AddSingleton<MailSender>();
#endregion DI
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc(VERSION, new OpenApiInfo { Title = TITLE, Version = VERSION });
});
var app = builder.Build();
var jwtProvider = app.Services.GetService<JwtProvider>();
var mailSender = app.Services.GetService<MailSender>();
#region Setup config
string? getSection(string section) => builder.Configuration?.GetSection(section)?.Value?.ToString();
jwtProvider?.SetupJwtOptions(new()
{
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
});
mailSender?.SetupMailOptions(new()
{
Email = getSection("MailOptions:Email") ?? string.Empty,
Password = getSection("MailOptions:Password") ?? string.Empty,
SmtpClientHost = getSection("MailOptions:SmtpClientHost") ?? string.Empty,
SmtpClientPort = Convert.ToInt16(getSection("MailOptions:SmtpClientPort"))
});
#endregion Setup config
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{VERSION}/swagger.json", $"{TITLE} {VERSION}"));
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();