feat(server): Express bootstrap, /health, ownerOnly on /api, smoke /api/spaces

This commit is contained in:
root
2026-05-31 15:30:50 +10:00
parent 7e55f07689
commit d862eaa3b0
4 changed files with 323 additions and 0 deletions

44
server.js Normal file
View File

@@ -0,0 +1,44 @@
import 'dotenv/config';
import express from 'express';
import { pool } from './lib/db/pool.js';
import { ownerOnly } from './lib/auth/owner.js';
import { log } from './lib/log.js';
import * as spaces from './lib/db/repos/spaces.js';
const VERSION = '2.0.0-alpha.1';
export function createApp() {
const app = express();
app.use(express.json({ limit: '10mb' }));
app.get('/health', async (_req, res) => {
let db_ok = false;
try {
await pool.query('SELECT 1');
db_ok = true;
} catch (e) {
log.error({ err: e }, 'healthcheck db ping failed');
}
res.json({ ok: true, db_ok, version: VERSION });
});
app.use('/api', ownerOnly);
app.get('/api/spaces', async (_req, res) => {
res.json(await spaces.list());
});
app.use((_req, res) => res.status(404).json({ error: { code: 'not_found' } }));
app.use((err, _req, res, _next) => {
log.error({ err }, 'unhandled');
res.status(500).json({ error: { code: 'internal', message: err.message } });
});
return app;
}
if (import.meta.url === `file://${process.argv[1]}`) {
const port = process.env.PORT || 3000;
createApp().listen(port, () => log.info({ port }, 'void-server listening'));
}