Skip to content

Commit cc62576

Browse files
committed
feat: 각 서비스 초기화를 각 모듈에서 진행
각 모듈에서 서비스에 필요한 초기화 분리 - Programs코드 간소화 및 using 간소화 - 책임 분리 가독성 상승
1 parent bbd52d8 commit cc62576

File tree

6 files changed

+320
-238
lines changed

6 files changed

+320
-238
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using Microsoft.AspNetCore.Builder;
2+
using ProjectVG.Api.Middleware;
3+
using ProjectVG.Api.Services;
4+
5+
namespace ProjectVG.Api
6+
{
7+
public static class ApiMiddlewareExtensions
8+
{
9+
/// <summary>
10+
/// API 미들웨어 파이프라인 구성
11+
/// </summary>
12+
public static IApplicationBuilder UseApiMiddleware(this IApplicationBuilder app, IWebHostEnvironment environment)
13+
{
14+
// 개발 환경 설정
15+
if (environment.IsDevelopment()) {
16+
app.UseSwagger();
17+
app.UseSwaggerUI(c => {
18+
c.SwaggerEndpoint("/swagger/v1/swagger.json", "ProjectVG API V1");
19+
c.RoutePrefix = "swagger";
20+
});
21+
}
22+
23+
// 전역 예외 처리
24+
app.UseGlobalExceptionHandler();
25+
26+
// WebSocket 지원
27+
app.UseWebSockets();
28+
29+
// WebSocket 미들웨어 등록
30+
app.UseMiddleware<WebSocketMiddleware>();
31+
32+
// 요청 로깅 미들웨어
33+
app.Use(async (ctx, next) => {
34+
var logger = ctx.RequestServices.GetRequiredService<ILogger<Program>>();
35+
logger.LogInformation("REQ {method} {path} from {remote}", ctx.Request.Method, ctx.Request.Path, ctx.Connection.RemoteIpAddress);
36+
await next();
37+
});
38+
39+
// 인증/인가
40+
app.UseAuthentication();
41+
app.UseAuthorization();
42+
43+
// CORS 미들웨어 적용
44+
app.UseCors("AllowAll");
45+
46+
// 컨트롤러 매핑
47+
app.UseRouting();
48+
app.UseEndpoints(endpoints => {
49+
endpoints.MapControllers();
50+
});
51+
52+
return app;
53+
}
54+
55+
/// <summary>
56+
/// 개발 환경 전용 기능
57+
/// </summary>
58+
public static IApplicationBuilder UseDevelopmentFeatures(this IApplicationBuilder app)
59+
{
60+
// 개발 환경에서 테스트 클라이언트 자동 실행
61+
var serviceProvider = app.ApplicationServices;
62+
serviceProvider.GetRequiredService<TestClientLauncher>().Launch();
63+
64+
return app;
65+
}
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using ProjectVG.Api.Services;
3+
using ProjectVG.Api.Filters;
4+
using Microsoft.AspNetCore.Authentication.Negotiate;
5+
6+
namespace ProjectVG.Api
7+
{
8+
public static class ApiServiceCollectionExtensions
9+
{
10+
/// <summary>
11+
/// API 서비스 등록
12+
/// </summary>
13+
public static IServiceCollection AddApiServices(this IServiceCollection services)
14+
{
15+
services.AddControllers(options => {
16+
options.Filters.Add<ModelStateValidationFilter>();
17+
});
18+
19+
services.AddEndpointsApiExplorer();
20+
services.AddSwaggerGen(c => {
21+
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo {
22+
Title = "ProjectVG API",
23+
Version = "v1",
24+
Description = "ProjectVG API Server"
25+
});
26+
});
27+
28+
services.AddSingleton<TestClientLauncher>();
29+
30+
return services;
31+
}
32+
33+
/// <summary>
34+
/// 인증 및 인가 서비스
35+
/// </summary>
36+
public static IServiceCollection AddApiAuthentication(this IServiceCollection services)
37+
{
38+
services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
39+
.AddNegotiate();
40+
41+
services.AddAuthorization(options => {
42+
options.FallbackPolicy = null;
43+
});
44+
45+
return services;
46+
}
47+
48+
/// <summary>
49+
/// 개발용 CORS 정책
50+
/// </summary>
51+
public static IServiceCollection AddDevelopmentCors(this IServiceCollection services)
52+
{
53+
services.AddCors(options => {
54+
options.AddPolicy("AllowAll",
55+
policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
56+
});
57+
58+
return services;
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)