|
|
@@ -0,0 +1,72 @@
|
|
|
+using System.Text;
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using CoupangRequest = goods.Models.Coupang.Request;
|
|
|
+
|
|
|
+namespace goods.Controllers
|
|
|
+{
|
|
|
+ // sitemap.xml 생성
|
|
|
+ // 카테고리/PL 목록은 Request.Categories, Request.Brand enum 에서 그대로 읽는다.
|
|
|
+ // HomeController 가 Enum.IsDefined 로 유효성을 검사하므로, enum 에 없는 ID 는 애초에 400 이 되고
|
|
|
+ // enum 에 추가된 ID 는 별도 작업 없이 sitemap 에 함께 노출된다.
|
|
|
+ public class SitemapController : Controller
|
|
|
+ {
|
|
|
+ // 사이트 정식 주소. 리버스 프록시 뒤에서는 Request.Scheme 이 http 로 들어올 수 있어
|
|
|
+ // 설정값(Site:BaseUrl)을 우선 사용하고, 없으면 운영 도메인을 쓴다.
|
|
|
+ private const string DefaultBaseUrl = "https://goods.web.or.kr";
|
|
|
+
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+
|
|
|
+ public SitemapController(IConfiguration configuration)
|
|
|
+ {
|
|
|
+ _configuration = configuration;
|
|
|
+ }
|
|
|
+
|
|
|
+ [HttpGet("/sitemap.xml")]
|
|
|
+ [ResponseCache(Duration = 3600, Location = ResponseCacheLocation.Any)]
|
|
|
+ public IActionResult Index()
|
|
|
+ {
|
|
|
+ var baseUrl = (_configuration["Site:BaseUrl"] ?? DefaultBaseUrl).TrimEnd('/');
|
|
|
+
|
|
|
+ // 목록 페이지는 쿠팡 API 응답에 따라 매일 내용이 바뀌므로 날짜 단위로만 표기한다.
|
|
|
+ var today = DateTime.UtcNow.ToString("yyyy-MM-dd");
|
|
|
+
|
|
|
+ var builder = new StringBuilder();
|
|
|
+ builder.AppendLine(@"<?xml version=""1.0"" encoding=""UTF-8""?>");
|
|
|
+ builder.AppendLine(@"<urlset xmlns=""http://www.sitemaps.org/schemas/sitemap/0.9"">");
|
|
|
+
|
|
|
+ // 홈
|
|
|
+ AppendUrl(builder, $"{baseUrl}/", today, "daily", "1.0");
|
|
|
+
|
|
|
+ // 골드박스, 이벤트
|
|
|
+ AppendUrl(builder, $"{baseUrl}/GoldBox", today, "daily", "0.8");
|
|
|
+ AppendUrl(builder, $"{baseUrl}/Event", today, "weekly", "0.8");
|
|
|
+
|
|
|
+ // 카테고리별 베스트 상품
|
|
|
+ foreach (var id in Enum.GetValues<CoupangRequest.Categories>().Select(value => (int)value).Distinct().OrderBy(id => id))
|
|
|
+ {
|
|
|
+ AppendUrl(builder, $"{baseUrl}/category/{id}", today, "daily", "0.8");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 쿠팡PL 브랜드별 상품
|
|
|
+ foreach (var id in Enum.GetValues<CoupangRequest.Brand>().Select(value => (int)value).Distinct().OrderBy(id => id))
|
|
|
+ {
|
|
|
+ AppendUrl(builder, $"{baseUrl}/pl/{id}", today, "daily", "0.8");
|
|
|
+ }
|
|
|
+
|
|
|
+ builder.AppendLine("</urlset>");
|
|
|
+
|
|
|
+ // 검색(/search)은 keyword 없이는 400 을 반환하는 파라미터 페이지라 제외한다.
|
|
|
+ return Content(builder.ToString(), "application/xml", Encoding.UTF8);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void AppendUrl(StringBuilder builder, string location, string lastModified, string changeFrequency, string priority)
|
|
|
+ {
|
|
|
+ builder.AppendLine(" <url>");
|
|
|
+ builder.AppendLine($" <loc>{location}</loc>");
|
|
|
+ builder.AppendLine($" <lastmod>{lastModified}</lastmod>");
|
|
|
+ builder.AppendLine($" <changefreq>{changeFrequency}</changefreq>");
|
|
|
+ builder.AppendLine($" <priority>{priority}</priority>");
|
|
|
+ builder.AppendLine(" </url>");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|