1
0
Fork 0
cosmic-web/server.js

43 lines
1.4 KiB
JavaScript

const express = require('express')
const app = express()
const port = 3000
var fs = require('fs')
const { bufferFile, wc, head } = require('./utils')
app.engine('cosmic', function (filePath, options, callback) {
fs.readFile(filePath, function (err, content) {
if (err) return callback(err)
var rendered = content.toString()
for (var s in options) {
if (typeof options[s] === 'string') {
rendered = rendered.replace('##' + s + '##', options[s])
}
}
return callback(null, rendered)
})
})
app.set('views', './views') // specify the views directory
app.set('view engine', 'cosmic')
app.use(express.static('/var/cosmic/files'))
app.use(express.static('/var/gopher'))
app.get('/', async function (req, res) {
const intro = bufferFile('/var/gopher/intro.gophermap')
const recent = await head('/var/gopher/listing.gophermap', 20)
var lines = await wc('/var/gopher/listing.gophermap')
// printf "<a href=\"%s.html\">%s <span class=\"dim\">&gt;&gt;</span> %s</a>\\n" "$loghtml" "$itemnum" "$title"
var content = ''
for (let i = 0; i < recent.length; ++i) {
const split = recent[i].split('\t')
let link = split[1]
let name = split[0].substr(1)
content += "<a href=\"" + link + "\">" + lines + " <span class=\"dim\">&gt;&gt;</span> " + name + "</a>\n"
lines--
}
res.render('index', { intro: intro, recent: content })
})
app.listen(port, () => console.log(`listening on port ${port}!`))