mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-12-06 07:41:09 +09:00
feat : IT 헤드라인 API 추가 (GET /api/headlines, GeekNews 피드 30분 메모리 캐시)
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 52s
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 52s
- 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져옴 - 피드 장애 시 직전 목록 유지, 요청마다 재시도하지 않음 - 메인 화면용이라 인증 없이 접근 허용 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
657d92114b
commit
569346ef31
2 files changed
+61
-2
No files matched your search
@@ -31,8 +31,8 @@ public class SecurityConfig {
|
|||||||
.cors(Customizer.withDefaults()) // 아래 corsConfigurationSource 빈 사용
|
.cors(Customizer.withDefaults()) // 아래 corsConfigurationSource 빈 사용
|
||||||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
|
||||||
.authorizeHttpRequests(auth -> auth
|
.authorizeHttpRequests(auth -> auth
|
||||||
// 로그인 API 및 파일 동기화 업로드 API는 인증 없이 누구나 접근 가능하도록 허용
|
// 로그인 API, 파일 동기화 업로드 API, 메인 화면 IT 헤드라인은 인증 없이 누구나 접근 가능하도록 허용
|
||||||
.requestMatchers("/api/login", "/api/sync/upload").permitAll()
|
.requestMatchers("/api/login", "/api/sync/upload", "/api/headlines").permitAll()
|
||||||
.anyRequest().authenticated()
|
.anyRequest().authenticated()
|
||||||
)
|
)
|
||||||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
|
||||||
|
|||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package company.specialsource.controller;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.jsoup.Jsoup;
|
||||||
|
import org.jsoup.nodes.Element;
|
||||||
|
import org.jsoup.parser.Parser;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 메인 화면 IT 헤드라인: GeekNews Atom 피드를 읽어 최근 글 제목·링크를 내려준다.
|
||||||
|
* 브라우저에서 직접 읽으면 CORS로 막혀서 백엔드가 대신 가져온다. 30분 동안 메모리에 보관.
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@RestController
|
||||||
|
public class HeadlineController {
|
||||||
|
|
||||||
|
private static final String FEED_URL = "https://news.hada.io/rss/news";
|
||||||
|
private static final Duration CACHE_TTL = Duration.ofMinutes(30);
|
||||||
|
private static final int LIMIT = 10;
|
||||||
|
|
||||||
|
public record Headline(String title, String link) {}
|
||||||
|
|
||||||
|
private List<Headline> cached = List.of();
|
||||||
|
private Instant fetchedAt = Instant.EPOCH;
|
||||||
|
|
||||||
|
@GetMapping("/api/headlines")
|
||||||
|
public synchronized List<Headline> getHeadlines() {
|
||||||
|
if (Instant.now().isBefore(fetchedAt.plus(CACHE_TTL))) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
// 실패해도 시각은 갱신 → 피드 장애 때 요청마다 다시 시도하지 않고, 직전 목록을 그대로 보여줌
|
||||||
|
fetchedAt = Instant.now();
|
||||||
|
try {
|
||||||
|
cached = Jsoup.connect(FEED_URL) // feedburner로 리다이렉트됨 (Jsoup이 자동으로 따라감)
|
||||||
|
.userAgent("Mozilla/5.0")
|
||||||
|
.timeout(10_000)
|
||||||
|
.parser(Parser.xmlParser())
|
||||||
|
.get()
|
||||||
|
.select("entry")
|
||||||
|
.stream()
|
||||||
|
.limit(LIMIT)
|
||||||
|
.map(e -> new Headline(text(e, "title"), text(e, "id"))) // id = utm 없는 깨끗한 글 주소
|
||||||
|
.toList();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("헤드라인 피드를 가져오지 못했습니다: {}", e.getMessage());
|
||||||
|
}
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String text(Element entry, String tag) {
|
||||||
|
Element el = entry.selectFirst(tag);
|
||||||
|
return el == null ? "" : el.text();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user