This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
const express = require('express');
|
||||
const { fetchHtml } = require('./scraper');
|
||||
const dbOps = require('./database');
|
||||
const { authenticateApiKey } = require('./middleware');
|
||||
const { isBrowserConnected } = require('./browser');
|
||||
const config = require('./config');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
// Main API endpoint
|
||||
router.get('/', authenticateApiKey, async (req, res) => {
|
||||
const { url, skipCache } = req.query;
|
||||
|
||||
// Validate URL parameter
|
||||
if (!url) {
|
||||
return res.status(400).json({
|
||||
error: 'Missing required parameter: url',
|
||||
});
|
||||
}
|
||||
|
||||
// Validate URL format
|
||||
let urlObj;
|
||||
try {
|
||||
urlObj = new URL(url);
|
||||
} catch (error) {
|
||||
return res.status(400).json({
|
||||
error: 'Invalid URL format',
|
||||
});
|
||||
}
|
||||
|
||||
// Parse skipCache (accept 'true', '1', 'yes', etc.)
|
||||
const shouldSkipCache = skipCache === 'true' || skipCache === '1' || skipCache === 'yes';
|
||||
|
||||
try {
|
||||
const html = await fetchHtml(url, shouldSkipCache);
|
||||
res.setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
res.send(html);
|
||||
} catch (error) {
|
||||
console.error('Request error:', error);
|
||||
res.status(500).json({
|
||||
error: 'Failed to fetch webpage',
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Health check endpoint
|
||||
router.get('/health', async (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
cacheExpiryHours: config.CACHE_EXPIRY_HOURS,
|
||||
browserConnected: isBrowserConnected(),
|
||||
database: dbOps.getDbType(),
|
||||
});
|
||||
});
|
||||
|
||||
// Cache stats endpoint
|
||||
router.get('/cache/stats', authenticateApiKey, async (req, res) => {
|
||||
try {
|
||||
const stats = await dbOps.getStats();
|
||||
res.json(stats);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Clear cache endpoint (optional, for maintenance)
|
||||
router.delete('/cache', authenticateApiKey, async (req, res) => {
|
||||
try {
|
||||
const { url } = req.query;
|
||||
if (url) {
|
||||
const deleted = await dbOps.deleteByUrl(url);
|
||||
res.json({ deleted });
|
||||
} else {
|
||||
// Clear all cache
|
||||
const deleted = await dbOps.deleteAll();
|
||||
res.json({ deleted });
|
||||
}
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user