Add project files.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
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 pri štarte
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||
db.Database.Migrate();
|
||||
}
|
||||
|
||||
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();
|
||||
Reference in New Issue
Block a user