-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
51 lines (40 loc) · 1.51 KB
/
Copy pathProgram.cs
File metadata and controls
51 lines (40 loc) · 1.51 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
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using MyAlumniApp.Data;
using System.IO; // ✅ Needed for Path.Combine
var builder = WebApplication.CreateBuilder(args);
// ✅ Database connection
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
builder.Configuration.GetConnectionString("DefaultConnection"),
new MySqlServerVersion(new Version(8, 0, 34))
)
);
// ✅ Controllers + CORS
builder.Services.AddControllers();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", policy =>
policy.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
var app = builder.Build();
// ✅ Get port from environment variable (Render provides this)
var port = Environment.GetEnvironmentVariable("PORT") ?? "8080";
app.Urls.Add($"http://0.0.0.0:{port}");
// ✅ Allow JS fetch calls from any origin
app.UseCors("AllowAll");
// ✅ Serve static files (HTML, images, etc.)
var staticPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
Console.WriteLine("👉 Serving static files from: " + staticPath);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(staticPath),
RequestPath = "" // means served directly under root (e.g. /images/akshay.jpg)
});
// ✅ Let index.html and results.html be directly accessible
app.UseDefaultFiles();
// ✅ Map API endpoints (controllers)
app.MapControllers();
app.Run();