45 lines
1.2 KiB
JavaScript
45 lines
1.2 KiB
JavaScript
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'));
|
|
}
|