PIAPS_CW/RestAPI/Program.cs

75 lines
2.2 KiB
C#
Raw Normal View History

2024-06-05 01:25:18 +04:00
using BusinessLogic.BusinessLogic;
2024-06-15 00:01:39 +04:00
using BusinessLogic.Tools;
using BusinessLogic.Tools.Mail;
2024-06-05 01:25:18 +04:00
using Contracts.BusinessLogicContracts;
using Contracts.StorageContracts;
using DatabaseImplement.Implements;
using Microsoft.OpenApi.Models;
using System;
2024-06-15 00:01:39 +04:00
using System.Net.Mail;
2024-06-05 01:25:18 +04:00
const string VERSION = "v1";
const string TITLE = "21GunsRestAPI";
2024-05-09 17:45:03 +04:00
var builder = WebApplication.CreateBuilder(args);
2024-06-05 01:25:18 +04:00
builder.Logging.SetMinimumLevel(LogLevel.Trace);
builder.Logging.AddLog4Net("log4net.config");
#region DI
builder.Services.AddTransient<IRoleLogic, RoleLogic>();
builder.Services.AddTransient<IUserLogic, UserLogic>();
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
builder.Services.AddTransient<IUserStorage, UserStorage>();
2024-06-15 00:01:39 +04:00
builder.Services.AddSingleton<JwtProvider>();
builder.Services.AddSingleton<MailSender>();
2024-06-05 01:25:18 +04:00
#endregion DI
2024-05-09 17:45:03 +04:00
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
2024-06-05 01:25:18 +04:00
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc(VERSION, new OpenApiInfo { Title = TITLE, Version = VERSION });
});
2024-05-09 17:45:03 +04:00
var app = builder.Build();
2024-06-15 00:01:39 +04:00
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"))
});
2024-06-15 00:01:39 +04:00
#endregion Setup config
2024-05-09 17:45:03 +04:00
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
2024-06-05 01:25:18 +04:00
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint($"/swagger/{VERSION}/swagger.json", $"{TITLE} {VERSION}"));
2024-05-09 17:45:03 +04:00
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
2024-06-05 01:25:18 +04:00
app.Run();