From cbbf373bd7b9f624ba00896aac59defeca1f4767 Mon Sep 17 00:00:00 2001 From: Jad Date: Mon, 3 Aug 2020 12:11:58 +0000 Subject: Initial commit --- index.js | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 index.js (limited to 'index.js') diff --git a/index.js b/index.js new file mode 100644 index 0000000..e8f49b0 --- /dev/null +++ b/index.js @@ -0,0 +1,74 @@ +'use strict' + +const fs = require('fs') +const express = require('express') +const compression = require('compression') + +const db = require('./db') + +const numList = require('./num-list') + +const PLACES = 7 + +function getCountImage(count) { + // This is not the greatest way for generating an SVG but it'll do for now + const countArray = count.toString().padStart(PLACES, '0').split('') + + const parts = countArray.reduce((acc, next, index) => ` + ${acc} + +`, '') + + return ` + + Kawaii Count + + ${parts} + + +` +} + + +const app = express() +app.use(compression()) +app.set('view engine', 'pug') + +app.get('/', (req, res) => { + res.render('index') +}); + +// get the image +app.get('/get/@:name', async (req, res) => { + const name = req.params.name + + if (!name) return + + const counter = await db.getNum(name) + const num = counter.num + 1 + + db.setNum(counter.name, num) + + // This helps with GitHub's image cache + res.set({ + 'content-type': 'image/svg+xml', + 'cache-control': 'max-age=0, no-cache, no-store, must-revalidate' + }) + + // Send the generated SVG as the result + res.send(getCountImage(num)) + console.log(counter) +}) + +app.get('/heart-beat', (req, res) => { + res.set({ + 'cache-control': 'max-age=0, no-cache, no-store, must-revalidate' + }) + + res.send('alive') + console.log('heart-beat') +}); + +const listener = app.listen(process.env.PORT, () => { + console.log('Your app is listening on port ' + listener.address().port) +}) -- cgit v1.2.3