Skip to content

Instantly share code, notes, and snippets.

@jakub-roman
Created January 7, 2019 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakub-roman/5e199a75ba3801afccaedbe81270ed5a to your computer and use it in GitHub Desktop.
Save jakub-roman/5e199a75ba3801afccaedbe81270ed5a to your computer and use it in GitHub Desktop.
var express = require('express')
var app = express()
var pug = require('pug')
var fs = require('fs')
var bp = require('body-parser')
const config = './config.json'
const timeout = 300 // 5mins
app.use(bp.urlencoded({ extended: false }))
app.use(bp.json())
app.get('/', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let d = Math.floor(Date.now() / 1000) - timeout
for( var index in c.members ){
if( c.pwd[index] && c.pwd[index].ts >= d ){
c.members[index].color = "green"
}else{
c.members[index].color = "red"
}
}
res.send(pug.renderFile('templates/main.pug', {
members: c.members
}))
})
app.get('/submit', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let d = Math.floor(Date.now() / 1000) - timeout
for( var index in c.members ){
if( c.pwd[index] && c.pwd[index].ts >= d ){
continue
}
res.send(pug.renderFile('templates/verify-fail.pug'))
return
}
fs.readFile(c.voucher.path, function (err,data){
res.contentType("application/pdf")
res.send(data)
})
})
app.get('/:name', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let m = req.params.name
res.send(pug.renderFile('templates/member.pug', {
member: c.members[m],
id: m
}))
})
app.post('/:name/submit', function (req, res) {
let raw = fs.readFileSync(config, 'utf8')
let c = JSON.parse(raw)
let user = req.params.name
let pass = req.body.password
if ( pass === c.pwd[user].pwd ) {
c.pwd[user].ts = Math.floor(Date.now() / 1000)
fs.writeFileSync(config, JSON.stringify(c), 'utf8')
res.send(pug.renderFile('templates/submit-ok.pug'))
}else{
res.send(pug.renderFile('templates/submit-fail.pug'))
}
})
app.listen(8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment