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:
2026-04-30 12:10:00 +08:00
commit 3d891d781c
21 changed files with 1901 additions and 0 deletions

7
.dockerignore Normal file
View File

@@ -0,0 +1,7 @@
node_modules
npm-debug.log
.env
data/*.json
!data/.gitkeep
.git
.DS_Store

7
.env.example Normal file
View File

@@ -0,0 +1,7 @@
PORT=3100
NEWS_API_KEY=03f614876f0645948cb9bbce1661f4b2
NEWS_API_BASE_URL=https://newsapi.org/v2/everything
NEWS_API_LANGUAGE=ko
NEWS_PAGE_SIZE=20
NEWS_REFRESH_CRON=0 * * * *
DATA_DIR=./data

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
node_modules/
.env
data/*.json
!data/.gitkeep

14
Dockerfile Normal file
View File

@@ -0,0 +1,14 @@
FROM node:20-alpine
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm install --omit=dev
COPY . ./
RUN mkdir -p /app/data
EXPOSE 3100
CMD ["npm", "start"]

130
README.md Normal file
View 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/*` 请求方式,只需要把代理目标指向本服务即可,不需要修改新闻请求代码。

21
docker-compose.yml Normal file
View File

@@ -0,0 +1,21 @@
version: '3.9'
services:
news-service:
build: .
container_name: news-service
env_file:
- .env.example
ports:
- "3100:3100"
environment:
PORT: 3100
NEWS_API_KEY: ${NEWS_API_KEY}
NEWS_API_BASE_URL: ${NEWS_API_BASE_URL:-https://newsapi.org/v2/everything}
NEWS_API_LANGUAGE: ${NEWS_API_LANGUAGE:-en}
NEWS_PAGE_SIZE: ${NEWS_PAGE_SIZE:-20}
NEWS_REFRESH_CRON: ${NEWS_REFRESH_CRON:-0 * * * *}
DATA_DIR: ${DATA_DIR:-/app/data}
volumes:
- ./data:/app/data
restart: unless-stopped

855
package-lock.json generated Normal file
View File

@@ -0,0 +1,855 @@
{
"name": "news-service",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "news-service",
"version": "1.0.0",
"dependencies": {
"express": "^4.21.2",
"node-cron": "^4.2.1"
},
"engines": {
"node": ">=20.0.0"
}
},
"node_modules/accepts": {
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.8.tgz",
"integrity": "sha512-PYAthTa2m2VKxuvSD3DPC/Gy+U+sOA1LAuT8mkmRuvw+NACSaeXEQ+NHcVF7rONl6qcaxV3Uuemwawk+7+SJLw==",
"license": "MIT",
"dependencies": {
"mime-types": "~2.1.34",
"negotiator": "0.6.3"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha512-PCVAQswWemu6UdxsDFFX/+gVeYqKAod3D3UVm91jHwynguOwAvYPhx8nNlM++NqRcK6CxxpUafjmhIdKiHibqg==",
"license": "MIT"
},
"node_modules/body-parser": {
"version": "1.20.5",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.5.tgz",
"integrity": "sha512-3grm+/2tUOvu2cjJkvsIxrv/wVpfXQW4PsQHYm7yk4vfpu7Ekl6nEsYBoJUL6qDwZUx8wUhQ8tR2qz+ad9c9OA==",
"license": "MIT",
"dependencies": {
"bytes": "~3.1.2",
"content-type": "~1.0.5",
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "~1.2.0",
"http-errors": "~2.0.1",
"iconv-lite": "~0.4.24",
"on-finished": "~2.4.1",
"qs": "~6.15.1",
"raw-body": "~2.5.3",
"type-is": "~1.6.18",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/body-parser/node_modules/qs": {
"version": "6.15.1",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.15.1.tgz",
"integrity": "sha512-6YHEFRL9mfgcAvql/XhwTvf5jKcOiiupt2FiJxHkiX1z4j7WL8J/jRHYLluORvc1XxB5rV20KoeK00gVJamspg==",
"license": "BSD-3-Clause",
"dependencies": {
"side-channel": "^1.1.0"
},
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/bytes": {
"version": "3.1.2",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.2.tgz",
"integrity": "sha512-/Nf7TyzTx6S3yRJObOAV7956r8cr2+Oj8AC5dt8wSP3BQAoeX58NoHyCU8P8zGkNXStjTSi6fzO6F0pBdcYbEg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/call-bind-apply-helpers": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/call-bound": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/call-bound/-/call-bound-1.0.4.tgz",
"integrity": "sha512-+ys997U96po4Kx/ABpBCqhA9EuxJaQWDQg7295H4hBphv3IZg0boBKuwYpt4YXp6MZ5AmZQnU/tyMTlRpaSejg==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.2",
"get-intrinsic": "^1.3.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/content-disposition": {
"version": "0.5.4",
"resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.4.tgz",
"integrity": "sha512-FveZTNuGw04cxlAiWbzi6zTAL/lhehaWbTtgluJh4/E95DqMwTmha3KZN1aAWA8cFIhHzMZUvLevkw5Rqk+tSQ==",
"license": "MIT",
"dependencies": {
"safe-buffer": "5.2.1"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/content-type": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.5.tgz",
"integrity": "sha512-nTjqfcBFEipKdXCv4YDQWCfmcLZKm81ldF0pAopTvyrFGVbcR6P/VAAd5G7N+0tTr8QqiU0tFadD6FK4NtJwOA==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cookie": {
"version": "0.7.2",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.2.tgz",
"integrity": "sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cookie-signature": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz",
"integrity": "sha512-NXdYc3dLr47pBkpUCHtKSwIOQXLVn8dZEuywboCOJY/osA0wFSLlSawr3KN8qXJEyX66FcONTH8EIlVuK0yyFA==",
"license": "MIT"
},
"node_modules/debug": {
"version": "2.6.9",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
"integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==",
"license": "MIT",
"dependencies": {
"ms": "2.0.0"
}
},
"node_modules/depd": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz",
"integrity": "sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/destroy": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/destroy/-/destroy-1.2.0.tgz",
"integrity": "sha512-2sJGJTaXIIaR1w4iJSNoN0hnMY7Gpc/n8D4qSCJw8QqFWXf7cuAgnEHxBpweaVcPevC2l3KpjYCx3NypQQgaJg==",
"license": "MIT",
"engines": {
"node": ">= 0.8",
"npm": "1.2.8000 || >= 1.4.16"
}
},
"node_modules/dunder-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.1",
"es-errors": "^1.3.0",
"gopd": "^1.2.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/ee-first": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
"integrity": "sha512-WMwm9LhRUo+WUaRN+vRuETqG89IgZphVSNkdFgeb6sS/E4OrDIN7t48CAewSHXc6C8lefD8KKfr5vY61brQlow==",
"license": "MIT"
},
"node_modules/encodeurl": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-2.0.0.tgz",
"integrity": "sha512-Q0n9HRi4m6JuGIV1eFlmvJB7ZEVxu93IrMyiMsGC0lrMJMWzRgx6WGquyfQgZVb31vhGgXnfmPNNXmxnOkRBrg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/es-define-property": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-errors": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/es-object-atoms": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/escape-html": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz",
"integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==",
"license": "MIT"
},
"node_modules/etag": {
"version": "1.8.1",
"resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz",
"integrity": "sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/express": {
"version": "4.22.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.22.1.tgz",
"integrity": "sha512-F2X8g9P1X7uCPZMA3MVf9wcTqlyNp7IhH5qPCI0izhaOIYXaW9L535tGA3qmjRzpH+bZczqq7hVKxTR4NWnu+g==",
"license": "MIT",
"dependencies": {
"accepts": "~1.3.8",
"array-flatten": "1.1.1",
"body-parser": "~1.20.3",
"content-disposition": "~0.5.4",
"content-type": "~1.0.4",
"cookie": "~0.7.1",
"cookie-signature": "~1.0.6",
"debug": "2.6.9",
"depd": "2.0.0",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"finalhandler": "~1.3.1",
"fresh": "~0.5.2",
"http-errors": "~2.0.0",
"merge-descriptors": "1.0.3",
"methods": "~1.1.2",
"on-finished": "~2.4.1",
"parseurl": "~1.3.3",
"path-to-regexp": "~0.1.12",
"proxy-addr": "~2.0.7",
"qs": "~6.14.0",
"range-parser": "~1.2.1",
"safe-buffer": "5.2.1",
"send": "~0.19.0",
"serve-static": "~1.16.2",
"setprototypeof": "1.2.0",
"statuses": "~2.0.1",
"type-is": "~1.6.18",
"utils-merge": "1.0.1",
"vary": "~1.1.2"
},
"engines": {
"node": ">= 0.10.0"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/finalhandler": {
"version": "1.3.2",
"resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.3.2.tgz",
"integrity": "sha512-aA4RyPcd3badbdABGDuTXCMTtOneUCAYH/gxoYRTZlIJdF0YPWuGqiAsIrhNnnqdXGswYk6dGujem4w80UJFhg==",
"license": "MIT",
"dependencies": {
"debug": "2.6.9",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"on-finished": "~2.4.1",
"parseurl": "~1.3.3",
"statuses": "~2.0.2",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/forwarded": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
"integrity": "sha512-buRG0fpBtRHSTCOASe6hD258tEubFoRLb4ZNA6NxMVHNw2gOcwHo9wyablzMzOA5z9xA9L1KNjk/Nt6MT9aYow==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/fresh": {
"version": "0.5.2",
"resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz",
"integrity": "sha512-zJ2mQYM18rEFOudeV4GShTGIQ7RbzA7ozbU9I/XBpm7kqgMywgmylMwXHxZJmkVoYkna9d2pVXVXPdYTP9ej8Q==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/function-bind": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-intrinsic": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
"license": "MIT",
"dependencies": {
"call-bind-apply-helpers": "^1.0.2",
"es-define-property": "^1.0.1",
"es-errors": "^1.3.0",
"es-object-atoms": "^1.1.1",
"function-bind": "^1.1.2",
"get-proto": "^1.0.1",
"gopd": "^1.2.0",
"has-symbols": "^1.1.0",
"hasown": "^2.0.2",
"math-intrinsics": "^1.1.0"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/get-proto": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
"license": "MIT",
"dependencies": {
"dunder-proto": "^1.0.1",
"es-object-atoms": "^1.0.0"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/gopd": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/has-symbols": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/hasown": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.3.tgz",
"integrity": "sha512-ej4AhfhfL2Q2zpMmLo7U1Uv9+PyhIZpgQLGT1F9miIGmiCJIoCgSmczFdrc97mWT4kVY72KA+WnnhJ5pghSvSg==",
"license": "MIT",
"dependencies": {
"function-bind": "^1.1.2"
},
"engines": {
"node": ">= 0.4"
}
},
"node_modules/http-errors": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/http-errors/-/http-errors-2.0.1.tgz",
"integrity": "sha512-4FbRdAX+bSdmo4AUFuS0WNiPz8NgFt+r8ThgNWmlrjQjt1Q7ZR9+zTlce2859x4KSXrwIsaeTqDoKQmtP8pLmQ==",
"license": "MIT",
"dependencies": {
"depd": "~2.0.0",
"inherits": "~2.0.4",
"setprototypeof": "~1.2.0",
"statuses": "~2.0.2",
"toidentifier": "~1.0.1"
},
"engines": {
"node": ">= 0.8"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/express"
}
},
"node_modules/iconv-lite": {
"version": "0.4.24",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz",
"integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==",
"license": "MIT",
"dependencies": {
"safer-buffer": ">= 2.1.2 < 3"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"license": "ISC"
},
"node_modules/ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
"license": "MIT",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/math-intrinsics": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
}
},
"node_modules/media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
"integrity": "sha512-dq+qelQ9akHpcOl/gUVRTxVIOkAJ1wR3QAvb4RsVjS8oVoFjDGTc679wJYmUmknUF5HwMLOgb5O+a3KxfWapPQ==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/merge-descriptors": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz",
"integrity": "sha512-gaNvAS7TZ897/rVaZ0nMtAyxNyi/pdbjbAwUpFQpN70GqnVfOiXpeUUMKRBmzXaSQ8DdTX4/0ms62r2K+hE6mQ==",
"license": "MIT",
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/methods": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz",
"integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
"license": "MIT",
"bin": {
"mime": "cli.js"
},
"engines": {
"node": ">=4"
}
},
"node_modules/mime-db": {
"version": "1.52.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/mime-types": {
"version": "2.1.35",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
"license": "MIT",
"dependencies": {
"mime-db": "1.52.0"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/ms": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz",
"integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==",
"license": "MIT"
},
"node_modules/negotiator": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.3.tgz",
"integrity": "sha512-+EUsqGPLsM+j/zdChZjsnX51g4XrHFOIXwfnCVPGlQk/k5giakcKsuxCObBRu6DSm9opw/O6slWbJdghQM4bBg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/node-cron": {
"version": "4.2.1",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-4.2.1.tgz",
"integrity": "sha512-lgimEHPE/QDgFlywTd8yTR61ptugX3Qer29efeyWw2rv259HtGBNn1vZVmp8lB9uo9wC0t/AT4iGqXxia+CJFg==",
"license": "ISC",
"engines": {
"node": ">=6.0.0"
}
},
"node_modules/object-inspect": {
"version": "1.13.4",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz",
"integrity": "sha512-W67iLl4J2EXEGTbfeHCffrjDfitvLANg0UlX3wFUUSTx92KXRFegMHUVgSqE+wvhAbi4WqjGg9czysTV2Epbew==",
"license": "MIT",
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/on-finished": {
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.4.1.tgz",
"integrity": "sha512-oVlzkg3ENAhCk2zdv7IJwd/QUD4z2RxRwpkcGY8psCVcCYZNq4wYnVWALHM+brtuJjePWiYF/ClmuDr8Ch5+kg==",
"license": "MIT",
"dependencies": {
"ee-first": "1.1.1"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
"integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/path-to-regexp": {
"version": "0.1.13",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.13.tgz",
"integrity": "sha512-A/AGNMFN3c8bOlvV9RreMdrv7jsmF9XIfDeCd87+I8RNg6s78BhJxMu69NEMHBSJFxKidViTEdruRwEk/WIKqA==",
"license": "MIT"
},
"node_modules/proxy-addr": {
"version": "2.0.7",
"resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz",
"integrity": "sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==",
"license": "MIT",
"dependencies": {
"forwarded": "0.2.0",
"ipaddr.js": "1.9.1"
},
"engines": {
"node": ">= 0.10"
}
},
"node_modules/qs": {
"version": "6.14.2",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.14.2.tgz",
"integrity": "sha512-V/yCWTTF7VJ9hIh18Ugr2zhJMP01MY7c5kh4J870L7imm6/DIzBsNLTXzMwUA3yZ5b/KBqLx8Kp3uRvd7xSe3Q==",
"license": "BSD-3-Clause",
"dependencies": {
"side-channel": "^1.1.0"
},
"engines": {
"node": ">=0.6"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/range-parser": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz",
"integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==",
"license": "MIT",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/raw-body": {
"version": "2.5.3",
"resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.3.tgz",
"integrity": "sha512-s4VSOf6yN0rvbRZGxs8Om5CWj6seneMwK3oDb4lWDH0UPhWcxwOWw5+qk24bxq87szX1ydrwylIOp2uG1ojUpA==",
"license": "MIT",
"dependencies": {
"bytes": "~3.1.2",
"http-errors": "~2.0.1",
"iconv-lite": "~0.4.24",
"unpipe": "~1.0.0"
},
"engines": {
"node": ">= 0.8"
}
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
"integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"license": "MIT"
},
"node_modules/send": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/send/-/send-0.19.2.tgz",
"integrity": "sha512-VMbMxbDeehAxpOtWJXlcUS5E8iXh6QmN+BkRX1GARS3wRaXEEgzCcB10gTQazO42tpNIya8xIyNx8fll1OFPrg==",
"license": "MIT",
"dependencies": {
"debug": "2.6.9",
"depd": "2.0.0",
"destroy": "1.2.0",
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"etag": "~1.8.1",
"fresh": "~0.5.2",
"http-errors": "~2.0.1",
"mime": "1.6.0",
"ms": "2.1.3",
"on-finished": "~2.4.1",
"range-parser": "~1.2.1",
"statuses": "~2.0.2"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/send/node_modules/ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"license": "MIT"
},
"node_modules/serve-static": {
"version": "1.16.3",
"resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.16.3.tgz",
"integrity": "sha512-x0RTqQel6g5SY7Lg6ZreMmsOzncHFU7nhnRWkKgWuMTu5NN0DR5oruckMqRvacAN9d5w6ARnRBXl9xhDCgfMeA==",
"license": "MIT",
"dependencies": {
"encodeurl": "~2.0.0",
"escape-html": "~1.0.3",
"parseurl": "~1.3.3",
"send": "~0.19.1"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/setprototypeof": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.2.0.tgz",
"integrity": "sha512-E5LDX7Wrp85Kil5bhZv46j8jOeboKq5JMmYM3gVGdGH8xFpPWXUMsNrlODCrkoxMEeNi/XZIwuRvY4XNwYMJpw==",
"license": "ISC"
},
"node_modules/side-channel": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz",
"integrity": "sha512-ZX99e6tRweoUXqR+VBrslhda51Nh5MTQwou5tnUDgbtyM0dBgmhEDtWGP/xbKn6hqfPRHujUNwz5fy/wbbhnpw==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"object-inspect": "^1.13.3",
"side-channel-list": "^1.0.0",
"side-channel-map": "^1.0.1",
"side-channel-weakmap": "^1.0.2"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-list": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/side-channel-list/-/side-channel-list-1.0.1.tgz",
"integrity": "sha512-mjn/0bi/oUURjc5Xl7IaWi/OJJJumuoJFQJfDDyO46+hBWsfaVM65TBHq2eoZBhzl9EchxOijpkbRC8SVBQU0w==",
"license": "MIT",
"dependencies": {
"es-errors": "^1.3.0",
"object-inspect": "^1.13.4"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-map": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/side-channel-map/-/side-channel-map-1.0.1.tgz",
"integrity": "sha512-VCjCNfgMsby3tTdo02nbjtM/ewra6jPHmpThenkTYh8pG9ucZ/1P8So4u4FGBek/BjpOVsDCMoLA/iuBKIFXRA==",
"license": "MIT",
"dependencies": {
"call-bound": "^1.0.2",
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.5",
"object-inspect": "^1.13.3"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel-weakmap": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/side-channel-weakmap/-/side-channel-weakmap-1.0.2.tgz",
"integrity": "sha512-WPS/HvHQTYnHisLo9McqBHOJk2FkHO/tlpvldyrnem4aeQp4hai3gythswg6p01oSoTl58rcpiFAjF2br2Ak2A==",
"license": "MIT",
"dependencies": {
"call-bound": "^1.0.2",
"es-errors": "^1.3.0",
"get-intrinsic": "^1.2.5",
"object-inspect": "^1.13.3",
"side-channel-map": "^1.0.1"
},
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/statuses": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/statuses/-/statuses-2.0.2.tgz",
"integrity": "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/toidentifier": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.1.tgz",
"integrity": "sha512-o5sSPKEkg/DIQNmH43V0/uerLrpzVedkUh8tGNvaeXpfpuwjKenlSox/2O/BTlZUtEe+JG7s5YhEz608PlAHRA==",
"license": "MIT",
"engines": {
"node": ">=0.6"
}
},
"node_modules/type-is": {
"version": "1.6.18",
"resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz",
"integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==",
"license": "MIT",
"dependencies": {
"media-typer": "0.3.0",
"mime-types": "~2.1.24"
},
"engines": {
"node": ">= 0.6"
}
},
"node_modules/unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
"integrity": "sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
},
"node_modules/utils-merge": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz",
"integrity": "sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==",
"license": "MIT",
"engines": {
"node": ">= 0.4.0"
}
},
"node_modules/vary": {
"version": "1.1.2",
"resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz",
"integrity": "sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==",
"license": "MIT",
"engines": {
"node": ">= 0.8"
}
}
}
}

18
package.json Normal file
View File

@@ -0,0 +1,18 @@
{
"name": "news-service",
"version": "1.0.0",
"description": "Standalone news microservice with scheduled sync and JSON storage",
"type": "commonjs",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"dev": "node --watch src/index.js"
},
"engines": {
"node": ">=20.0.0"
},
"dependencies": {
"express": "^4.21.2",
"node-cron": "^4.2.1"
}
}

View File

@@ -0,0 +1,98 @@
const express = require('express')
const { CategoryNewsRepository } = require('../repositories/CategoryNewsRepository')
const { NewsApiClient } = require('../services/NewsApiClient')
const { NewsStorageService } = require('../services/NewsStorageService')
const { NewsRefreshScheduler } = require('../services/NewsRefreshScheduler')
const { NewsController } = require('../controllers/NewsController')
const { NewsApiCompatibilityController } = require('../controllers/NewsApiCompatibilityController')
const { NewsCategoryCatalog } = require('../config/CategoryCatalog')
const { ConsoleLogger } = require('../services/ConsoleLogger')
/**
* NewsApplication
*
* Composes the service graph, configures Express, and manages lifecycle.
*/
class NewsApplication {
constructor(runtimeConfig) {
this._runtimeConfig = runtimeConfig
this._logger = new ConsoleLogger()
this._categoryCatalog = new NewsCategoryCatalog()
this._repository = new CategoryNewsRepository(runtimeConfig.dataDirectory)
this._apiClient = new NewsApiClient(
runtimeConfig.apiBaseUrl,
runtimeConfig.apiKey,
runtimeConfig.language,
runtimeConfig.pageSize
)
this._storageService = new NewsStorageService(this._categoryCatalog, this._repository, this._apiClient)
this._scheduler = new NewsRefreshScheduler(this._storageService, runtimeConfig.refreshCron, this._logger)
this._express = express()
this._server = null
}
/**
* @returns {Promise<void>}
*/
async start() {
this._configureHttpPipeline()
await this._scheduler.start()
await new Promise((resolve) => {
this._server = this._express.listen(this._runtimeConfig.port, () => {
this._logger.info('News service started', { port: this._runtimeConfig.port })
resolve()
})
})
}
/**
* @returns {Promise<void>}
*/
async stop() {
this._scheduler.stop()
if (!this._server) {
return
}
await new Promise((resolve, reject) => {
this._server.close((error) => {
if (error) {
reject(error)
return
}
resolve()
})
})
}
/**
* @returns {void}
*/
_configureHttpPipeline() {
this._express.use(express.json())
this._express.get('/health', (_request, response) => {
response.json({
status: 'ok',
service: 'news-service',
timestamp: new Date().toISOString()
})
})
const newsController = new NewsController(this._storageService)
const newsApiCompatibilityController = new NewsApiCompatibilityController(this._storageService)
this._express.use('/api/news', newsController.router)
this._express.use('/v2', newsApiCompatibilityController.router)
this._express.use((error, _request, response, _next) => {
this._logger.error('Request processing failed', error)
response.status(500).json({
status: 'error',
message: error.message || 'Internal server error'
})
})
}
}
module.exports = {
NewsApplication
}

View File

@@ -0,0 +1,55 @@
/**
* NewsCategory
*
* Immutable category model used across the application.
*/
class NewsCategory {
/**
* @param {string} key
* @param {string} query
* @param {string} fileName
* @param {string} label
*/
constructor(key, query, fileName, label) {
this.key = key
this.query = query
this.fileName = fileName
this.label = label
}
}
/**
* NewsCategoryCatalog
*
* Encapsulates all supported news categories and lookup behavior.
*/
class NewsCategoryCatalog {
constructor() {
this._categories = [
new NewsCategory('finance', 'finance', 'finance.json', 'Finance'),
new NewsCategory('business', 'business', 'business.json', 'Business'),
new NewsCategory('technology', 'technology', 'technology.json', 'Technology'),
new NewsCategory('market', 'market', 'market.json', 'Market')
]
}
/**
* @returns {NewsCategory[]}
*/
all() {
return [...this._categories]
}
/**
* @param {string} key
* @returns {NewsCategory | null}
*/
getByKey(key) {
return this._categories.find((item) => item.key === key) || null
}
}
module.exports = {
NewsCategory,
NewsCategoryCatalog
}

View File

@@ -0,0 +1,31 @@
const path = require('path')
/**
* ServiceConfig
*
* Centralizes environment parsing and default values.
*/
class ServiceConfig {
constructor(env = process.env) {
this._env = env
}
/**
* @returns {{port: number, apiKey: string, apiBaseUrl: string, language: string, pageSize: number, refreshCron: string, dataDirectory: string}}
*/
toRuntimeConfig() {
return {
port: Number(this._env.PORT || 3100),
apiKey: this._env.NEWS_API_KEY || '',
apiBaseUrl: this._env.NEWS_API_BASE_URL || 'https://newsapi.org/v2/everything',
language: this._env.NEWS_API_LANGUAGE || 'en',
pageSize: Number(this._env.NEWS_PAGE_SIZE || 20),
refreshCron: this._env.NEWS_REFRESH_CRON || '0 * * * *',
dataDirectory: path.resolve(process.cwd(), this._env.DATA_DIR || './data')
}
}
}
module.exports = {
ServiceConfig
}

View File

@@ -0,0 +1,42 @@
const fs = require('fs')
const path = require('path')
/**
* Loads environment variables from local files without requiring extra dependencies.
* Priority: .env, then .env.example for any keys that are still unset.
*
* @returns {void}
*/
function loadEnvironmentFiles() {
const candidateFiles = ['.env', '.env.example']
for (const fileName of candidateFiles) {
const filePath = path.resolve(process.cwd(), fileName)
if (!fs.existsSync(filePath)) {
continue
}
const lines = fs.readFileSync(filePath, 'utf8').split(/\r?\n/)
for (const rawLine of lines) {
const line = rawLine.trim()
if (!line || line.startsWith('#')) {
continue
}
const separatorIndex = line.indexOf('=')
if (separatorIndex === -1) {
continue
}
const key = line.slice(0, separatorIndex).trim()
const value = line.slice(separatorIndex + 1).trim().replace(/^['"]|['"]$/g, '')
if (!(key in process.env)) {
process.env[key] = value
}
}
}
}
module.exports = {
loadEnvironmentFiles
}

View File

@@ -0,0 +1,78 @@
const express = require('express')
/**
* NewsApiCompatibilityController
*
* Mimics the subset of NewsAPI endpoints currently consumed by the front-end.
*/
class NewsApiCompatibilityController {
constructor(storageService) {
this._storageService = storageService
this.router = express.Router()
this._registerRoutes()
}
/**
* @returns {void}
*/
_registerRoutes() {
this.router.get('/everything', this._wrap(async (request, response) => {
const categoryKey = this._normalizeCategoryKey(request.query.q)
const page = this._parsePositiveInteger(request.query.page, 1)
const pageSize = this._parsePositiveInteger(request.query.pageSize, 10)
const document = await this._storageService.getCategoryNewsPage(categoryKey, page, pageSize)
response.json({
status: 'ok',
totalResults: document.total,
articles: document.articles
})
}))
this.router.get('/top-headlines', this._wrap(async (request, response) => {
const categoryKey = this._normalizeCategoryKey(request.query.category || 'business')
const page = this._parsePositiveInteger(request.query.page, 1)
const pageSize = this._parsePositiveInteger(request.query.pageSize, 10)
const document = await this._storageService.getCategoryNewsPage(categoryKey, page, pageSize)
response.json({
status: 'ok',
totalResults: document.total,
articles: document.articles
})
}))
}
/**
* @param {unknown} value
* @returns {string}
*/
_normalizeCategoryKey(value) {
const categoryKey = String(value || 'finance').trim().toLowerCase()
return categoryKey
}
/**
* @param {unknown} value
* @param {number} fallback
* @returns {number}
*/
_parsePositiveInteger(value, fallback) {
const parsed = Number(value)
return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback
}
/**
* @param {(request: import('express').Request, response: import('express').Response) => Promise<void>} handler
* @returns {(request: import('express').Request, response: import('express').Response, next: import('express').NextFunction) => void}
*/
_wrap(handler) {
return (request, response, next) => {
handler(request, response).catch(next)
}
}
}
module.exports = {
NewsApiCompatibilityController
}

View File

@@ -0,0 +1,77 @@
const express = require('express')
/**
* NewsController
*
* Exposes HTTP routes for querying and refreshing cached news data.
*/
class NewsController {
constructor(storageService) {
this._storageService = storageService
this.router = express.Router()
this._registerRoutes()
}
/**
* @returns {void}
*/
_registerRoutes() {
this.router.get('/categories', this._wrap(async (_request, response) => {
response.json({ data: this._storageService.getCategories() })
}))
this.router.get('/all', this._wrap(async (request, response) => {
const limit = this._parseLimit(request.query.limit)
const data = await this._storageService.getAllNews(limit)
response.json({ data })
}))
this.router.get('/', this._wrap(async (request, response) => {
const category = request.query.category
if (!category) {
response.status(400).json({ message: 'category query parameter is required' })
return
}
const limit = this._parseLimit(request.query.limit)
const data = await this._storageService.getCategoryNews(String(category), limit)
response.json({ data })
}))
this.router.get('/:category', this._wrap(async (request, response) => {
const limit = this._parseLimit(request.query.limit)
const data = await this._storageService.getCategoryNews(request.params.category, limit)
response.json({ data })
}))
this.router.post('/refresh', this._wrap(async (request, response) => {
const category = request.query.category
const data = category
? await this._storageService.refreshCategory(String(category))
: await this._storageService.refreshAll()
response.json({ data })
}))
}
/**
* @param {string | undefined} limitValue
* @returns {number}
*/
_parseLimit(limitValue) {
const parsed = Number(limitValue)
return Number.isInteger(parsed) && parsed > 0 ? parsed : 0
}
/**
* @param {(request: import('express').Request, response: import('express').Response) => Promise<void>} handler
* @returns {(request: import('express').Request, response: import('express').Response, next: import('express').NextFunction) => void}
*/
_wrap(handler) {
return (request, response, next) => {
handler(request, response).catch(next)
}
}
}
module.exports = {
NewsController
}

View File

@@ -0,0 +1,63 @@
const fs = require('fs/promises')
const path = require('path')
/**
* AbstractJsonRepository
*
* Defines the storage contract for JSON-backed repositories.
*/
class AbstractJsonRepository {
constructor(dataDirectory) {
if (new.target === AbstractJsonRepository) {
throw new Error('AbstractJsonRepository cannot be instantiated directly')
}
this._dataDirectory = dataDirectory
}
/**
* @returns {Promise<void>}
*/
async ensureDirectory() {
await fs.mkdir(this._dataDirectory, { recursive: true })
}
/**
* @param {string} fileName
* @returns {string}
*/
resolvePath(fileName) {
return path.join(this._dataDirectory, fileName)
}
/**
* @param {string} fileName
* @param {object} payload
* @returns {Promise<void>}
*/
async writeJson(fileName, payload) {
await this.ensureDirectory()
await fs.writeFile(this.resolvePath(fileName), JSON.stringify(payload, null, 2), 'utf8')
}
/**
* @param {string} fileName
* @param {object} fallback
* @returns {Promise<object>}
*/
async readJson(fileName, fallback) {
await this.ensureDirectory()
try {
const fileContent = await fs.readFile(this.resolvePath(fileName), 'utf8')
return JSON.parse(fileContent)
} catch (error) {
if (error.code === 'ENOENT') {
return fallback
}
throw error
}
}
}
module.exports = {
AbstractJsonRepository
}

41
src/index.js Normal file
View File

@@ -0,0 +1,41 @@
const { ServiceConfig } = require('./config/ServiceConfig')
const { loadEnvironmentFiles } = require('./config/loadEnvironmentFiles')
const { NewsApplication } = require('./app/NewsApplication')
/**
* Bootstrap
*
* Starts the news microservice and wires process lifecycle handlers.
*/
class Bootstrap {
constructor() {
loadEnvironmentFiles()
const serviceConfig = new ServiceConfig()
this._application = new NewsApplication(serviceConfig.toRuntimeConfig())
}
/**
* @returns {Promise<void>}
*/
async run() {
await this._application.start()
this._registerShutdownHooks()
}
/**
* @returns {void}
*/
_registerShutdownHooks() {
const shutdown = async () => {
await this._application.stop()
process.exit(0)
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)
}
}
new Bootstrap().run().catch((error) => {
console.error(error)
process.exit(1)
})

View File

@@ -0,0 +1,57 @@
const { AbstractJsonRepository } = require('../core/AbstractJsonRepository')
/**
* CategoryNewsRepository
*
* Stores and retrieves articles for each category in a dedicated JSON file.
*/
class CategoryNewsRepository extends AbstractJsonRepository {
constructor(dataDirectory) {
super(dataDirectory)
}
/**
* @param {import('../config/CategoryCatalog').NewsCategory} category
* @returns {Promise<object>}
*/
async readCategory(category) {
return this.readJson(category.fileName, this._createEmptyDocument(category))
}
/**
* @param {import('../config/CategoryCatalog').NewsCategory} category
* @param {object[]} articles
* @returns {Promise<object>}
*/
async saveCategory(category, articles) {
const document = {
category: category.key,
query: category.query,
label: category.label,
updatedAt: new Date().toISOString(),
total: articles.length,
articles
}
await this.writeJson(category.fileName, document)
return document
}
/**
* @param {import('../config/CategoryCatalog').NewsCategory} category
* @returns {object}
*/
_createEmptyDocument(category) {
return {
category: category.key,
query: category.query,
label: category.label,
updatedAt: null,
total: 0,
articles: []
}
}
}
module.exports = {
CategoryNewsRepository
}

View File

@@ -0,0 +1,59 @@
/**
* AbstractLogger
*
* Defines the shared logging interface for concrete loggers.
*/
class AbstractLogger {
info() {
throw new Error('info must be implemented by subclasses')
}
error() {
throw new Error('error must be implemented by subclasses')
}
}
/**
* ConsoleLogger
*
* Writes structured logs to stdout and stderr.
*/
class ConsoleLogger extends AbstractLogger {
/**
* @param {string} message
* @param {object} [context]
* @returns {void}
*/
info(message, context = {}) {
console.log(JSON.stringify({ level: 'info', message, context, timestamp: new Date().toISOString() }))
}
/**
* @param {string} message
* @param {Error | object} [error]
* @returns {void}
*/
error(message, error = {}) {
console.error(JSON.stringify({ level: 'error', message, error: this._normalizeError(error), timestamp: new Date().toISOString() }))
}
/**
* @param {Error | object} error
* @returns {object}
*/
_normalizeError(error) {
if (error instanceof Error) {
return {
name: error.name,
message: error.message,
stack: error.stack
}
}
return error
}
}
module.exports = {
AbstractLogger,
ConsoleLogger
}

View File

@@ -0,0 +1,69 @@
/**
* NewsApiClient
*
* Wraps outbound requests to NewsAPI.
*/
class NewsApiClient {
constructor(apiBaseUrl, apiKey, language, pageSize) {
this._apiBaseUrl = apiBaseUrl
this._apiKey = apiKey
this._language = language
this._pageSize = pageSize
}
/**
* @param {import('../config/CategoryCatalog').NewsCategory} category
* @returns {Promise<object[]>}
*/
async fetchArticlesByCategory(category) {
if (!this._apiKey) {
throw new Error('NEWS_API_KEY is required')
}
const url = new URL(this._apiBaseUrl)
url.searchParams.set('q', category.query)
url.searchParams.set('language', this._language)
url.searchParams.set('pageSize', String(this._pageSize))
url.searchParams.set('page', '1')
url.searchParams.set('sortBy', 'publishedAt')
const response = await fetch(url, {
headers: {
'X-Api-Key': this._apiKey
}
})
if (!response.ok) {
const failure = await response.json().catch(() => ({}))
throw new Error(failure.message || `NewsAPI request failed with status ${response.status}`)
}
const payload = await response.json()
if (payload.status !== 'ok') {
throw new Error(payload.message || 'NewsAPI returned a non-ok payload')
}
return (payload.articles || []).map((article) => this._normalizeArticle(article))
}
/**
* @param {object} article
* @returns {object}
*/
_normalizeArticle(article) {
return {
source: article.source || null,
author: article.author || null,
title: article.title || '',
description: article.description || '',
url: article.url || '',
urlToImage: article.urlToImage || '',
publishedAt: article.publishedAt || null,
content: article.content || ''
}
}
}
module.exports = {
NewsApiClient
}

View File

@@ -0,0 +1,51 @@
const cron = require('node-cron')
/**
* NewsRefreshScheduler
*
* Owns startup refresh and hourly refresh scheduling.
*/
class NewsRefreshScheduler {
constructor(storageService, cronExpression, logger) {
this._storageService = storageService
this._cronExpression = cronExpression
this._logger = logger
this._task = null
}
/**
* @returns {Promise<void>}
*/
async start() {
await this._runRefresh('startup')
this._task = cron.schedule(this._cronExpression, async () => {
await this._runRefresh('scheduled')
})
}
/**
* @returns {void}
*/
stop() {
if (this._task) {
this._task.stop()
}
}
/**
* @param {string} trigger
* @returns {Promise<void>}
*/
async _runRefresh(trigger) {
try {
const results = await this._storageService.refreshAll()
this._logger.info(`News refresh completed via ${trigger}`, { categories: results.length })
} catch (error) {
this._logger.error(`News refresh failed via ${trigger}`, error)
}
}
}
module.exports = {
NewsRefreshScheduler
}

View File

@@ -0,0 +1,124 @@
/**
* NewsStorageService
*
* Coordinates category validation, refresh logic, and repository access.
*/
class NewsStorageService {
constructor(categoryCatalog, repository, apiClient) {
this._categoryCatalog = categoryCatalog
this._repository = repository
this._apiClient = apiClient
}
/**
* @returns {Promise<object[]>}
*/
async refreshAll() {
const results = []
for (const category of this._categoryCatalog.all()) {
results.push(await this.refreshCategory(category.key))
}
return results
}
/**
* @param {string} categoryKey
* @returns {Promise<object>}
*/
async refreshCategory(categoryKey) {
const category = this._resolveCategory(categoryKey)
const articles = await this._apiClient.fetchArticlesByCategory(category)
return this._repository.saveCategory(category, articles)
}
/**
* @param {string} categoryKey
* @param {number} limit
* @returns {Promise<object>}
*/
async getCategoryNews(categoryKey, limit) {
const category = this._resolveCategory(categoryKey)
const document = await this._repository.readCategory(category)
return this._applyLimit(document, limit)
}
/**
* @param {string} categoryKey
* @param {number} page
* @param {number} pageSize
* @returns {Promise<object>}
*/
async getCategoryNewsPage(categoryKey, page, pageSize) {
const category = this._resolveCategory(categoryKey)
const document = await this._repository.readCategory(category)
const safePage = Number.isInteger(page) && page > 0 ? page : 1
const safePageSize = Number.isInteger(pageSize) && pageSize > 0 ? pageSize : document.articles.length
const start = (safePage - 1) * safePageSize
const end = start + safePageSize
return {
...document,
total: document.total,
page: safePage,
pageSize: safePageSize,
articles: document.articles.slice(start, end)
}
}
/**
* @param {number} limit
* @returns {Promise<object[]>}
*/
async getAllNews(limit) {
const results = []
for (const category of this._categoryCatalog.all()) {
const document = await this._repository.readCategory(category)
results.push(this._applyLimit(document, limit))
}
return results
}
/**
* @returns {object[]}
*/
getCategories() {
return this._categoryCatalog.all().map((category) => ({
key: category.key,
label: category.label,
query: category.query,
fileName: category.fileName
}))
}
/**
* @param {string} categoryKey
* @returns {import('../config/CategoryCatalog').NewsCategory}
*/
_resolveCategory(categoryKey) {
const category = this._categoryCatalog.getByKey(categoryKey)
if (!category) {
throw new Error(`Unsupported category: ${categoryKey}`)
}
return category
}
/**
* @param {object} document
* @param {number} limit
* @returns {object}
*/
_applyLimit(document, limit) {
if (!limit || limit < 1) {
return document
}
return {
...document,
total: Math.min(document.total, limit),
articles: document.articles.slice(0, limit)
}
}
}
module.exports = {
NewsStorageService
}