|
| 1 | +package com.hihat.blog.controller; |
| 2 | + |
| 3 | +import com.hihat.blog.domain.Article; |
| 4 | +import com.hihat.blog.repository.BlogRepository; |
| 5 | +import jakarta.servlet.http.HttpServletRequest; |
| 6 | +import lombok.RequiredArgsConstructor; |
| 7 | +import org.springframework.http.MediaType; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.ResponseBody; |
| 10 | +import org.springframework.web.bind.annotation.RestController; |
| 11 | + |
| 12 | +import java.net.URLEncoder; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.time.format.DateTimeFormatter; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +@RestController |
| 19 | +@RequiredArgsConstructor |
| 20 | +public class SeoController { |
| 21 | + private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ISO_LOCAL_DATE; |
| 22 | + private final BlogRepository blogRepository; |
| 23 | + |
| 24 | + @GetMapping(value = "/robots.txt", produces = MediaType.TEXT_PLAIN_VALUE) |
| 25 | + @ResponseBody |
| 26 | + public String robots(HttpServletRequest request) { |
| 27 | + String baseUrl = resolveBaseUrl(request); |
| 28 | + return "User-agent: *\n" + |
| 29 | + "Allow: /\n" + |
| 30 | + "Sitemap: " + baseUrl + "/sitemap.xml\n"; |
| 31 | + } |
| 32 | + |
| 33 | + @GetMapping(value = "/sitemap.xml", produces = MediaType.APPLICATION_XML_VALUE) |
| 34 | + @ResponseBody |
| 35 | + public String sitemap(HttpServletRequest request) { |
| 36 | + String baseUrl = resolveBaseUrl(request); |
| 37 | + StringBuilder xml = new StringBuilder(); |
| 38 | + xml.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); |
| 39 | + xml.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">"); |
| 40 | + |
| 41 | + appendUrl(xml, baseUrl + "/", null); |
| 42 | + appendUrl(xml, baseUrl + "/articles?type=" + encodeParam("이론정리"), null); |
| 43 | + appendUrl(xml, baseUrl + "/articles?type=" + encodeParam("문제풀이"), null); |
| 44 | + |
| 45 | + List<Article> articles = blogRepository.findAll(); |
| 46 | + for (Article article : articles) { |
| 47 | + LocalDateTime lastmod = article.getUpdatedAt() != null ? article.getUpdatedAt() : article.getCreatedAt(); |
| 48 | + appendUrl(xml, baseUrl + "/articles/" + article.getId(), lastmod); |
| 49 | + } |
| 50 | + |
| 51 | + xml.append("</urlset>"); |
| 52 | + return xml.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + private void appendUrl(StringBuilder xml, String loc, LocalDateTime lastmod) { |
| 56 | + xml.append("<url>"); |
| 57 | + xml.append("<loc>").append(escapeXml(loc)).append("</loc>"); |
| 58 | + if (lastmod != null) { |
| 59 | + xml.append("<lastmod>").append(lastmod.format(DATE_FORMAT)).append("</lastmod>"); |
| 60 | + } |
| 61 | + xml.append("</url>"); |
| 62 | + } |
| 63 | + |
| 64 | + private String resolveBaseUrl(HttpServletRequest request) { |
| 65 | + String scheme = headerValue(request, "X-Forwarded-Proto"); |
| 66 | + String host = headerValue(request, "X-Forwarded-Host"); |
| 67 | + |
| 68 | + if (scheme == null || scheme.isBlank()) { |
| 69 | + scheme = request.getScheme(); |
| 70 | + } |
| 71 | + |
| 72 | + if (host == null || host.isBlank()) { |
| 73 | + host = request.getHeader("Host"); |
| 74 | + } |
| 75 | + |
| 76 | + if (host == null || host.isBlank()) { |
| 77 | + host = request.getServerName(); |
| 78 | + int port = request.getServerPort(); |
| 79 | + if (("http".equalsIgnoreCase(scheme) && port != 80) || ("https".equalsIgnoreCase(scheme) && port != 443)) { |
| 80 | + host = host + ":" + port; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return scheme + "://" + host; |
| 85 | + } |
| 86 | + |
| 87 | + private String encodeParam(String value) { |
| 88 | + return URLEncoder.encode(value, StandardCharsets.UTF_8); |
| 89 | + } |
| 90 | + |
| 91 | + private String escapeXml(String value) { |
| 92 | + return value.replace("&", "&").replace("<", "<").replace(">", ">"); |
| 93 | + } |
| 94 | + |
| 95 | + private String headerValue(HttpServletRequest request, String name) { |
| 96 | + String value = request.getHeader(name); |
| 97 | + return value == null ? null : value.split(",")[0].trim(); |
| 98 | + } |
| 99 | +} |
0 commit comments