PIAPS_CW/RestAPI/Program.cs

93 lines
3.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<ISaleLogic, SaleLogic>();
2024-06-05 01:25:18 +04:00
builder.Services.AddTransient<IUserLogic, UserLogic>();
2024-06-25 15:03:16 +04:00
builder.Services.AddTransient<IProductLogic, ProductLogic>();
builder.Services.AddTransient<ISellLogic, SellLogic>();
builder.Services.AddTransient<IPurchaseLogic, PurchaseLogic>();
2024-06-25 20:26:34 +04:00
builder.Services.AddTransient<IMediaFileLogic, MediaFileLogic>();
builder.Services.AddTransient<ICartItemLogic, CartItemLogic>();
builder.Services.AddTransient<IBillLogic, BillLogic>();
2024-06-25 15:03:16 +04:00
2024-06-22 18:42:11 +04:00
builder.Services.AddSingleton<ITwoFactorAuthService, TwoFactorAuthService>();
2024-06-05 01:25:18 +04:00
builder.Services.AddTransient<IRoleStorage, RoleStorage>();
builder.Services.AddTransient<ISaleStorage, SaleStorage>();
2024-06-05 01:25:18 +04:00
builder.Services.AddTransient<IUserStorage, UserStorage>();
2024-06-25 15:03:16 +04:00
builder.Services.AddTransient<IProductStorage, ProductStorage>();
builder.Services.AddTransient<ISellStorage, SellStorage>();
builder.Services.AddTransient<IPurchaseStorage, PurchaseStorage>();
2024-06-25 20:26:34 +04:00
builder.Services.AddTransient<IMediaFileStorage, MediaFileStorage>();
builder.Services.AddTransient<ICartItemStorage, CartItemStorage>();
2024-06-05 01:25:18 +04:00
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 =>
{
2024-06-22 18:42:11 +04:00
c.SwaggerDoc(VERSION, new OpenApiInfo { Title = TITLE, Version = VERSION });
2024-06-05 01:25:18 +04:00
});
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()
{
2024-06-22 18:42:11 +04:00
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
2024-06-15 00:01:39 +04:00
});
mailSender?.SetupMailOptions(new()
{
2024-06-22 18:42:11 +04:00
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
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
2024-05-09 17:45:03 +04:00
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
2024-06-22 18:42:11 +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();
app.UseDeveloperExceptionPage();
2024-05-09 17:45:03 +04:00
2024-06-05 01:25:18 +04:00
app.Run();