-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (51 loc) · 2.12 KB
/
server.js
File metadata and controls
59 lines (51 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import express from 'express';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
import { readdirSync, writeFileSync } from 'fs';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 8081;
app.use('/three', express.static(join(__dirname, 'node_modules/three')));
app.use('/rapier', express.static(join(__dirname, 'node_modules/@dimforge/rapier3d-compat')));
app.use('/eruda', express.static(join(__dirname, 'node_modules/eruda')));
app.use('/manifold',express.static(join(__dirname, 'node_modules/manifold-3d')));
app.use('/pathtracer', express.static(join(__dirname, 'node_modules/three-gpu-pathtracer/build')));
app.use('/mesh-bvh', express.static(join(__dirname, 'node_modules/three-mesh-bvh/build')));
app.use('/bank', express.static(join(__dirname, 'bank')));
app.use(express.static(join(__dirname, 'public')));
app.use(express.json({ limit: '50mb' }));
app.put('/dynamics', (req, res) => {
try {
const filePath = join(__dirname, 'public', 'data', 'assembly-dynamics.json');
writeFileSync(filePath, JSON.stringify(req.body, null, 2));
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.put('/mechanics', express.text({ limit: '2mb' }), (req, res) => {
try {
const filePath = join(__dirname, 'public', 'data', 'assembly-mechanics.toml');
writeFileSync(filePath, req.body, 'utf8');
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.put('/bank/:name', (req, res) => {
try {
const name = decodeURIComponent(req.params.name);
const filePath = join(__dirname, 'bank', name + '.json');
writeFileSync(filePath, JSON.stringify(req.body, null, 2));
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.get('/bank-index', (_req, res) => {
const files = readdirSync(join(__dirname, 'bank'))
.filter(f => f.endsWith('.json'))
.map(f => f.replace(/\.json$/, ''));
res.json(files);
});
app.listen(PORT, () => console.log(`rBang running at http://localhost:${PORT}`));