feat: initialize news service with core functionality
- Add package.json for project configuration and dependencies. - Implement NewsApplication class to manage service lifecycle and HTTP pipeline. - Create CategoryCatalog for managing news categories. - Introduce ServiceConfig for environment variable management. - Load environment variables from .env files. - Develop NewsApiCompatibilityController to mimic NewsAPI endpoints. - Create NewsController for querying and refreshing news data. - Establish AbstractJsonRepository for JSON storage operations. - Implement main entry point in index.js to bootstrap the application. - Create CategoryNewsRepository for category-specific news storage. - Add ConsoleLogger for structured logging. - Implement NewsApiClient for fetching articles from NewsAPI. - Develop NewsRefreshScheduler for scheduling news refresh tasks. - Create NewsStorageService to coordinate news data management.
This commit is contained in:
130
README.md
Normal file
130
README.md
Normal file
@@ -0,0 +1,130 @@
|
||||
# News Service
|
||||
|
||||
独立新闻微服务,负责周期性从 NewsAPI 拉取新闻并落盘为 JSON 文件,再通过 HTTP 接口提供给前端。
|
||||
|
||||
## 功能
|
||||
|
||||
- 每小时自动刷新一次新闻数据
|
||||
- 启动时自动预热数据
|
||||
- 使用 JSON 文件作为存储,每个分类一个文件
|
||||
- 提供按分类查询、全部分类查询、手动刷新接口
|
||||
- 支持 Docker 和 Docker Compose 部署
|
||||
|
||||
## 分类
|
||||
|
||||
- finance
|
||||
- business
|
||||
- technology
|
||||
- market
|
||||
|
||||
## 目录
|
||||
|
||||
```text
|
||||
news_service/
|
||||
data/
|
||||
finance.json
|
||||
business.json
|
||||
technology.json
|
||||
market.json
|
||||
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=finance&limit=10
|
||||
GET /api/news/finance?limit=10
|
||||
```
|
||||
|
||||
### 兼容前端现有 NewsAPI 调用
|
||||
|
||||
```http
|
||||
GET /v2/everything?q=finance&language=ko&pageSize=10&page=1
|
||||
GET /v2/top-headlines?category=business&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=finance
|
||||
```
|
||||
|
||||
## 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,因此不会触发跨域限制。
|
||||
|
||||
## 前端接入建议
|
||||
|
||||
前端如果继续使用原来的 `/newsapi/v2/*` 请求方式,只需要把代理目标指向本服务即可,不需要修改新闻请求代码。
|
||||
Reference in New Issue
Block a user