This repository was archived by the owner on Jul 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
456 lines (404 loc) · 16.3 KB
/
Copy pathProgram.cs
File metadata and controls
456 lines (404 loc) · 16.3 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using BpTracker.Api.Data;
using BpTracker.Api.Endpoints;
using BpTracker.Api.Services;
using BpTracker.Api.Models;
using BpTracker.Api.Middleware;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using System.Security.Claims;
using System.Text.Json;
using Scalar.AspNetCore;
using System.Threading.RateLimiting;
using Serilog;
using Serilog.Events;
using Serilog.Formatting.Compact;
using Fido2NetLib;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Options;
using System.Text.Encodings.Web;
using Lib.Net.Http.WebPush;
var builder = WebApplication.CreateBuilder(args);
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor |
Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto;
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Override("Microsoft", LogEventLevel.Warning)
.Enrich.FromLogContext()
.Enrich.WithThreadId()
.Enrich.WithEnvironmentName()
.Enrich.WithMachineName()
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore", LogEventLevel.Warning)
.WriteTo.Console(new CompactJsonFormatter())
.WriteTo.Seq(builder.Configuration["SEQ_URL"] ?? "http://seq:80")
.CreateLogger();
builder.Host.UseSerilog();
// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
{
if (builder.Configuration["UseInMemoryDatabase"] == "true")
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
}
else
{
options.UseNpgsql(connectionString);
}
});
builder.Services.AddHttpClient();
builder.Services.AddOpenApi();
var geminiHealthClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) };
var photoApiHealthClient = new HttpClient { Timeout = TimeSpan.FromSeconds(2) };
builder.Services.AddHealthChecks()
.AddNpgSql(connectionString!)
.AddAsyncCheck("gemini", async () =>
{
var apiKey = builder.Configuration["GEMINI_API_KEY"];
var model = builder.Configuration["GEMINI_MODEL"] ?? "gemini-flash-latest";
if (string.IsNullOrEmpty(apiKey)) return HealthCheckResult.Unhealthy("Gemini API key is missing");
try
{
var response = await geminiHealthClient.GetAsync($"https://generativelanguage.googleapis.com/v1beta/models/{model}?key={apiKey}");
return response.IsSuccessStatusCode
? HealthCheckResult.Healthy()
: HealthCheckResult.Unhealthy("Gemini API unavailable");
}
catch
{
return HealthCheckResult.Unhealthy("Gemini API is unreachable");
}
})
.AddAsyncCheck("photo-api", async () =>
{
var enabled = builder.Configuration["PHOTO_API_ENABLED"] == "true";
if (!enabled) return HealthCheckResult.Healthy("aivm-photo-api is disabled");
var url = builder.Configuration["PHOTO_API_URL"];
if (string.IsNullOrEmpty(url))
return HealthCheckResult.Degraded("PHOTO_API_URL is not configured");
try
{
var response = await photoApiHealthClient.GetAsync($"{url.TrimEnd('/')}/health");
return response.IsSuccessStatusCode
? HealthCheckResult.Healthy()
: HealthCheckResult.Degraded("aivm-photo-api health check failed");
}
catch
{
return HealthCheckResult.Degraded("aivm-photo-api is unreachable");
}
});
builder.Services.AddScoped<IMeasurementService, MeasurementService>();
builder.Services.AddScoped<ISchemaService, SchemaService>();
builder.Services.AddScoped<IAuthService, AuthService>();
builder.Services.AddScoped<IPushService, PushService>();
builder.Services.AddScoped<IReminderService, ReminderService>();
builder.Services.AddSingleton(TimeProvider.System);
var vapidPublicKey = builder.Configuration["VAPID_PUBLIC_KEY"];
var vapidPrivateKey = builder.Configuration["VAPID_PRIVATE_KEY"];
var vapidSubject = builder.Configuration["VAPID_SUBJECT"];
if (string.IsNullOrEmpty(vapidPublicKey) || string.IsNullOrEmpty(vapidPrivateKey) || string.IsNullOrEmpty(vapidSubject))
{
Log.Warning("VAPID configuration is incomplete. Web push sending will be disabled.");
builder.Services.AddSingleton<IWebPushClient, DisabledWebPushClient>();
}
else
{
builder.Services.AddSingleton(new Lib.Net.Http.WebPush.Authentication.VapidAuthentication(vapidPublicKey, vapidPrivateKey)
{
Subject = vapidSubject
});
builder.Services.AddHttpClient<IWebPushClient, WebPushClient>();
}
builder.Services.Configure<AuthSettings>(options =>
{
var allowedEmailsStr = builder.Configuration["ALLOWED_EMAILS"];
if (string.IsNullOrEmpty(allowedEmailsStr))
{
options.AllowedEmails = [];
}
else
{
options.AllowedEmails = allowedEmailsStr.Split(',').ToHashSet();
}
});
builder.Services.Configure<SmtpSettings>(options =>
{
options.Host = builder.Configuration["SMTP_HOST"] ?? string.Empty;
options.Port = int.TryParse(builder.Configuration["SMTP_PORT"], out var smtpPort) ? smtpPort : 587;
options.Username = builder.Configuration["SMTP_USER"] ?? string.Empty;
options.Password = builder.Configuration["SMTP_PASSWORD"] ?? string.Empty;
options.FromAddress = builder.Configuration["SMTP_FROM"] ?? string.Empty;
options.FromName = builder.Configuration["SMTP_FROM_NAME"] ?? "BP Tracker";
options.UseTls = builder.Configuration["SMTP_TLS"] != "false";
});
builder.Services.AddScoped<SmtpEmailSender>();
var smtpConfigured = !string.IsNullOrEmpty(builder.Configuration["SMTP_HOST"]);
if (builder.Environment.IsDevelopment() && !smtpConfigured)
builder.Services.AddScoped<IEmailSender, DevConsoleEmailSender>();
else
builder.Services.AddScoped<IEmailSender, ResilientEmailSender>();
builder.Services.AddHostedService<EmailOutboxWorker>();
builder.Services.AddHostedService<CleanupWorker>();
builder.Services.AddHostedService<ReminderWorker>();
builder.Services.Configure<GeminiSettings>(options =>
{
options.ApiKey = builder.Configuration["GEMINI_API_KEY"] ?? string.Empty;
var modelEnv = builder.Configuration["GEMINI_MODEL"];
options.Model = string.IsNullOrWhiteSpace(modelEnv) ? "gemini-flash-latest" : modelEnv;
});
builder.Services.AddHttpClient<IGeminiService, GeminiService>();
builder.Services.Configure<PhotoApiSettings>(options =>
{
options.Enabled = builder.Configuration["PHOTO_API_ENABLED"] == "true";
options.Url = builder.Configuration["PHOTO_API_URL"];
options.Token = builder.Configuration["PHOTO_API_TOKEN"];
options.DeviceModel = builder.Configuration["PHOTO_API_DEVICE_MODEL"] ?? "Paramed Expert-X";
if (options.Enabled)
{
if (string.IsNullOrEmpty(options.Url))
throw new OptionsValidationException("PhotoApi", typeof(PhotoApiSettings), new[] { "PHOTO_API_URL is required when PHOTO_API_ENABLED is true" });
if (string.IsNullOrEmpty(options.Token))
throw new OptionsValidationException("PhotoApi", typeof(PhotoApiSettings), new[] { "PHOTO_API_TOKEN is required when PHOTO_API_ENABLED is true" });
}
});
builder.Services.AddHttpClient("PhotoApi", c => c.Timeout = TimeSpan.FromSeconds(5));
builder.Services.AddSingleton<IPhotoApiService, PhotoApiService>();
builder.Services.AddScoped<IFido2, Fido2>(sp => new Fido2(new Fido2Configuration
{
ServerDomain = builder.Configuration["FIDO2_DOMAIN"] ?? "bptracker.home.vn.ua",
ServerName = "BP Tracker",
Origins = (builder.Configuration["CORS_ORIGINS"] ?? "https://bptracker.home.vn.ua")
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Concat((builder.Configuration["FIDO2_ANDROID_ORIGINS"] ?? "")
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
.ToHashSet()
}));
builder.Services.AddMemoryCache();
builder.Services.AddDistributedMemoryCache(); // For storing Fido2 challenges
builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(5);
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Lax;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Session";
options.DefaultChallengeScheme = "Session";
})
.AddScheme<AuthenticationSchemeOptions, SessionAuthHandler>("Session", null);
builder.Services.AddAuthorization();
builder.Services.AddRateLimiter(options =>
{
// Per-user rate limit; falls back to IP if userId is unavailable (should not happen on auth'd endpoint)
options.AddPolicy("analyze", ctx =>
{
var userId = ctx.User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId is null)
{
Log.Warning("Rate limit: userId missing on /analyze, falling back to IP");
userId = "ip:" + (ctx.Connection.RemoteIpAddress?.ToString() ?? "unknown");
}
return RateLimitPartition.GetFixedWindowLimiter(userId, _ => new FixedWindowRateLimiterOptions
{
PermitLimit = 10,
Window = TimeSpan.FromMinutes(1),
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
QueueLimit = 0
});
});
// Per-IP rate limit for unauthenticated Fido2 challenge endpoints
options.AddPolicy("auth-challenge", ctx =>
RateLimitPartition.GetFixedWindowLimiter(
"ip:" + (ctx.Connection.RemoteIpAddress?.ToString() ?? "unknown"),
_ => new FixedWindowRateLimiterOptions
{
PermitLimit = 10,
Window = TimeSpan.FromMinutes(1),
QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
QueueLimit = 0
}));
options.OnRejected = async (ctx, token) =>
{
ctx.HttpContext.Response.StatusCode = 429;
await ctx.HttpContext.Response.WriteAsJsonAsync(
new { error = "Too many requests. Try again later." }, token);
};
});
var corsOrigins = (builder.Configuration["CORS_ORIGINS"] ?? "https://bptracker.home.vn.ua")
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(corsOrigins)
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options
.WithTitle("BP Tracker API")
.WithTheme(ScalarTheme.Moon)
.WithDefaultHttpClient(ScalarTarget.JavaScript, ScalarClient.Fetch);
});
app.MapDevAuthEndpoints();
}
// Configure the HTTP request pipeline.
app.UseForwardedHeaders();
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var exceptionFeature = context.Features.Get<IExceptionHandlerFeature>();
var exception = exceptionFeature?.Error;
if (exception is null) return;
var logger = context.RequestServices.GetRequiredService<ILogger<Program>>();
var requestId = context.Response.Headers["X-Request-ID"].FirstOrDefault()
?? context.TraceIdentifier;
logger.LogError(exception,
"Unhandled exception [{Method} {Path}] request_id={RequestId}",
context.Request.Method, context.Request.Path, requestId);
(int status, string title) = exception switch
{
DbUpdateConcurrencyException => (409, "Conflict"),
DbUpdateException dbEx when IsUniqueConstraintViolation(dbEx) => (409, "Conflict"),
DbUpdateException => (500, "Database error"),
_ => (500, "An unexpected error occurred")
};
context.Response.StatusCode = status;
context.Response.ContentType = "application/problem+json";
await context.Response.WriteAsJsonAsync(new
{
type = $"https://httpstatuses.com/{status}",
title,
status,
traceId = requestId,
detail = app.Environment.IsProduction() ? null : exception.ToString()
});
});
});
// CORS must be before auth/session middleware to handle OPTIONS preflight
app.UseCors();
app.Use(async (context, next) =>
{
var requestId = context.Request.Headers["X-Request-ID"].FirstOrDefault()
?? Guid.NewGuid().ToString();
using (Serilog.Context.LogContext.PushProperty("request_id", requestId))
{
context.Response.Headers["X-Request-ID"] = requestId;
await next();
}
});
app.UseSerilogRequestLogging();
// ASP.NET session (used by Fido2 for challenge storage) must come before endpoint handlers
app.UseSession();
// Custom session middleware: reads __Host-session cookie, sets HttpContext.User
// Must run before UseRateLimiter so the "analyze" policy can partition by userId
app.UseMiddleware<SessionMiddleware>();
app.UseRateLimiter();
app.UseAuthentication();
app.UseAuthorization();
app.MapHealthChecks("/api/v1/health", new HealthCheckOptions
{
ResponseWriter = async (context, report) =>
{
context.Response.ContentType = "application/json";
var result = JsonSerializer.Serialize(new
{
status = report.Status.ToString(),
checks = report.Entries.Select(e => new
{
name = e.Key,
status = e.Value.Status.ToString(),
description = e.Value.Description,
duration = e.Value.Duration
})
});
await context.Response.WriteAsync(result);
}
});
app.MapMeasurementEndpoints();
app.MapSchemaEndpoints();
app.MapAnalyzeEndpoints();
app.MapAuthEndpoints();
app.MapSettingsEndpoints();
app.MapExportEndpoints();
app.MapPushEndpoints();
app.MapReminderEndpoints();
// Apply migrations on startup with retry (waits for DB container to be ready)
using (var scope = app.Services.CreateScope())
{
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<Program>>();
var db = services.GetRequiredService<AppDbContext>();
const int maxAttempts = 15;
for (int i = 0; i < maxAttempts; i++)
{
try
{
if (db.Database.IsNpgsql())
{
db.Database.Migrate();
logger.LogInformation("Database migrated successfully");
}
else
{
logger.LogInformation("Skipping migrations for non-Npgsql provider: {Provider}", db.Database.ProviderName);
}
break;
}
catch (Exception ex)
{
if (i == maxAttempts - 1) throw;
logger.LogWarning(ex, "Database not ready, retrying in 2 s (attempt {Attempt}/{Max})", i + 1, maxAttempts);
Thread.Sleep(2000);
}
}
if (app.Environment.IsDevelopment())
{
try
{
await DbInitializer.SeedAsync(db);
logger.LogInformation("Development database seeded successfully");
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while seeding the development database");
}
}
}
app.Run();
static bool IsUniqueConstraintViolation(DbUpdateException ex) =>
ex.InnerException?.Message.Contains("23505") == true ||
ex.InnerException?.Message.Contains("unique constraint", StringComparison.OrdinalIgnoreCase) == true;
public partial class Program { }
public class SessionAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public SessionAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder)
: base(options, logger, encoder) { }
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (Context.User.Identity?.IsAuthenticated == true)
{
var ticket = new AuthenticationTicket(Context.User, "Session");
return Task.FromResult(AuthenticateResult.Success(ticket));
}
return Task.FromResult(AuthenticateResult.NoResult());
}
}