mirror of
https://github.com/seigler/advent-of-code-2020
synced 2025-07-27 00:06:09 +00:00
Initial commit
This commit is contained in:
commit
f3d555f01c
12 changed files with 370 additions and 0 deletions
49
solutions/viewer-server.js
Normal file
49
solutions/viewer-server.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
const path = require('path')
|
||||
const express = require('express')
|
||||
const { position, find, write } = require('promise-path')
|
||||
const fromHere = position(__dirname)
|
||||
const report = (...messages) => console.log(`[${require(fromHere('../package.json')).logName} / ${__filename.split(path.sep).pop().split('.js').shift()}]`, ...messages)
|
||||
|
||||
const app = express()
|
||||
const packageData = require('../package.json')
|
||||
|
||||
async function generateIndexHTML () {
|
||||
const title = packageData.logName
|
||||
const solutions = await find(fromHere('/*'))
|
||||
const links = solutions
|
||||
.filter(n => n.indexOf('.js') === -1 && n.indexOf('.html') === -1)
|
||||
.map(solution => {
|
||||
const folder = solution.substr(fromHere('../').length)
|
||||
return ` <li><a href="/${folder}/viewer.html">${folder}</a></li>`
|
||||
})
|
||||
|
||||
const html = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>${title}</title>
|
||||
<style> html, body { font-family: sans-serif; }</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>${title}</h1>
|
||||
<ul>
|
||||
${links.join('\n')}
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
||||
`
|
||||
|
||||
report('Updated hard coded index:', fromHere('index.html'))
|
||||
await write(fromHere('index.html'), html, 'utf8')
|
||||
|
||||
return html
|
||||
}
|
||||
|
||||
app.use('/solutions', express.static(fromHere('')))
|
||||
|
||||
app.get('/', async (req, res) => {
|
||||
const html = await generateIndexHTML()
|
||||
res.send(html)
|
||||
})
|
||||
|
||||
const port = 8080
|
||||
app.listen(port, () => report(`Listening on http://localhost:${port}/`))
|
Loading…
Add table
Add a link
Reference in a new issue