-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
138 lines (115 loc) · 4.47 KB
/
Copy pathProgram.cs
File metadata and controls
138 lines (115 loc) · 4.47 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
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Npgsql.EntityFrameworkCore.PostgreSQL;
using Scalar.AspNetCore;
using Serilog;
using WebApplication1.Data;
using WebApplication1.Domains;
using WebApplication1.Infrastructure.Background;
using WebApplication1.Infrastructure.Mapping;
using WebApplication1.Infrastructure.Middleware;
using WebApplication1.Services.Implementations;
using WebApplication1.Services.Interfaces;
var builder = WebApplication.CreateBuilder(args);
// Configure Serilog - Only log errors
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Error()
.WriteTo.Console()
.WriteTo.File("logs/log-.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
builder.Host.UseSerilog();
// Add services to the container
builder.Services.AddControllers();
// Database configuration (switchable via appsettings.json "Database:Provider")
var dbProvider = builder.Configuration["Database:Provider"] ?? "Sqlite";
builder.Services.AddDbContext<AppDbContext>(options =>
{
if (dbProvider == "Supabase")
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
options.UseNpgsql(
builder.Configuration.GetConnectionString("SupabaseConnection"),
npgsqlOptions => npgsqlOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
);
}
else
{
options.UseSqlite(
builder.Configuration.GetConnectionString("DefaultConnection"),
sqliteOptions => sqliteOptions.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
);
}
options.EnableSensitiveDataLogging(builder.Environment.IsDevelopment());
});
// Built-in OpenAPI support
builder.Services.AddOpenApi();
// AutoMapper configuration
builder.Services.AddAutoMapper(typeof(MappingProfile));
// JWT Authentication
var jwtKey = builder.Configuration["Jwt:Key"];
var jwtIssuer = builder.Configuration["Jwt:Issuer"];
var jwtAudience = builder.Configuration["Jwt:Audience"];
builder.Services
.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = jwtIssuer,
ValidAudience = jwtAudience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey!))
};
});
builder.Services.AddAuthorization();
// Dependency Injection
builder.Services.AddSingleton<IEncryptionService, EncryptionService>();
builder.Services.AddScoped<PasswordHasher<User>>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IEmailService, EmailService>();
builder.Services.AddScoped<IMemoryService, MemoryService>();
builder.Services.AddScoped<ILetterService, LetterService>();
builder.Services.AddScoped<IQuestionService, QuestionService>();
// HttpClient for Gemini (90 second timeout for model inference)
builder.Services.AddHttpClient<IAIQuestionGenerator, GeminiQuestionGenerator>()
.ConfigureHttpClient(client => client.Timeout = TimeSpan.FromSeconds(90));
// Question generation queue (singleton to persist across requests)
builder.Services.AddSingleton<IQuestionGenerationQueue, QuestionGenerationQueue>();
// Background service for question generation
builder.Services.AddHostedService<QuestionGenerationBackgroundService>();
var app = builder.Build();
// Apply migrations automatically in development (SQLite only - Supabase uses manual schema)
if (app.Environment.IsDevelopment() && dbProvider == "Sqlite")
{
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
dbContext.Database.Migrate();
}
}
// Global exception middleware
app.UseMiddleware<GlobalExceptionMiddleware>();
// Configure the HTTP request pipeline
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference(options =>
{
options.Title = "WebApplication1 API";
});
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();