DependencyInjection.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using SharedKernel;
  2. using SharedKernel.Storage;
  3. using Application.Abstractions.Authentication;
  4. using Application.Abstractions.Data;
  5. using Application.Abstractions.Identity;
  6. using Application.Abstractions.Messaging.Email;
  7. using Application.Abstractions.Cache;
  8. using Application.Abstractions.Crypto;
  9. using Infrastructure.Authentication;
  10. using Infrastructure.Messaging.Email;
  11. using Infrastructure.Persistence;
  12. using Infrastructure.Persistence.Identity;
  13. using Infrastructure.Storage;
  14. using Infrastructure.Cache;
  15. using Infrastructure.Crypto;
  16. using Microsoft.AspNetCore.Authentication.JwtBearer;
  17. using Microsoft.AspNetCore.DataProtection;
  18. using Microsoft.AspNetCore.Identity;
  19. using Microsoft.AspNetCore.Identity.UI.Services;
  20. using Microsoft.EntityFrameworkCore;
  21. using Microsoft.Extensions.Configuration;
  22. using Microsoft.Extensions.DependencyInjection;
  23. using Microsoft.IdentityModel.Tokens;
  24. using System.Text;
  25. using StackExchange.Redis;
  26. namespace Infrastructure
  27. {
  28. public static class DependencyInjection
  29. {
  30. // SQL Server
  31. private static IServiceCollection AddDatabase(this IServiceCollection services, IConfiguration configuration)
  32. {
  33. var dbConn = configuration.GetConnectionString("DefaultConnection");
  34. if (string.IsNullOrWhiteSpace(dbConn))
  35. {
  36. throw new InvalidOperationException("Connection string 'DefaultConnection' is not configured.");
  37. }
  38. services.AddDbContext<AppDbContext>(options => options.UseSqlServer(dbConn));
  39. services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(dbConn));
  40. services.AddScoped<IAppDbContext>(sp => sp.GetRequiredService<AppDbContext>());
  41. return services;
  42. }
  43. // Redis Server
  44. public static IServiceCollection AddRedis(this IServiceCollection services, IConfiguration configuration)
  45. {
  46. var settings = configuration.Get<AppSettings>()!;
  47. var redis = ConnectionMultiplexer.Connect(settings.Redis.DefaultConnection);
  48. services.AddSingleton<IConnectionMultiplexer>(redis);
  49. services.AddDataProtection().SetApplicationName(settings.App.Name).PersistKeysToStackExchangeRedis(redis, settings.Redis.DataProtectionKey).SetDefaultKeyLifetime(settings.Redis.DefaultKeyLifetime); // 기본 90일 주기
  50. // Distributed Cache 설정
  51. services.AddStackExchangeRedisCache(options =>
  52. {
  53. options.Configuration = settings.Redis.DefaultConnection;
  54. options.InstanceName = settings.Redis.CachePrefix;
  55. });
  56. return services;
  57. }
  58. private static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration)
  59. {
  60. var settings = configuration.Get<AppSettings>()!;
  61. services.AddHealthChecks().AddSqlServer(settings.ConnectionStrings.DefaultConnection).AddRedis(settings.Redis.DefaultConnection);
  62. return services;
  63. }
  64. private static IServiceCollection AddServices(this IServiceCollection services)
  65. {
  66. services.AddSingleton<IJwtTokenProvider, JwtTokenProvider>();
  67. services.AddSingleton<ICacheService, RedisCacheService>();
  68. services.AddTransient<IMailService, MailService>();
  69. services.AddTransient<IEmailSender, IdentityEmailSender>();
  70. services.AddScoped<IFileStorage, LocalFileStorage>();
  71. services.AddScoped<IEditorImageService, EditorImageService>();
  72. services.AddScoped<IIdentityUserReader, IdentityUserReader>();
  73. services.AddScoped<IIdentityUserWriter, IdentityUserWriter>();
  74. services.AddScoped<IIdentityRoleReader, IdentityRoleReader>();
  75. services.AddScoped<IIdentityRoleWriter, IdentityRoleWriter>();
  76. services.AddHttpClient<IUpbitClient, UpbitRestClient>(client =>
  77. {
  78. client.BaseAddress = new Uri("https://api.upbit.com/v1/");
  79. client.DefaultRequestHeaders.Add("Accept", "application/json");
  80. client.Timeout = TimeSpan.FromSeconds(10);
  81. });
  82. return services;
  83. }
  84. /**
  85. * ========================================================================================================================================================================================================
  86. * ========================================================================================================================================================================================================
  87. */
  88. // Admin 전용
  89. public static IServiceCollection AddAdminInfrastructure(this IServiceCollection services, IConfiguration configuration)
  90. {
  91. return services.AddDatabase(configuration).AddRedis(configuration).AddServices().AddHealthChecks(configuration);
  92. }
  93. /**
  94. * ========================================================================================================================================================================================================
  95. * ========================================================================================================================================================================================================
  96. */
  97. // API 전용
  98. public static IServiceCollection AddApiInfrastructure(this IServiceCollection services, IConfiguration configuration)
  99. {
  100. return services.AddDatabase(configuration).AddRedis(configuration).AddApiAuthentication(configuration).AddServices().AddUpbit().AddHealthChecks(configuration);
  101. }
  102. private static IServiceCollection AddUpbit(this IServiceCollection services)
  103. {
  104. services.AddHostedService<UpbitWebSocketService>();
  105. return services;
  106. }
  107. private static IServiceCollection AddApiAuthentication(this IServiceCollection services, IConfiguration configuration)
  108. {
  109. var settings = configuration.Get<AppSettings>()!;
  110. // 인증 정책- JWT Bearer
  111. services.AddAuthentication(options =>
  112. {
  113. options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  114. options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  115. })
  116. .AddJwtBearer(options =>
  117. {
  118. options.RequireHttpsMetadata = false;
  119. options.MapInboundClaims = false;
  120. options.TokenValidationParameters = new TokenValidationParameters
  121. {
  122. ValidateIssuer = true,
  123. ValidateAudience = true,
  124. ValidateLifetime = true,
  125. ValidateIssuerSigningKey = true,
  126. ValidIssuer = settings.JWT.Issuer,
  127. ValidAudience = settings.JWT.Audience,
  128. IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(settings.JWT.SecretKey)),
  129. ClockSkew = TimeSpan.Zero
  130. };
  131. });
  132. services.AddAuthorization();
  133. // Identity Core (Admin Handler의 UserManager/RoleManager 의존 해소용)
  134. services.AddIdentityCore<ApplicationUser>().AddRoles<IdentityRole>().AddEntityFrameworkStores<IdentityDbContext>();
  135. return services;
  136. }
  137. }
  138. }