Created
January 7, 2019 11:22
-
-
Save jakub-roman/5e199a75ba3801afccaedbe81270ed5a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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