Files
PIBD-23_Coursach_YouAreProg…/YouAreProgrammerShop/YAPWebApplication/Program.cs

128 lines
4.7 KiB
C#

using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Serilog;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using YAPBusinessLogic.Implementations;
using YAPContracts.AdapterContracts;
using YAPContracts.BusinessLogicContracts;
using YAPContracts.Infrastructure;
using YAPContracts.StorageContracts;
using YAPDatabase;
using YAPDatabase.Implementations;
using YAPWebApplication;
using YAPWebApplication.Adapters;
using YAPWebApplication.Infrastructure;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddControllers();
using var loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger());
builder.Services.AddSingleton(loggerFactory.CreateLogger("Any"));
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.Cookie.Name = "YAPAuth";
options.ExpireTimeSpan = TimeSpan.FromDays(7);
options.SlidingExpiration = true;
});
builder.Services.AddAuthorization(options =>
{
options.AddPolicy("WorkerOnly", policy => policy.RequireRole("Worker"));
options.AddPolicy("StorekeeperOnly", policy => policy.RequireRole("Storekeeper"));
});
builder.Services.AddRazorPages(options =>
{
// authorize all pages by default
options.Conventions.AuthorizeFolder("/");
// anonimous access to the account pages
options.Conventions.AllowAnonymousToPage("/Account/Login");
options.Conventions.AllowAnonymousToPage("/Account/Register");
options.Conventions.AllowAnonymousToPage("/Account/AccessDenied");
});
builder.Services.AddSingleton<IConfigurationDatabase, DatabaseConfiguration>();
builder.Services.AddTransient<YAPDbContext>();
builder.Services.AddTransient<ICommentBusinessLogicContract, CommentBusinessLogicContract>();
builder.Services.AddTransient<IComponentBusinessLogicContract, ComponentBusinessLogicContract>();
builder.Services.AddTransient<IPurchaseBusinessLogicContract, PurchaseBusinessLogicContract>();
builder.Services.AddTransient<IProductOrderBusinessLogicContract, ProductOrderBusinessLogicContract>();
builder.Services.AddTransient<IProductBusinessLogicContract, ProductBusinessLogicContract>();
builder.Services.AddTransient<IProductSetBusinessLogicContract, ProductSetBusinessLogicContract>();
builder.Services.AddTransient<IWorkerBusinessLogicContract, WorkerBusinessLogicContract>();
builder.Services.AddTransient<IStorekeeperBusinessLogicContract, StorekeeperBusinessLogicContract>();
builder.Services.AddTransient<ICommentStorageContract, CommentStorageContract>();
builder.Services.AddTransient<IComponentStorageContract, ComponentStorageContract>();
builder.Services.AddTransient<IPurchaseStorageContract, PurchaseStorageContract>();
builder.Services.AddTransient<IProductOrderStorageContract, ProductOrderStorageContract>();
builder.Services.AddTransient<IProductStorageContract, ProductStorageContract>();
builder.Services.AddTransient<IProductSetStorageContract, ProductSetStorageContract>();
builder.Services.AddTransient<IWorkerStorageContract, WorkerStorageContract>();
builder.Services.AddTransient<IStorekeeperStorageContract, StorekeeperStorageContract>();
builder.Services.AddTransient<IProductAdapter, ProductAdapter>();
builder.Services.AddTransient<ICommentAdapter, CommentAdapter>();
builder.Services.AddTransient<IComponentAdapter, ComponentAdapter>();
builder.Services.AddTransient<IProductSetAdapter, ProductSetAdapter>();
builder.Services.AddTransient<IProductOrderAdapter, ProductOrderAdapter>();
builder.Services.AddTransient<IPurchaseAdapter, PurchaseAdapter>();
builder.Services.AddTransient<IWorkerAdapter, WorkerAdapter>();
builder.Services.AddTransient<IStorekeeperAdapter, StorekeeperAdapter>();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
if (app.Environment.IsProduction())
{
app.UseExceptionHandler("/Error");
var dbContext = app.Services.GetRequiredService<YAPDbContext>();
if (dbContext.Database.CanConnect())
{
dbContext.Database.EnsureCreated();
dbContext.Database.Migrate();
}
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseStaticFiles();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapStaticAssets();
app.MapRazorPages()
.WithStaticAssets();
app.Run();