-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
29 lines (26 loc) · 861 Bytes
/
index.js
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
26
27
28
29
const http = require('http')
const commits = require('./cache.json')
const getCommitHTML = require('./get-commit-html')
if (require.main === module) {
createServer()
.then(server => {
const port = +process.env.PORT || 3000
server.listen(port)
console.log(`listening on https://localhost:${port}`)
})
} else {
module.exports = createServer
}
async function createServer () {
console.log('# commits', commits.length)
const server = http.createServer(async (req, res) => {
console.log(req.method, req.url)
const userCommit = req.url.match(/\w+/)
const hash = Array.isArray(userCommit) ? userCommit[0] : commits[0].hash
console.log('hash', hash)
const commit = commits.find(c => c.hash === hash)
if (!commit) return res.end('404')
res.end(await getCommitHTML(commit, commits))
})
return server
}