37 lines
906 B
JavaScript
37 lines
906 B
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import { getValue, setValue } from './db.js';
|
|
|
|
const app = express();
|
|
app.use(cors());
|
|
app.use(express.json({ limit: '5mb' }));
|
|
|
|
const PORT = process.env.PORT || 4001;
|
|
const STORAGE_KEY = 'hospital-data-v1';
|
|
|
|
app.get('/api/state', async (req, res) => {
|
|
try {
|
|
const v = await getValue(STORAGE_KEY);
|
|
if (!v) return res.json(null);
|
|
res.json(JSON.parse(v));
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'failed to read state' });
|
|
}
|
|
});
|
|
|
|
app.put('/api/state', async (req, res) => {
|
|
try {
|
|
const body = req.body;
|
|
await setValue(STORAGE_KEY, JSON.stringify(body));
|
|
res.json({ ok: true });
|
|
} catch (err) {
|
|
console.error(err);
|
|
res.status(500).json({ error: 'failed to save state' });
|
|
}
|
|
});
|
|
|
|
app.listen(PORT, () => {
|
|
console.log(`API server listening on http://localhost:${PORT}`);
|
|
});
|