Files
BimAI/BimAI.API/Program.cs

118 lines
3.7 KiB
C#
Raw Normal View History

2025-10-11 11:33:46 +02:00
using System.Text;
using BimAI.API.Services;
using BimAI.Infrastructure.Data;
2025-10-12 18:28:14 +02:00
using BimAI.Infrastructure.Jobs;
2025-10-11 11:33:46 +02:00
using BimAI.Infrastructure.Sync;
2025-10-12 18:28:14 +02:00
using Hangfire;
using Hangfire.SqlServer;
2025-10-11 11:33:46 +02:00
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<BimAIDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddScoped<ProductSyncService>();
builder.Services.AddHttpClient();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
2025-10-12 18:28:14 +02:00
// Start Hangfire section
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"),
new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.Zero,
UseRecommendedIsolationLevel = true,
DisableGlobalLocks = true,
SchemaName = "Hangfire"
}
)
);
builder.Services.AddHangfireServer(options =>
{
options.ServerName = builder.Configuration["Hangfire:ServerName"];
options.WorkerCount = builder.Configuration.GetValue<int>("Hangfire:WorkerCount", 5);
});
// End Hangfire section
2025-10-11 11:33:46 +02:00
// Start auth section
var jwtSettings = builder.Configuration.GetSection("JwtSettings");
var secretKey = jwtSettings["SecretKey"];
var issuer = jwtSettings["Issuer"];
var audience = jwtSettings["Audience"];
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = issuer,
ValidAudience = audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secretKey)),
ClockSkew = TimeSpan.Zero,
};
});
builder.Services.AddAuthentication();
builder.Services.AddScoped<GoogleAuthService>();
builder.Services.AddScoped<JwtTokenService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// End auth section
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseCors("AllowAll");
2025-10-12 18:28:14 +02:00
app.UseHangfireDashboard(builder.Configuration["Hangfire:DashboardPath"] ?? "/hangfire", new DashboardOptions
{
AsyncAuthorization = new[] { new HangfireAuthorizationFilter() },
DashboardTitle = "BimAI - Job Dashboard"
});
2025-10-11 11:33:46 +02:00
app.UseAuthorization();
app.UseAuthorization();
app.MapControllers();
2025-10-12 18:28:14 +02:00
2025-10-12 20:17:33 +02:00
app.MapGet("/health", () => Results.Ok(new { status = "OK", timestamp = DateTime.UtcNow }))
.AllowAnonymous();
2025-10-12 18:28:14 +02:00
RecurringJob.AddOrUpdate<ProductSyncJob>(
"product-sync",
job => job.ExecuteAsync(),
Cron.Daily(2, 0), // Every day at 2:00 AM
new RecurringJobOptions
{
2025-10-12 20:17:33 +02:00
TimeZone = TimeZoneInfo.Local,
MisfireHandling = app.Environment.IsDevelopment()
? MisfireHandlingMode.Relaxed
: MisfireHandlingMode.Strict
2025-10-12 18:28:14 +02:00
});
app.Run();