Files
news_service/README.md
zheng b57b64ef95
All checks were successful
Docker Build and Push - News Service / Build and Push News Service Image (push) Successful in 42s
Docker Build and Push - News Service / Build Summary (push) Successful in 0s
feat: implement detail caching for news articles to optimize content fetching
2026-04-30 16:38:02 +08:00

139 lines
2.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# News Service
独立新闻微服务,负责周期性从 Naver Stock 新闻接口拉取新闻并落盘为 JSON 文件,再通过 HTTP 接口提供给前端。
## 功能
- 每小时自动刷新一次新闻数据
- 启动时自动预热数据
- 使用 JSON 文件作为存储,每个分类一个文件
- 提供按分类查询、全部分类查询、手动刷新接口
- 支持 Docker 和 Docker Compose 部署
## 分类
- flashnews
- mainnews
- ranknews
- worldnews
## 目录
```text
news_service/
data/
flashnews.json
mainnews.json
ranknews.json
worldnews.json
detail-cache.json
images/
src/
app/
config/
controllers/
core/
repositories/
services/
```
## 本地启动
1. 配置环境变量
```bash
# 可直接修改 .env.example服务会自动读取
# 或者复制一份 .env 覆盖默认配置
cp .env.example .env
```
2. 安装依赖
```bash
npm install
```
3. 启动服务
```bash
npm start
```
默认地址:`http://localhost:3100`
## 接口
### 健康检查
```http
GET /health
```
### 获取分类列表
```http
GET /api/news/categories
```
### 获取指定分类新闻
```http
GET /api/news?category=flashnews&limit=10
GET /api/news/flashnews?limit=10
```
### 兼容前端现有 NewsAPI 调用
```http
GET /v2/everything?q=flashnews&language=ko&pageSize=10&page=1
GET /v2/top-headlines?category=mainnews&country=ko&pageSize=10
```
返回结构与前端当前使用的 NewsAPI 结构保持一致:
```json
{
"status": "ok",
"totalResults": 10,
"articles": []
}
```
### 获取全部分类新闻
```http
GET /api/news/all?limit=10
```
### 手动刷新
```http
POST /api/news/refresh
POST /api/news/refresh?category=flashnews
```
## 缓存
- 分类列表和前端返回数据会落盘到 `data/<category>.json`
- 详情正文会按详情页 URL 去重缓存到 `data/detail-cache.json`,同一篇新闻后续刷新不会重复抓详情页。
- 正文图片会下载到 `data/images/`,正文 HTML 里的图片地址会替换成 `/api/news/images/<file>`
## Docker
```bash
docker compose up -d --build
```
如果前端和新闻服务需要一起对外提供访问,建议在项目根目录使用组合编排:
```bash
cd /Users/wjp/Projects/juYou
docker compose -f docker-compose.news-stack.yml up -d --build
```
这样前端容器中的 Nginx 会把同源路径 `/newsapi/*` 代理到容器网络中的 `news-service:3100`,浏览器不会直接访问外部新闻接口,因此不会触发跨域限制。
## 前端接入建议
前端如果继续使用原来的 `/newsapi/v2/*` 请求方式,只需要把代理目标指向本服务即可,不需要修改新闻请求代码。