2020-08-03 14:11:58 +02:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
2020-08-12 12:03:35 +02:00
|
|
|
const config = require('config-yml')
|
2020-08-03 14:11:58 +02:00
|
|
|
const express = require('express')
|
|
|
|
const compression = require('compression')
|
|
|
|
|
2020-08-12 12:03:35 +02:00
|
|
|
const db = require('./db')
|
2020-08-05 07:37:12 +02:00
|
|
|
const themify = require('./utils/themify')
|
2020-08-03 14:11:58 +02:00
|
|
|
|
|
|
|
const PLACES = 7
|
|
|
|
|
|
|
|
const app = express()
|
2020-08-21 18:14:01 +02:00
|
|
|
|
2020-08-04 10:36:37 +02:00
|
|
|
app.use(express.static('assets'))
|
2020-08-03 14:11:58 +02: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) => {
|
2020-08-22 15:20:20 +02:00
|
|
|
const { name } = req.params
|
|
|
|
const { theme = 'moebooru' } = req.query
|
|
|
|
let length = PLACES
|
2020-08-03 14:11:58 +02: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-21 18:24:02 +02:00
|
|
|
const data = await getCountByName(name)
|
2020-08-21 18:14:01 +02:00
|
|
|
|
2020-08-05 07:37:12 +02:00
|
|
|
if (name === 'demo') {
|
2020-08-09 18:18:18 +02:00
|
|
|
res.set({
|
|
|
|
'cache-control': 'max-age=31536000'
|
|
|
|
})
|
2020-08-05 07:37:12 +02:00
|
|
|
length = 10
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:11:58 +02:00
|
|
|
// Send the generated SVG as the result
|
2020-08-22 15:20:20 +02:00
|
|
|
const renderSvg = themify.getCountImage({ count: data.num, theme, length })
|
2020-08-21 18:14:01 +02:00
|
|
|
res.send(renderSvg)
|
2020-08-22 15:20:20 +02:00
|
|
|
|
2023-03-06 11:57:41 +01:00
|
|
|
console.log(data, `theme: ${theme}`, `ref: ${req.get('Referrer') || null}`, `ua: ${req.get('User-Agent') || null}`)
|
2020-08-22 15:20:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// JSON record
|
|
|
|
app.get('/record/@:name', async (req, res) => {
|
|
|
|
const { name } = req.params
|
|
|
|
|
|
|
|
const data = await getCountByName(name)
|
|
|
|
|
|
|
|
res.json(data)
|
2020-08-03 14:11:58 +02: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')
|
|
|
|
});
|
|
|
|
|
2020-08-22 15:20:20 +02:00
|
|
|
const listener = app.listen(config.app.port || 3000, () => {
|
|
|
|
console.log('Your app is listening on port ' + listener.address().port)
|
|
|
|
})
|
|
|
|
|
2023-03-06 11:57:41 +01:00
|
|
|
let __cache_counter = {}, shouldPush = false
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
shouldPush = true
|
|
|
|
}, 1000 * 60);
|
|
|
|
|
|
|
|
async function pushDB() {
|
|
|
|
if (!shouldPush) return
|
|
|
|
|
|
|
|
try {
|
|
|
|
shouldPush = false
|
|
|
|
if (Object.keys(__cache_counter).length === 0) return
|
|
|
|
|
|
|
|
console.log("pushDB", __cache_counter)
|
|
|
|
|
|
|
|
const counters = Object.keys(__cache_counter).map(key => {
|
|
|
|
return {
|
|
|
|
name: key,
|
|
|
|
num: __cache_counter[key]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
await db.setNumMulti(counters)
|
|
|
|
__cache_counter = {}
|
|
|
|
} catch (error) {
|
|
|
|
console.log("pushDB is error: ", error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 15:20:20 +02:00
|
|
|
async function getCountByName(name) {
|
|
|
|
const defaultCount = { name, num: 0 }
|
|
|
|
|
|
|
|
if (name === 'demo') return { name, num: '0123456789' }
|
|
|
|
|
|
|
|
try {
|
2023-03-06 11:57:41 +01:00
|
|
|
if (!(name in __cache_counter)) {
|
|
|
|
const counter = await db.getNum(name) || defaultCount
|
|
|
|
__cache_counter[name] = counter.num + 1
|
|
|
|
} else {
|
|
|
|
__cache_counter[name]++
|
|
|
|
}
|
|
|
|
|
|
|
|
pushDB()
|
|
|
|
|
|
|
|
return { name, num: __cache_counter[name] }
|
2020-08-22 15:20:20 +02:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
console.log("get count by name is error: ", error)
|
|
|
|
return defaultCount
|
2020-08-21 18:14:01 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|