46 lines
1.1 KiB
JavaScript
46 lines
1.1 KiB
JavaScript
|
const express = require('express');
|
||
|
const { Level } = require('level');
|
||
|
const {
|
||
|
createHash,
|
||
|
} = require('node:crypto');
|
||
|
|
||
|
require('dotenv').config();
|
||
|
|
||
|
const app = express();
|
||
|
const port = process.env.PORT || 3000;
|
||
|
const dataDir = process.env.DATA_DIR || 'data';
|
||
|
|
||
|
const db = new Level(`${dataDir}/forum`, { valueEncoding: 'json' });
|
||
|
|
||
|
app.use(express.json());
|
||
|
|
||
|
app.post('/write', async (req, res) => {
|
||
|
const { body: data } = req;
|
||
|
// TODO: Require author signature
|
||
|
// Compute content hash
|
||
|
const hash = createHash('sha256').update(JSON.stringify(data)).digest('base64url');
|
||
|
console.log('write', hash);
|
||
|
// Store content
|
||
|
db.put(hash, data);
|
||
|
// Return hash
|
||
|
res.send(hash);
|
||
|
});
|
||
|
|
||
|
app.get('/read/:hash', async (req, res) => {
|
||
|
// Fetch content
|
||
|
const { hash } = req.params;
|
||
|
const data = await db.get(req.params.hash);
|
||
|
console.log('read', hash);
|
||
|
// Return content
|
||
|
res.json(data);
|
||
|
});
|
||
|
|
||
|
app.get('*', (req, res) => {
|
||
|
console.log(`req.path: ${req.path}`);
|
||
|
res.send('Hello World!');
|
||
|
});
|
||
|
|
||
|
app.listen(port, () => {
|
||
|
console.log(`Listening on port ${port}`);
|
||
|
});
|