File size: 1,902 Bytes
315d85e
 
 
871bfa6
315d85e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 极简静态服务器:托管构建好的网站;/api/keys 只返回各 Key 是否已配置(不返回 Key 本身)
const http = require('http'), fs = require('fs'), path = require('path')
const root = path.join(__dirname, 'dist')
const types = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript', '.css': 'text/css', '.svg': 'image/svg+xml', '.png': 'image/png', '.jpg': 'image/jpeg', '.mp4': 'video/mp4', '.mp3': 'audio/mpeg', '.wav': 'audio/wav', '.json': 'application/json', '.ico': 'image/x-icon', '.woff2': 'font/woff2', '.txt': 'text/plain; charset=utf-8', '.md': 'text/markdown; charset=utf-8', '.zip': 'application/zip' }
http.createServer((req, res) => {
  const url = decodeURIComponent(req.url.split('?')[0])
  if (url === '/api/keys') {
    const k = ['TRIPO_API_KEY', 'ELEVENLABS_API_KEY', 'SPRITERRIFIC_API_KEY']
    res.writeHead(200, { 'Content-Type': 'application/json' })
    return res.end(JSON.stringify(Object.fromEntries(k.map((n) => [n, !!process.env[n]]))))
  }
  let f = path.normalize(path.join(root, url))
  if (!f.startsWith(root)) { res.writeHead(403); return res.end() }
  if (!fs.existsSync(f) || fs.statSync(f).isDirectory()) f = path.join(root, 'index.html')
  const stat = fs.statSync(f), type = types[path.extname(f)] || 'application/octet-stream'
  const range = req.headers.range
  if (range && /^bytes=/.test(range)) {
    const [s, e] = range.replace('bytes=', '').split('-'); const start = +s, end = e ? +e : stat.size - 1
    res.writeHead(206, { 'Content-Type': type, 'Content-Range': `bytes ${start}-${end}/${stat.size}`, 'Accept-Ranges': 'bytes', 'Content-Length': end - start + 1 })
    return fs.createReadStream(f, { start, end }).pipe(res)
  }
  res.writeHead(200, { 'Content-Type': type, 'Content-Length': stat.size, 'Accept-Ranges': 'bytes' })
  fs.createReadStream(f).pipe(res)
}).listen(7860, () => console.log('listening 7860'))