File size: 1,438 Bytes
2e54131
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Zero-dependency static server for the Synthia Arsenal directory (HF Docker Space).
const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = process.env.PORT || 7860;
const ROOT = path.join(__dirname, 'public');
const TYPES = {
  '.html':'text/html; charset=utf-8', '.js':'text/javascript; charset=utf-8',
  '.css':'text/css; charset=utf-8', '.json':'application/json; charset=utf-8',
  '.svg':'image/svg+xml', '.png':'image/png', '.ico':'image/x-icon', '.txt':'text/plain; charset=utf-8'
};

http.createServer((req, res) => {
  if (req.url === '/healthz') { res.writeHead(200, {'Content-Type':'text/plain'}); return res.end('ok'); }
  let p = decodeURIComponent(req.url.split('?')[0]);
  if (p === '/' || p === '') p = '/index.html';
  const file = path.normalize(path.join(ROOT, p));
  if (!file.startsWith(ROOT)) { res.writeHead(403); return res.end('forbidden'); }
  fs.readFile(file, (err, buf) => {
    if (err) { res.writeHead(404, {'Content-Type':'text/html'}); return res.end('<h1>404</h1>'); }
    const ext = path.extname(file).toLowerCase();
    const headers = {'Content-Type': TYPES[ext] || 'application/octet-stream'};
    if (ext === '.html') headers['Cache-Control'] = 'no-cache';
    else headers['Cache-Control'] = 'public, max-age=3600';
    res.writeHead(200, headers);
    res.end(buf);
  });
}).listen(PORT, '0.0.0.0', () => console.log('Synthia Arsenal on :' + PORT));