fix: update
This commit is contained in:
@@ -5,8 +5,22 @@ const db = require('./db')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
// 数据库 Dashboard:返回各表原始数据
|
||||
router.get('/db/dashboard', (req, res) => {
|
||||
// 简单鉴权:通过环境变量配置的 API_ADMIN_KEY 保护敏感接口(不返回真实密钥)
|
||||
const ADMIN_API_KEY = process.env.API_ADMIN_KEY || ''
|
||||
|
||||
function requireAdmin(req, res, next) {
|
||||
if (!ADMIN_API_KEY) {
|
||||
return res.status(500).json({ error: 'admin key not configured' })
|
||||
}
|
||||
const token = req.headers['x-api-key']
|
||||
if (typeof token !== 'string' || token !== ADMIN_API_KEY) {
|
||||
return res.status(401).json({ error: 'unauthorized' })
|
||||
}
|
||||
return next()
|
||||
}
|
||||
|
||||
// 数据库 Dashboard:返回各表原始数据(需 admin 鉴权)
|
||||
router.get('/db/dashboard', requireAdmin, (req, res) => {
|
||||
try {
|
||||
const tables = [
|
||||
'feedback',
|
||||
@@ -58,8 +72,14 @@ router.get('/db/dashboard', (req, res) => {
|
||||
}
|
||||
})
|
||||
|
||||
// 资讯内容(独立表,供后续消费)
|
||||
// 资讯内容(独立表,供后续消费,可选 admin key;若配置了 ADMIN_API_KEY 则也要求鉴权)
|
||||
router.get('/news', (req, res) => {
|
||||
if (ADMIN_API_KEY) {
|
||||
const token = req.headers['x-api-key']
|
||||
if (typeof token !== 'string' || token !== ADMIN_API_KEY) {
|
||||
return res.status(401).json({ error: 'unauthorized' })
|
||||
}
|
||||
}
|
||||
try {
|
||||
const limit = Math.min(parseInt(req.query.limit, 10) || 50, 200)
|
||||
const rows = db.prepare('SELECT id, title, summary, url, source, published_at, category, severity, created_at FROM news_content ORDER BY published_at DESC LIMIT ?').all(limit)
|
||||
|
||||
Reference in New Issue
Block a user