Moe-counter/index.js

84 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-08-03 08:11:58 -04:00
'use strict'
const fs = require('fs')
const express = require('express')
const compression = require('compression')
2020-08-05 01:37:12 -04:00
const db = require('./utils/db')
const themify = require('./utils/themify')
2020-08-03 08:11:58 -04:00
const PLACES = 7
2020-08-05 01:37:12 -04:00
function getCountImage({ count, theme='konachan', PLACES=PLACES }) {
2020-08-03 08:11:58 -04:00
// 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}
2020-08-05 01:37:12 -04:00
<image x="${index * 45}" y="0" width="45px" height="100px" xlink:href="${themify.wrap(next, theme)}" />
2020-08-03 08:11:58 -04:00
`, '')
return `<?xml version="1.0" encoding="UTF-8"?>
<svg width="${PLACES * 45}" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Kawaii Count</title>
<g>
${parts}
</g>
</svg>
`
}
const app = express()
2020-08-04 04:36:37 -04:00
app.use(express.static('assets'))
2020-08-03 08:11:58 -04:00
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
2020-08-05 01:37:12 -04:00
const theme = req.query.theme || 'konachan'
let length = PLACES, num = 0
2020-08-03 08:11:58 -04:00
// 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'
})
2020-08-05 01:37:12 -04:00
if (name === 'demo') {
2020-08-09 12:18:18 -04:00
res.set({
'cache-control': 'max-age=31536000'
})
2020-08-05 01:37:12 -04:00
num = '0123456789'
length = 10
} else {
const counter = await db.getNum(name)
num = counter.num + 1
db.setNum(counter.name, num)
console.log(counter, `theme: ${theme}`)
}
2020-08-03 08:11:58 -04:00
// Send the generated SVG as the result
2020-08-05 01:37:12 -04:00
res.send(getCountImage({ count: num, theme, PLACES: length }))
2020-08-03 08:11:58 -04:00
})
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)
})