73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using CalendarReminder.Api.Data;
|
|
using CalendarReminder.Api.Services;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using System.Text;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Controllers + OpenAPI
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddOpenApi();
|
|
|
|
// MariaDB / EF Core
|
|
var connStr = builder.Configuration.GetConnectionString("Default");
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseMySql(connStr, ServerVersion.AutoDetect(connStr)));
|
|
|
|
// JWT autentifikácia
|
|
var jwtSecret = builder.Configuration["Jwt:Secret"]!;
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
ValidateIssuerSigningKey = true,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret)),
|
|
ValidateIssuer = true,
|
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|
ValidateAudience = true,
|
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|
ValidateLifetime = true
|
|
};
|
|
});
|
|
builder.Services.AddAuthorization();
|
|
|
|
// Services
|
|
builder.Services.AddScoped<TokenService>();
|
|
builder.Services.AddScoped<EmailService>();
|
|
builder.Services.AddHostedService<ReminderNotificationService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Auto-migrate + seed admin
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
db.Database.Migrate();
|
|
|
|
if (!db.Users.Any())
|
|
{
|
|
db.Users.Add(new CalendarReminder.Domain.Entities.User
|
|
{
|
|
Username = "admin",
|
|
Email = "nas@culak.sk",
|
|
PasswordHash = BCrypt.Net.BCrypt.HashPassword("Admin@2025"),
|
|
IsAdmin = true
|
|
});
|
|
db.SaveChanges();
|
|
}
|
|
}
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
app.MapOpenApi();
|
|
|
|
app.UseDefaultFiles(); // servuje index.html z wwwroot
|
|
app.UseStaticFiles(); // frontend súbory
|
|
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
app.MapControllers();
|
|
app.Run();
|