Moe-counter/index.js

65 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-08-03 08:11:58 -04:00
'use strict'
const fs = require('fs')
const config = require('config-yml')
2020-08-03 08:11:58 -04:00
const express = require('express')
const compression = require('compression')
const db = require('./db')
2020-08-05 01:37:12 -04:00
const themify = require('./utils/themify')
2020-08-03 08:11:58 -04:00
const PLACES = 7
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
const theme = req.query.theme || 'moebooru'
let length = PLACES, count = 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'
})
count = '0123456789'
2020-08-05 01:37:12 -04:00
length = 10
} else {
const counter = await db.getNum(name) || { name, num: 0 }
count = counter.num + 1
2020-08-05 01:37:12 -04:00
db.setNum(counter.name, count)
2020-08-05 01:37:12 -04:00
console.log(counter, `theme: ${theme}`)
}
2020-08-03 08:11:58 -04:00
// Send the generated SVG as the result
res.send(themify.getCountImage({ count, theme, 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(config.app.port, () => {
2020-08-03 08:11:58 -04:00
console.log('Your app is listening on port ' + listener.address().port)
})