import Fastify from 'fastify'; import formbody from '@fastify/formbody'; import secureSession from '@fastify/secure-session'; import fastifyStatic from '@fastify/static'; import { join } from 'node:path'; import { ROOT, PORT, HOST, SESSION_SECRET } from './config.js'; import indexRoutes from './routes/index.js'; import apiRoutes from './routes/api.js'; import searchRoutes from './routes/search.js'; import { getRatings } from './lib/imdbRatings.js'; import { getPool } from './lib/searchEngine.js'; const fastify = Fastify({ logger: true, trustProxy: true }); await fastify.register(formbody); await fastify.register(secureSession, { // 32 bytes minimum. Use SESSION_SECRET env var in production. secret: SESSION_SECRET.padEnd(32, '0').slice(0, 32), salt: 'proxytmdb-salt-1', cookieName: 'session', cookie: { path: '/', httpOnly: true, sameSite: 'lax', secure: 'auto', }, }); await fastify.register(indexRoutes); await fastify.register(apiRoutes); await fastify.register(searchRoutes); // Serve any other path as a static file from the project root, so that the // "directory listing" links keep working exactly as they did under Apache. await fastify.register(fastifyStatic, { root: ROOT, serve: true, index: false, list: false, decorateReply: false, prefix: '/', }); // Warm up: load IMDb ratings and spawn search workers eagerly. fastify.ready().then(async () => { try { await getRatings(); getPool('movie'); getPool('tv'); fastify.log.info('Warmup complete'); } catch (err) { fastify.log.warn({ err }, 'Warmup failed'); } }); fastify.listen({ port: PORT, host: HOST }).catch((err) => { fastify.log.error(err); process.exit(1); });