Program.cs
4.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System.Text;
using System.Text.Json;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.IdentityModel.Tokens;
using RobotProductionSystem.Api.Endpoints;
using RobotProductionSystem.Api.Infrastructure;
using RobotProductionSystem.Api.Services;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<JwtOptions>(builder.Configuration.GetSection("Jwt"));
var jwtOptions = builder.Configuration.GetSection("Jwt").Get<JwtOptions>() ?? new JwtOptions();
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<JwtTokenService>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtOptions.Issuer,
ValidAudience = jwtOptions.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtOptions.Secret)),
ClockSkew = TimeSpan.FromSeconds(30)
};
});
builder.Services.AddAuthorization();
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.SerializerOptions.DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never;
});
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
var configuredUploadsRoot = builder.Configuration["FileStorage:UploadsRoot"]?.Trim();
var uploadsRoot = string.IsNullOrWhiteSpace(configuredUploadsRoot)
? Path.Combine(app.Environment.ContentRootPath, "uploads")
: Path.GetFullPath(configuredUploadsRoot);
Directory.CreateDirectory(uploadsRoot);
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(uploadsRoot),
RequestPath = "/uploads"
});
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/health", () => new { status = "ok" });
app.MapRobotApi();
await using (var scope = app.Services.CreateAsyncScope())
{
var logger = scope.ServiceProvider
.GetRequiredService<ILoggerFactory>()
.CreateLogger("DatabaseStartup");
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
var maxRetries = builder.Configuration.GetValue<int?>("Database:Migration:MaxRetries") ?? 10;
var retryDelaySeconds = builder.Configuration.GetValue<int?>("Database:Migration:RetryDelaySeconds") ?? 3;
for (var attempt = 1; ; attempt++)
{
try
{
logger.LogInformation("Starting database migration and seed.");
var pendingMigrations = (await db.Database.GetPendingMigrationsAsync()).ToArray();
if (pendingMigrations.Length > 0)
{
logger.LogInformation(
"Pending migrations detected ({Count}): {Migrations}",
pendingMigrations.Length,
string.Join(", ", pendingMigrations));
}
else
{
logger.LogInformation("No pending migrations detected.");
}
await db.Database.MigrateAsync();
await SeedData.InitializeAsync(db);
logger.LogInformation("Database migration and seed completed successfully.");
break;
}
catch (Exception ex) when (attempt < maxRetries)
{
logger.LogWarning(
ex,
"Database initialization failed on attempt {Attempt}/{MaxRetries}. Retrying in {RetryDelaySeconds}s.",
attempt,
maxRetries,
retryDelaySeconds);
await Task.Delay(TimeSpan.FromSeconds(retryDelaySeconds));
}
}
}
await app.RunAsync();