110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using System.Text;
|
|
using Hospitality.Backend.Configuration;
|
|
using Hospitality.Backend.Services;
|
|
using Hospitality.Domain.Entities;
|
|
using Hospitality.Infrastructure.Data;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
builder.Services.AddDbContext<HospitalityDbContext>(options =>
|
|
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// Configure JWT settings
|
|
builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("JwtSettings"));
|
|
var jwtSettings = builder.Configuration.GetSection("JwtSettings").Get<JwtSettings>();
|
|
|
|
// Add Identity
|
|
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
|
|
{
|
|
options.Password.RequireDigit = true;
|
|
options.Password.RequireLowercase = true;
|
|
options.Password.RequireUppercase = true;
|
|
options.Password.RequireNonAlphanumeric = true;
|
|
options.Password.RequiredLength = 8;
|
|
})
|
|
.AddEntityFrameworkStores<HospitalityDbContext>()
|
|
.AddDefaultTokenProviders();
|
|
|
|
// Add JWT Authentication
|
|
builder.Services.AddAuthentication(options =>
|
|
{
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
})
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuer = true,
|
|
ValidateAudience = true,
|
|
ValidateLifetime = true,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidIssuer = jwtSettings!.Issuer,
|
|
ValidAudience = jwtSettings.Audience,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecretKey))
|
|
};
|
|
});
|
|
|
|
// Register services
|
|
builder.Services.AddScoped<IAuthService, AuthService>();
|
|
builder.Services.AddScoped<IEventService, EventService>();
|
|
builder.Services.AddScoped<IGroupService, GroupService>();
|
|
builder.Services.AddScoped<IProductService, ProductService>();
|
|
builder.Services.AddScoped<IQrCodeService, QrCodeService>();
|
|
builder.Services.AddScoped<ITransactionService, TransactionService>();
|
|
|
|
// Add CORS
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowFrontend", policy =>
|
|
{
|
|
policy.WithOrigins("http://localhost:5173")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
// Seed database with sample data
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var context = scope.ServiceProvider.GetRequiredService<HospitalityDbContext>();
|
|
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<ApplicationUser>>();
|
|
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
|
|
|
// Apply migrations
|
|
await context.Database.MigrateAsync();
|
|
|
|
await Hospitality.Backend.Data.DbSeeder.SeedAsync(context, userManager, roleManager);
|
|
}
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseCors("AllowFrontend");
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|