scaffold festival drink tracker (pnpm workspace, Fastify + SQLite, Preact tablet UI, admin)
This commit is contained in:
parent
cf33e6ea04
commit
e0898bea22
34 changed files with 5446 additions and 0 deletions
58
server/src/index.ts
Normal file
58
server/src/index.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import Fastify from 'fastify';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import fastifyCookie from '@fastify/cookie';
|
||||
import { join, dirname } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { existsSync } from 'node:fs';
|
||||
|
||||
import { openDb } from './db.js';
|
||||
import { registerPublicRoutes } from './routes/public.js';
|
||||
import { registerAdminRoutes } from './routes/admin.js';
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
|
||||
const PORT = Number(process.env.PORT ?? 3000);
|
||||
const HOST = process.env.HOST ?? '0.0.0.0';
|
||||
const DB_PATH = process.env.DB_PATH ?? join(process.cwd(), 'wutz.db');
|
||||
|
||||
const app = Fastify({ logger: true });
|
||||
|
||||
await app.register(fastifyCookie);
|
||||
|
||||
const db = openDb(DB_PATH);
|
||||
|
||||
registerPublicRoutes(app, db);
|
||||
registerAdminRoutes(app, db);
|
||||
|
||||
app.get('/healthz', async () => ({ ok: true }));
|
||||
|
||||
// Static client (built by Vite into client/dist)
|
||||
const clientDist = join(__dirname, '..', '..', 'client', 'dist');
|
||||
if (existsSync(clientDist)) {
|
||||
await app.register(fastifyStatic, {
|
||||
root: clientDist,
|
||||
prefix: '/',
|
||||
wildcard: false,
|
||||
});
|
||||
app.setNotFoundHandler((req, reply) => {
|
||||
if (req.url.startsWith('/api') || req.url.startsWith('/admin/api')) {
|
||||
reply.code(404).send({ error: 'not found' });
|
||||
return;
|
||||
}
|
||||
if (req.url.startsWith('/admin')) {
|
||||
reply.sendFile('admin.html');
|
||||
return;
|
||||
}
|
||||
reply.sendFile('index.html');
|
||||
});
|
||||
} else {
|
||||
app.log.warn(`client dist not found at ${clientDist} — run \`pnpm --filter client build\``);
|
||||
}
|
||||
|
||||
try {
|
||||
await app.listen({ port: PORT, host: HOST });
|
||||
app.log.info(`listening on http://${HOST}:${PORT}`);
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue