const express = require('express') const path = require('path') const fs = require('fs') const fsp = fs.promises const router = express.Router() const cfgLoader = require(path.join('..', '..', 'config-loader'))(path.join(__dirname, 'config.json'), { root: path.join(process.cwd(), '..', 'fiche', 'code'), style: 'tomorrow-night' }) async function init () { let config = await cfgLoader let root = path.resolve(config.root) await fsp.access(root, fs.constants.F_OK) router.get('/:name', async (req, res, next) => { let name = req.params.name if (name.length != 4) { try { let text = await fsp.readFile(path.join(root, 'index.html')) res.send(text) } catch (e) { res.status(403) } return res.end() } let fichePath = path.join(root, name, 'index.txt') let reqRaw = req.query.raw != null let text try { text = await fsp.readFile(fichePath, {encoding: 'utf8'}) } catch (e) { return res.status(404).end() } if (reqRaw) { return res.set('Content-Type', 'text/plain').send(text) } let etext = text.replace(/&/g,'&').replace(//g,'>') let payload = '' + '' + '' + '' + name + '' + '' + '' + '' + '' + '' + '
' + etext + '
' + '' + '' + '' res.set('Content-Type', 'application/xhtml+xml').send(payload) }) return router } module.exports = init