65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using backend.Data;
|
|
using backend.Models;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container
|
|
builder.Services.AddControllers();
|
|
|
|
// Configure Entity Framework with SQL Server
|
|
builder.Services.AddDbContext<AppDbContext>(options =>
|
|
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
|
|
|
|
// Bind AppConfig from appsettings.json
|
|
builder.Services.Configure<AppConfig>(
|
|
builder.Configuration.GetSection("AppConfig"));
|
|
|
|
// Configure CORS
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("AllowFrontend", policy =>
|
|
{
|
|
policy.WithOrigins("http://localhost:5173", "http://localhost:3000")
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
|
|
// Configure serving the built SPA static files (used in Production)
|
|
builder.Services.AddSpaStaticFiles(configuration =>
|
|
{
|
|
configuration.RootPath = "ClientApp/dist";
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
app.UseCors("AllowFrontend");
|
|
app.MapControllers();
|
|
|
|
// Auto-migrate database on startup
|
|
using (var scope = app.Services.CreateScope())
|
|
{
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
db.Database.Migrate();
|
|
}
|
|
|
|
// Configure SPA serving
|
|
if (!app.Environment.IsDevelopment())
|
|
{
|
|
// In Production/Staging, serve the pre-built frontend from ClientApp/dist
|
|
app.UseSpaStaticFiles();
|
|
app.UseSpa(spa =>
|
|
{
|
|
spa.Options.SourcePath = "ClientApp";
|
|
});
|
|
}
|
|
// In Development:
|
|
// - The backend serves only the API at http://localhost:5000
|
|
// - The frontend is served by Vite dev server at http://localhost:5173
|
|
// - Vite proxies /api requests to the backend
|
|
// - Run: cd backend\ClientApp && npm run dev (for hot-reload frontend)
|
|
// - Or open http://localhost:5000 after building the frontend
|
|
|
|
app.Run();
|