SPMS_API/SPMS.API/Program.cs
SEAN 787190f512 feat: Generic Repository 및 UnitOfWork 패턴 구현 (#18)
- IRepository<T> 구현체: CRUD, 페이징, 조건 검색 지원
- IUnitOfWork 구현체: 트랜잭션 관리 (Begin/Commit/Rollback)
- Program.cs에 DI 등록 (AddScoped)

Closes #18
2026-02-09 14:44:31 +09:00

55 lines
1.5 KiB
C#

using Microsoft.EntityFrameworkCore;
using SPMS.API.Middlewares;
using SPMS.Domain.Interfaces;
using SPMS.Infrastructure;
using SPMS.Infrastructure.Persistence;
using SPMS.Infrastructure.Persistence.Repositories;
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
WebRootPath = Environment.GetEnvironmentVariable("ASPNETCORE_WEBROOT")
?? "wwwroot"
});
builder.Services.AddControllers();
builder.Services.AddOpenApi();
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString)));
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
var app = builder.Build();
// ── 1. 예외 처리 (최외곽 — 이후 모든 미들웨어 예외 포착) ──
app.UseMiddleware<ExceptionMiddleware>();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
var webRoot = app.Environment.WebRootPath;
Console.WriteLine($"[System] Web Root Path: {webRoot}"); // 로그에 경로 찍어보기
if (Directory.Exists(webRoot))
{
app.UseStaticFiles(); // 경로가 있으면 파일 서빙
}
else
{
Console.WriteLine("[Error] Web root folder not found!");
}
app.UseHttpsRedirection();
app.UseRouting();
// [4] 요청 처리
app.MapControllers();
app.MapFallbackToFile("index.html");
app.Run();