import Fastify from 'fastify'; import fastifyCookie from '@fastify/cookie'; import fastifyJwt from '@fastify/jwt'; import fastifyMultipart from '@fastify/multipart'; import fastifyFormbody from '@fastify/formbody'; import fastifyStatic from '@fastify/static'; import { join } from 'path'; import { fileURLToPath } from 'url'; import type Database from 'better-sqlite3'; import type { Config } from './config.ts'; import { createLogger } from './middleware/logging.ts'; import { authApiRoutes } from './routes/api/v1/auth.ts'; import { filesApiRoutes } from './routes/api/v1/files.ts'; import { pageRoutes } from './routes/pages.ts'; const __dirname = fileURLToPath(new URL('.', import.meta.url)); interface ServerDeps { config: Config; db: Database.Database; } export function createServer({ config, db }: ServerDeps) { const app = Fastify({ logger: false, trustProxy: config.trustProxy }); const logger = createLogger(config.logFile); app.register(fastifyCookie); app.register(fastifyJwt, { secret: config.jwtSecret, cookie: { cookieName: 'token', signed: false }, }); app.register(fastifyFormbody); app.register(fastifyMultipart, { limits: { fileSize: config.maxFileSize } }); app.register(fastifyStatic, { root: join(__dirname, '..', 'public'), prefix: '/public/', }); const deps = { db, config, logger }; app.register(authApiRoutes, { prefix: '/api/v1/auth', deps }); app.register(filesApiRoutes, { prefix: '/api/v1/files', deps }); app.register(pageRoutes, { prefix: '/', deps }); return app; }