123 lines
3.1 KiB
TypeScript
123 lines
3.1 KiB
TypeScript
import * as fs from 'fs'
|
|
|
|
import * as config from './config'
|
|
import * as logger from './log'
|
|
|
|
import * as express from 'express'
|
|
|
|
|
|
function getPageOptions(contentStr: string) : any {
|
|
let pageOptions : any = {}
|
|
// logger.info(`contentStr: ${contentStr}`)
|
|
try {
|
|
let lines = contentStr.split("\n")
|
|
let firstLine = lines[0]
|
|
// logger.info(`firstLine: ${firstLine}`)
|
|
let pageOptionsStr = firstLine.replace(/^\s*<!-- (\{.+\}) -->[\s\S]*$/, "\$1")
|
|
// logger.info(`pageOptionsStr: ${pageOptionsStr}`)
|
|
pageOptions = JSON.parse(pageOptionsStr)
|
|
// logger.info(JSON.stringify(pageOptions))
|
|
} catch {
|
|
// logger.info("No pageOptions found")
|
|
}
|
|
return pageOptions
|
|
}
|
|
|
|
function getToc() : string {
|
|
logger.info("building toc")
|
|
let toc : string = ""
|
|
let posts = fs.readdirSync('./docroot/posts')
|
|
posts.forEach((v) => {
|
|
let content = fs.readFileSync(`./docroot/posts/${v}/article.pag`)
|
|
let contentStr = content.toString()
|
|
let pageOptions = getPageOptions(contentStr)
|
|
toc += `<li><a href="posts/${v}">${v} - ${pageOptions.title}</a></li>`
|
|
})
|
|
return toc
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("Homepage starting")
|
|
|
|
let app = express()
|
|
|
|
let masterTmpl: string
|
|
let toc: string
|
|
let cache: any = {}
|
|
|
|
|
|
|
|
app.engine('pag', (filePath: string, options: any, callback: any) => {
|
|
if (! (filePath in cache)) {
|
|
logger.info(`${filePath} not yet in cache`)
|
|
|
|
fs.readFile(filePath, (err, content) => {
|
|
if (err) {
|
|
return callback(new Error(err.message))
|
|
}
|
|
|
|
let contentStr = content.toString()
|
|
let pageOptions = getPageOptions(contentStr)
|
|
let renderedPhase1 = contentStr
|
|
.replace('#bla#', 'blu')
|
|
|
|
let renderedPhase2 = masterTmpl
|
|
.replace('#maincontent#', renderedPhase1)
|
|
|
|
for (let key in pageOptions) {
|
|
let value = pageOptions[key]
|
|
if (key == 'toc' && value == 'compute') {
|
|
value = toc
|
|
}
|
|
renderedPhase2 = renderedPhase2.replace(`#${key}#`, value)
|
|
}
|
|
|
|
cache[filePath] = renderedPhase2
|
|
|
|
return callback(null, renderedPhase2)
|
|
})
|
|
} else {
|
|
logger.info(`${filePath} served from cache`)
|
|
return callback(null, cache[filePath])
|
|
}
|
|
})
|
|
|
|
app.set('views', './docroot')
|
|
app.set('view engine', 'pag')
|
|
|
|
app.get('/index', (req, res) => {
|
|
logger.info(req.url)
|
|
logger.info(req.baseUrl)
|
|
logger.info(req.originalUrl)
|
|
res.render('index', {})
|
|
})
|
|
|
|
app.get('/posts/:date', (req, res) => {
|
|
res.render(`posts/${req.params.date}/article`, {})
|
|
})
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send('Hello world!')
|
|
})
|
|
|
|
app.get('/reload', (req, res) => {
|
|
logger.info('truncating cache')
|
|
cache = {}
|
|
toc = getToc()
|
|
res.send('reload triggered')
|
|
})
|
|
|
|
app.use('/files', express.static('files'))
|
|
|
|
|
|
masterTmpl = fs.readFileSync('./docroot/master.tmpl').toString()
|
|
toc = getToc()
|
|
|
|
app.listen(config.dict.httpPort, () => {
|
|
logger.info("Homepage is listening")
|
|
})
|
|
logger.info("Homepage running")
|
|
|