| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using Application.Abstractions.Authentication;
- using Application.Abstractions.Cache;
- using Application.Abstractions.Chat;
- using Application.Abstractions.Data;
- using Application.Abstractions.Identity;
- using Application.Abstractions.Forum;
- using Application.Abstractions.Messaging.Email;
- using Application.Abstractions.Notification;
- using Application.Abstractions.Payment;
- using Application.Abstractions.YouTube;
- using Infrastructure.Authentication;
- using Infrastructure.Payment;
- using Infrastructure.Forum;
- using Infrastructure.Cache;
- using Infrastructure.Chat;
- using Infrastructure.Messaging.Email;
- using Infrastructure.Persistence;
- using Infrastructure.Persistence.Identity;
- using Infrastructure.Storage;
- using Infrastructure.YouTube;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.DataProtection;
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.Identity.UI.Services;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.IdentityModel.Tokens;
- using SharedKernel;
- using SharedKernel.Storage;
- using StackExchange.Redis;
- using System.Text;
- namespace Infrastructure
- {
- public static class DependencyInjection
- {
- // SQL Server
- private static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
- {
- var dbConn = configuration.GetConnectionString("DefaultConnection");
- if (string.IsNullOrWhiteSpace(dbConn))
- {
- throw new InvalidOperationException("Connection string 'DefaultConnection' is not configured.");
- }
- services.AddDbContext<AppDbContext>(options => options.UseSqlServer(dbConn));
- services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(dbConn));
- services.AddScoped<IAppDbContext>(sp => sp.GetRequiredService<AppDbContext>());
- return services;
- }
- // Redis Server
- public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
- {
- var settings = configuration.Get<AppSettings>()!;
- var redis = ConnectionMultiplexer.Connect(settings.Redis.DefaultConnection);
- services.AddSingleton<IConnectionMultiplexer>(redis);
- services.AddDataProtection().SetApplicationName(settings.App.Name).PersistKeysToStackExchangeRedis(redis, settings.Redis.DataProtectionKey).SetDefaultKeyLifetime(settings.Redis.DefaultKeyLifetime); // 기본 90일 주기
- // Distributed Cache 설정
- services.AddStackExchangeRedisCache(options =>
- {
- options.Configuration = settings.Redis.DefaultConnection;
- options.InstanceName = settings.Redis.CachePrefix;
- });
- return services;
- }
- private static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
- {
- var settings = configuration.Get<AppSettings>()!;
- services.AddHealthChecks().AddSqlServer(settings.ConnectionStrings.DefaultConnection).AddRedis(settings.Redis.DefaultConnection);
- return services;
- }
- private static IServiceCollection AddServices(this IServiceCollection services)
- {
- services.AddSingleton<IJwtTokenProvider, JwtTokenProvider>();
- services.AddSingleton<ICacheService, RedisCacheService>();
- services.AddTransient<IMailService, MailService>();
- services.AddTransient<IEmailSender, IdentityEmailSender>();
- services.AddScoped<IFileStorage, LocalFileStorage>();
- services.AddScoped<IEditorImageService, EditorImageService>();
- services.AddScoped<IIdentityUserReader, IdentityUserReader>();
- services.AddScoped<IIdentityUserWriter, IdentityUserWriter>();
- services.AddScoped<IIdentityRoleReader, IdentityRoleReader>();
- services.AddScoped<IIdentityRoleWriter, IdentityRoleWriter>();
- services.AddSingleton<IChatMessageStore, RedisChatMessageStore>();
- services.AddSingleton<IChatConnectionTracker, RedisChatConnectionTracker>();
- services.AddScoped<IBoardPermissionService, BoardPermissionService>();
- services.AddHttpClient<IGoogleTokenValidator, GoogleTokenValidator>();
- // YouTube & Google OAuth
- services.AddTransient<YouTubeApiKeyHandler>();
- services.AddHttpClient("YouTubeApi", client =>
- {
- client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
- })
- .AddHttpMessageHandler<YouTubeApiKeyHandler>();
- services.AddHttpClient("PubSubHub");
- services.AddHttpClient<IGoogleOAuthService, GoogleOAuthService>();
- services.AddHttpClient<IDanalPayService, DanalPayService>();
- services.AddSingleton<IYouTubeApiService, YouTubeApiService>();
- services.AddSingleton<IYouTubeChannelCache, YouTubeChannelCache>();
- services.AddSingleton<IYouTubeLiveStateStore, YouTubeLiveStateStore>();
- services.AddSingleton<YouTubeLiveChatService>();
- services.AddSingleton<IYouTubeLiveChatService>(sp => sp.GetRequiredService<YouTubeLiveChatService>());
- services.AddHostedService(sp => sp.GetRequiredService<YouTubeLiveChatService>());
- services.AddSingleton<YouTubePubSubService>();
- services.AddSingleton<IYouTubePubSubService>(sp => sp.GetRequiredService<YouTubePubSubService>());
- services.AddHostedService<YouTubePubSubRenewalService>();
- services.AddHostedService<YouTubeChannelCacheRefreshService>();
- // Notification
- services.AddScoped<INotificationService, Notification.NotificationService>();
- return services;
- }
- /**
- * ========================================================================================================================================================================================================
- * ========================================================================================================================================================================================================
- */
- // Admin 전용
- public static IServiceCollection AddAdminInfrastructure(this IServiceCollection services, IConfiguration configuration)
- {
- return services.AddDatabase(configuration).AddRedis(configuration).AddServices().AddHealthChecks(configuration);
- }
- /**
- * ========================================================================================================================================================================================================
- * ========================================================================================================================================================================================================
- */
- // API 전용
- public static IServiceCollection AddApiInfrastructure(this IServiceCollection services, IConfiguration configuration)
- {
- return services.AddDatabase(configuration).AddRedis(configuration).AddApiAuthentication(configuration).AddServices().AddHealthChecks(configuration);
- }
- private static IServiceCollection AddApiAuthentication(this IServiceCollection services, IConfiguration configuration)
- {
- var settings = configuration.Get<AppSettings>()!;
- // 인증 정책- JWT Bearer
- services.AddAuthentication(options =>
- {
- options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
- options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
- })
- .AddJwtBearer(options =>
- {
- options.RequireHttpsMetadata = false;
- options.MapInboundClaims = false;
- options.TokenValidationParameters = new TokenValidationParameters
- {
- ValidateIssuer = true,
- ValidateAudience = true,
- ValidateLifetime = true,
- ValidateIssuerSigningKey = true,
- ValidIssuer = settings.JWT.Issuer,
- ValidAudience = settings.JWT.Audience,
- IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(settings.JWT.SecretKey)),
- ClockSkew = TimeSpan.Zero
- };
- options.Events = new JwtBearerEvents
- {
- OnMessageReceived = context =>
- {
- var accessToken = context.Request.Query["access_token"];
- var path = context.HttpContext.Request.Path;
- if (!string.IsNullOrEmpty(accessToken) && path.StartsWithSegments("/hubs"))
- {
- context.Token = accessToken;
- }
- return Task.CompletedTask;
- }
- };
- });
- services.AddAuthorization();
- // Identity Core (Admin Handler의 UserManager/RoleManager 의존 해소용)
- services.AddIdentityCore<ApplicationUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
- return services;
- }
- }
- }
|