Tests / bridge tests (push) Successful in 54s
Tests / writer-pipeline tests (push) Successful in 57s
Tests / frontend lib tests (push) Successful in 54s
Tests / writer-pipeline tests (pull_request) Successful in 43s
Tests / bridge tests (pull_request) Successful in 41s
Tests / frontend lib tests (pull_request) Successful in 40s
- POST /auth/login — validates credentials via WP wp-login.php (server-side), issues HttpOnly np_sid session cookie backed by Redis/in-memory - POST /auth/register — creates subscriber via admin app password, auto-login - POST /auth/logout — destroys session, clears cookie - GET /auth/session — returns current WpSessionUser or 401 - LATEST_COUNT 12 → 24 on /homepage (WP fetch budget already covered the delta) - .env.example: WP_ADMIN_USER, WP_ADMIN_APP_PASSWORD, SESSION_TTL_SECONDS Paperclip-Issue: BIT-26752 Paperclip-Agent: cto
86 lines
2.8 KiB
TypeScript
86 lines
2.8 KiB
TypeScript
import Fastify from 'fastify';
|
|
import cors from '@fastify/cors';
|
|
import rate_limit from '@fastify/rate-limit';
|
|
import { initReporter, reportError } from './lib/errorReporter.js';
|
|
|
|
import { health_routes } from './routes/health.js';
|
|
import { site_config_routes } from './routes/site-config.js';
|
|
import { articles_routes } from './routes/articles.js';
|
|
import { categories_routes } from './routes/categories.js';
|
|
import { homepage_routes } from './routes/homepage.js';
|
|
import { writers_routes } from './routes/writers.js';
|
|
import { fpa_sync_routes } from './routes/fpa-sync.js';
|
|
import { auth_routes } from './routes/auth.js';
|
|
|
|
// BitReport error reporting (env: ERROR_REPORTING_TOKEN, ERROR_REPORTING_ENDPOINT)
|
|
initReporter();
|
|
|
|
const PORT = parseInt(process.env['PORT'] ?? '3200', 10);
|
|
const LOG_LEVEL = process.env['LOG_LEVEL'] ?? 'info';
|
|
|
|
const app = Fastify({
|
|
logger: {
|
|
level: LOG_LEVEL,
|
|
transport: process.env['NODE_ENV'] !== 'production'
|
|
? { target: 'pino-pretty', options: { colorize: true } }
|
|
: undefined,
|
|
},
|
|
disableRequestLogging: false,
|
|
});
|
|
|
|
await app.register(cors, { origin: true });
|
|
|
|
await app.register(rate_limit, {
|
|
global: true,
|
|
max: parseInt(process.env['RATE_LIMIT_MAX'] ?? '100', 10),
|
|
timeWindow: parseInt(process.env['RATE_LIMIT_WINDOW_MS'] ?? '60000', 10),
|
|
keyGenerator: (req) => req.ip,
|
|
});
|
|
|
|
// Global error handler — map known error types to HTTP status codes
|
|
app.setErrorHandler((err, req, reply) => {
|
|
const msg = err.message ?? 'Internal server error';
|
|
const is_not_found = msg.includes('not found') || msg.includes('Not Found');
|
|
const is_wp_down = msg.startsWith('WP API') || err.name === 'TimeoutError' || msg.includes('fetch failed');
|
|
|
|
if (is_not_found) {
|
|
return reply.code(404).send({ error: msg });
|
|
}
|
|
|
|
if (is_wp_down) {
|
|
app.log.warn({ url: req.url, err: msg }, 'WP upstream unavailable');
|
|
return reply.code(503).send({ error: 'Upstream unavailable' });
|
|
}
|
|
|
|
if (err.statusCode && err.statusCode < 500) {
|
|
return reply.code(err.statusCode).send({ error: msg });
|
|
}
|
|
|
|
app.log.error({ url: req.url, err }, 'Unhandled error');
|
|
reportError(err, { url: req.url, method: req.method, route: req.routeOptions?.url });
|
|
return reply.code(500).send({ error: 'Internal server error' });
|
|
});
|
|
|
|
// Register route plugins
|
|
await app.register(health_routes);
|
|
await app.register(site_config_routes);
|
|
await app.register(articles_routes);
|
|
await app.register(categories_routes);
|
|
await app.register(homepage_routes);
|
|
await app.register(writers_routes);
|
|
await app.register(fpa_sync_routes);
|
|
await app.register(auth_routes);
|
|
|
|
// 404 fallback
|
|
app.setNotFoundHandler((_req, reply) => {
|
|
return reply.code(404).send({ error: 'Route not found' });
|
|
});
|
|
|
|
try {
|
|
await app.listen({ port: PORT, host: '0.0.0.0' });
|
|
app.log.info(`Bridge API running on port ${PORT}`);
|
|
} catch (err) {
|
|
app.log.error(err);
|
|
process.exit(1);
|
|
}
|