-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
93 lines (73 loc) · 2.5 KB
/
Copy pathProgram.cs
File metadata and controls
93 lines (73 loc) · 2.5 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
using CollecthubDotNet.Models;
using CollecthubDotNet.Services;
using DotNetEnv;
// Load .env file
Env.Load();
var builder = WebApplication.CreateBuilder(args);
// Configure MongoDB settings from environment variables
builder.Services.Configure<MongoDbSettings>(options =>
{
options.ConnectionString = Environment.GetEnvironmentVariable("MONGODB_CONNECTION_STRING") ?? "mongodb://localhost:27017";
options.DatabaseName = Environment.GetEnvironmentVariable("DATABASE_NAME") ?? "collecthub";
// Add logging to debug
Console.WriteLine($"MongoDB ConnectionString: {options.ConnectionString}");
Console.WriteLine($"MongoDB DatabaseName: {options.DatabaseName}");
});
// Register services
builder.Services.AddSingleton<MongoDbService>();
builder.Services.AddScoped<UserService>();
builder.Services.AddScoped<GameService>();
builder.Services.AddScoped<YouTubeChannelService>();
builder.Services.AddScoped<FavProgrammingLanguageService>();
builder.Services.AddScoped<FavVehicleService>();
builder.Services.AddScoped<FavMusicService>();
builder.Services.AddScoped<MobileAppService>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
{
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
// Add health checks
builder.Services.AddHealthChecks();
// Add services to the container
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Test MongoDB connection on startup
try
{
var mongoService = app.Services.GetRequiredService<MongoDbService>();
var testCollection = mongoService.GetCollection<object>("test");
Console.WriteLine("✅ MongoDB connection successful!");
}
catch (Exception ex)
{
Console.WriteLine($"❌ MongoDB connection failed: {ex.Message}");
}
// Configure the HTTP request pipeline
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
//}
app.UseDefaultFiles(); // Looks for index.html in wwwroot
app.UseStaticFiles();
// Enable CORS - THIS WAS MISSING! Must be before UseRouting and UseAuthorization
app.UseCors("AllowAll");
app.UseHttpsRedirection();
app.UseRouting(); // Add this for explicit routing
app.UseAuthorization();
// Map health checks
app.MapHealthChecks("/healthcheck");
app.MapControllers();
// Add custom route for health UI (redirect /health to /health/ui)
app.MapGet("/health", async context =>
{
context.Response.Redirect("/health/ui");
});
app.Run();