ludops-skeleton/apps/api/src/index.ts

36 lines
999 B
TypeScript

import Fastify from 'fastify';
import path from 'path';
import { fileURLToPath } from 'url';
import fastifyStatic from '@fastify/static';
import { AppStatus } from '@ludops/shared';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const fastify = Fastify({ logger: true });
// API Route
fastify.get('/api/status', async (): Promise<AppStatus> => {
return { status: 'OK', version: '1.0.0', database: true };
});
// Serve Frontend (Vite Dist)
const webDistPath = process.env.NODE_ENV === 'production'
? path.join(__dirname, '../web-dist')
: path.join(__dirname, '../../web/dist');
fastify.register(fastifyStatic, {
root: webDistPath,
prefix: '/',
});
// SPA Routing: Redirect everything else to index.html
fastify.setNotFoundHandler((req, reply) => {
if (req.url.startsWith('/api')) {
reply.code(404).send({ error: 'Not Found' });
} else {
reply.sendFile('index.html');
}
});
fastify.listen({ port: 3000, host: '0.0.0.0' });