diff --git a/modules/api/api.js b/modules/api/api.js index fc1be82..c4b22bf 100644 --- a/modules/api/api.js +++ b/modules/api/api.js @@ -39,6 +39,8 @@ const passwordFile = 'data/dataEditorPasswords.json' const dataEditsLog = 'stats/dataEdits' const dailyDataCountFile = 'stats/dailyDataCount' const usersDbBackupPath = 'data/dbs/backup' +const quickVoteResultsDir = 'stats/qvote' +const quickVotes = 'stats/qvote/votes.json' // other constants const maxVeteranPwGetCount = 10 @@ -129,6 +131,73 @@ function GetApp () { // ------------------------------------------------------------- + app.get('/quickvote', (req, res) => { + const key = req.query.key + const val = req.query.val + + if (!key || !val) { + res.render('votethank', { + results: 'error', + msg: 'no key or val query param!' + }) + return + } + + let votes = {} + if (utils.FileExists(quickVotes)) { + votes = utils.ReadJSON(quickVotes) + } else { + res.render('votethank', { + result: 'no such pool' + }) + return + } + + if (!votes.voteNames.includes(key)) { + res.render('votethank', { + result: 'no such pool' + }) + return + } + + const voteFile = quickVoteResultsDir + '/' + key + '.json' + const user = req.session.user + + let voteData = { + votes: {}, + users: [] + } + + if (utils.FileExists(voteFile)) { + voteData = utils.ReadJSON(voteFile) + } else { + utils.CreatePath(quickVoteResultsDir) + } + + if (!voteData.users.includes(user.id)) { + if (voteData[val]) { + voteData.votes[val]++ + } else { + voteData.votes[val] = 1 + } + voteData.users.push(user.id) + + logger.Log(`Vote from #${user.id}: ${key}: ${val}`, logger.GetColor('blue')) + res.render('votethank', { + result: 'success', + msg: 'vote added' + }) + } else { + logger.Log(`#${user.id} already voted for: ${key}: ${val}`, logger.GetColor('blue')) + res.render('votethank', { + result: 'already voted', + msg: 'already voted' + }) + } + + utils.WriteFile(JSON.stringify(voteData), voteFile) + }) + app.get('/avaiblePWS', (req, res) => { logger.LogReq(req) diff --git a/modules/api/views/votethank.ejs b/modules/api/views/votethank.ejs new file mode 100755 index 0000000..bf1d266 --- /dev/null +++ b/modules/api/views/votethank.ejs @@ -0,0 +1,31 @@ + + + + Shit uploader + + + +
+ <% + if (result == 'success') { + %> ty a szavazásért c: <% + } else if (result == 'no such pool') { + %> Ilyen nevű szavazás nincs :c <% + } else if (result == 'already voted') { + %> Már szavaztál, de azért ty c: <% + } else if (result == 'error') { + %> Helytelen url paraméterek :c <% + } else { + %> bit of a fuckup here <% + } + %> + ty c: +
+ + diff --git a/server.js b/server.js index 26b7633..81462a1 100755 --- a/server.js +++ b/server.js @@ -89,18 +89,20 @@ function exit (reason) { const app = express() -app.use(function (req, res, next) { - if (req.secure) { - next() - } else { - logger.DebugLog(`HTTPS ${req.method} redirect to: ${'https://' + req.headers.host + req.url}`, 'https', 1) - if (req.method === 'POST') { - res.redirect(307, 'https://' + req.headers.host + req.url) +if (!process.env.NS_DEVEL) { + app.use(function (req, res, next) { + if (req.secure) { + next() } else { - res.redirect('https://' + req.headers.host + req.url) + logger.DebugLog(`HTTPS ${req.method} redirect to: ${'https://' + req.headers.host + req.url}`, 'https', 1) + if (req.method === 'POST') { + res.redirect(307, 'https://' + req.headers.host + req.url) + } else { + res.redirect('https://' + req.headers.host + req.url) + } } - } -}) + }) +} // https://github.com/expressjs/cors#configuration-options app.use(cors({ credentials: true, diff --git a/sharedViews/login.ejs b/sharedViews/login.ejs index a697f55..296cd87 100644 --- a/sharedViews/login.ejs +++ b/sharedViews/login.ejs @@ -101,7 +101,7 @@
- +