some cleanup
This commit is contained in:
@@ -1,29 +1,29 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Apis.Auth" Version="1.70.0" />
|
||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.21" />
|
||||
<PackageReference Include="Hangfire.Core" Version="1.8.21" />
|
||||
<PackageReference Include="Hangfire.SqlServer" Version="1.8.21" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.17">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.12.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BimAI.Application\BimAI.Application.csproj" />
|
||||
<ProjectReference Include="..\BimAI.Infrastructure\BimAI.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Apis.Auth" Version="1.70.0" />
|
||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.21" />
|
||||
<PackageReference Include="Hangfire.Core" Version="1.8.21" />
|
||||
<PackageReference Include="Hangfire.SqlServer" Version="1.8.21" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.17">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.12.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.10" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BimAI.Application\BimAI.Application.csproj" />
|
||||
<ProjectReference Include="..\BimAI.Infrastructure\BimAI.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
@BimAI.API_HostAddress = http://localhost:5090
|
||||
|
||||
GET {{BimAI.API_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@BimAI.API_HostAddress = http://localhost:5090
|
||||
|
||||
GET {{BimAI.API_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
|
||||
@@ -1,145 +1,145 @@
|
||||
using System.Text;
|
||||
using BimAI.API.Services;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using BimAI.Infrastructure.Jobs;
|
||||
using BimAI.Infrastructure.Sync;
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
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();
|
||||
|
||||
// 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
|
||||
// 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();
|
||||
|
||||
// Auto-apply migrations on startup
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<BimAIDbContext>();
|
||||
|
||||
try
|
||||
{
|
||||
var pending = await db.Database.GetPendingMigrationsAsync();
|
||||
if (pending.Any())
|
||||
{
|
||||
app.Logger.LogWarning("Applying {Count} pending migrations: {List}",
|
||||
pending.Count(), string.Join(", ", pending));
|
||||
await db.Database.MigrateAsync();
|
||||
app.Logger.LogInformation("Migrations applied successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
app.Logger.LogInformation("No pending migrations.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
app.Logger.LogCritical(ex, "Migration failed - application will not start.");
|
||||
throw; // stop startup
|
||||
}
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors("AllowAll");
|
||||
app.UseHangfireDashboard(builder.Configuration["Hangfire:DashboardPath"] ?? "/hangfire", new DashboardOptions
|
||||
{
|
||||
AsyncAuthorization = new[] { new HangfireAuthorizationFilter() },
|
||||
DashboardTitle = "BimAI - Job Dashboard"
|
||||
});
|
||||
app.UseAuthorization();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.MapGet("/health", () => Results.Ok(new { status = "OK", timestamp = DateTime.UtcNow }))
|
||||
.AllowAnonymous();
|
||||
|
||||
RecurringJob.AddOrUpdate<ProductSyncJob>(
|
||||
"product-sync",
|
||||
job => job.ExecuteAsync(),
|
||||
Cron.Daily(2, 0), // Every day at 2:00 AM
|
||||
new RecurringJobOptions
|
||||
{
|
||||
TimeZone = TimeZoneInfo.Local,
|
||||
MisfireHandling = app.Environment.IsDevelopment()
|
||||
? MisfireHandlingMode.Relaxed
|
||||
: MisfireHandlingMode.Strict
|
||||
});
|
||||
|
||||
using System.Text;
|
||||
using BimAI.API.Services;
|
||||
using BimAI.Infrastructure.Data;
|
||||
using BimAI.Infrastructure.Jobs;
|
||||
using BimAI.Infrastructure.Sync;
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
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();
|
||||
|
||||
// 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
|
||||
// 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();
|
||||
|
||||
// Auto-apply migrations on startup
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var db = scope.ServiceProvider.GetRequiredService<BimAIDbContext>();
|
||||
|
||||
try
|
||||
{
|
||||
var pending = await db.Database.GetPendingMigrationsAsync();
|
||||
if (pending.Any())
|
||||
{
|
||||
app.Logger.LogWarning("Applying {Count} pending migrations: {List}",
|
||||
pending.Count(), string.Join(", ", pending));
|
||||
await db.Database.MigrateAsync();
|
||||
app.Logger.LogInformation("Migrations applied successfully.");
|
||||
}
|
||||
else
|
||||
{
|
||||
app.Logger.LogInformation("No pending migrations.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
app.Logger.LogCritical(ex, "Migration failed - application will not start.");
|
||||
throw; // stop startup
|
||||
}
|
||||
}
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors("AllowAll");
|
||||
app.UseHangfireDashboard(builder.Configuration["Hangfire:DashboardPath"] ?? "/hangfire", new DashboardOptions
|
||||
{
|
||||
AsyncAuthorization = new[] { new HangfireAuthorizationFilter() },
|
||||
DashboardTitle = "BimAI - Job Dashboard"
|
||||
});
|
||||
app.UseAuthorization();
|
||||
app.UseAuthorization();
|
||||
app.MapControllers();
|
||||
|
||||
app.MapGet("/health", () => Results.Ok(new { status = "OK", timestamp = DateTime.UtcNow }))
|
||||
.AllowAnonymous();
|
||||
|
||||
RecurringJob.AddOrUpdate<ProductSyncJob>(
|
||||
"product-sync",
|
||||
job => job.ExecuteAsync(),
|
||||
Cron.Daily(2, 0), // Every day at 2:00 AM
|
||||
new RecurringJobOptions
|
||||
{
|
||||
TimeZone = TimeZoneInfo.Local,
|
||||
MisfireHandling = app.Environment.IsDevelopment()
|
||||
? MisfireHandlingMode.Relaxed
|
||||
: MisfireHandlingMode.Strict
|
||||
});
|
||||
|
||||
app.Run();
|
||||
@@ -1,14 +1,14 @@
|
||||
{
|
||||
"profiles": {
|
||||
"dev": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:7142;http://0.0.0.0:7142",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"profiles": {
|
||||
"dev": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:7142;http://0.0.0.0:7142",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
@@ -1,38 +1,25 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "#{db-connection-string}#",
|
||||
"HangfireConnection": "#{hangfire-connection-string}#"
|
||||
},
|
||||
"E5_CRM": {
|
||||
"ApiKey": "#{e5-crm-api-key}#"
|
||||
},
|
||||
"GoogleAuth": {
|
||||
"ClientId": "#{google-auth-client-id}#"
|
||||
},
|
||||
"JwtSettings": {
|
||||
"SecretKey": "#{jwt-secret-key}#",
|
||||
"Issuer": "#{jwt-issuer}#",
|
||||
"Audience": "#{jwt-audience}#",
|
||||
"ExpiryDays": 7
|
||||
},
|
||||
"Hangfire": {
|
||||
"ServerName": "#{hangfire-server-name}#",
|
||||
"WorkerCount": 5,
|
||||
"DashboardPath": "/hangfire"
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://0.0.0.0:7142"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "",
|
||||
"HangfireConnection": ""
|
||||
},
|
||||
"E5_CRM": {
|
||||
"ApiKey": ""
|
||||
},
|
||||
"GoogleAuth": {
|
||||
"ClientId": ""
|
||||
},
|
||||
"JwtSettings": {
|
||||
"SecretKey": ""
|
||||
},
|
||||
"Hangfire": {
|
||||
"ServerName": "BimAI-Production"
|
||||
},
|
||||
"Kestrel": {
|
||||
"Endpoints": {
|
||||
"Http": {
|
||||
"Url": "http://0.0.0.0:7142"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
21
BimAI.API/appsettings.json
Normal file
21
BimAI.API/appsettings.json
Normal file
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"AllowedHosts": "*",
|
||||
"JwtSettings": {
|
||||
"Issuer": "BimAI.API",
|
||||
"Audience": "BimAI.Clients",
|
||||
"ExpiryDays": 7
|
||||
},
|
||||
"Hangfire": {
|
||||
"WorkerCount": 5,
|
||||
"DashboardPath": "/hangfire"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Database.Command": "Warning",
|
||||
"Microsoft.EntityFrameworkCore.Infrastructure": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user