improvement: ApnsSender 환경별 APNs 호스트 자동 분기 (#273) #274

Merged
seonkyu.kim merged 1 commits from improvement/#273-apns-host-env into develop 2026-03-03 01:41:42 +00:00

View File

@ -4,6 +4,7 @@ using System.Net.Http.Headers;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens; using Microsoft.IdentityModel.Tokens;
using SPMS.Application.DTOs.Push; using SPMS.Application.DTOs.Push;
@ -14,17 +15,21 @@ namespace SPMS.Infrastructure.Push;
public class ApnsSender : IApnsSender public class ApnsSender : IApnsSender
{ {
private const string ProductionHost = "https://api.push.apple.com"; private const string ProductionHost = "https://api.push.apple.com";
private const string SandboxHost = "https://api.sandbox.push.apple.com";
private const int MaxConcurrency = 50; private const int MaxConcurrency = 50;
private const int TokenRefreshMinutes = 50; private const int TokenRefreshMinutes = 50;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
private readonly ILogger<ApnsSender> _logger; private readonly ILogger<ApnsSender> _logger;
private readonly string _apnsHost;
private readonly ConcurrentDictionary<string, (string Token, DateTime ExpiresAt)> _tokenCache = new(); private readonly ConcurrentDictionary<string, (string Token, DateTime ExpiresAt)> _tokenCache = new();
public ApnsSender(IHttpClientFactory httpClientFactory, ILogger<ApnsSender> logger) public ApnsSender(IHttpClientFactory httpClientFactory, IHostEnvironment hostEnvironment, ILogger<ApnsSender> logger)
{ {
_httpClient = httpClientFactory.CreateClient("ApnsSender"); _httpClient = httpClientFactory.CreateClient("ApnsSender");
_logger = logger; _logger = logger;
_apnsHost = hostEnvironment.IsDevelopment() ? SandboxHost : ProductionHost;
_logger.LogInformation("APNs 환경: {Host}", _apnsHost);
} }
public async Task<PushResultDto> SendAsync( public async Task<PushResultDto> SendAsync(
@ -79,7 +84,7 @@ public class ApnsSender : IApnsSender
string jwt, string bundleId, string deviceToken, string jwt, string bundleId, string deviceToken,
string payload, CancellationToken cancellationToken) string payload, CancellationToken cancellationToken)
{ {
var url = $"{ProductionHost}/3/device/{deviceToken}"; var url = $"{_apnsHost}/3/device/{deviceToken}";
using var request = new HttpRequestMessage(HttpMethod.Post, url) using var request = new HttpRequestMessage(HttpMethod.Post, url)
{ {