2020-08-03 12:11:58 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
2020-08-12 10:03:35 +00:00
|
|
|
const config = require('config-yml')
|
2020-08-03 12:11:58 +00:00
|
|
|
const express = require('express')
|
|
|
|
const compression = require('compression')
|
|
|
|
|
2020-08-12 10:03:35 +00:00
|
|
|
const db = require('./db')
|
2020-08-05 05:37:12 +00:00
|
|
|
const themify = require('./utils/themify')
|
2020-08-03 12:11:58 +00:00
|
|
|
|
|
|
|
const PLACES = 7
|
|
|
|
|
|
|
|
const app = express()
|
2020-08-21 16:14:01 +00:00
|
|
|
|
2020-08-04 08:36:37 +00:00
|
|
|
app.use(express.static('assets'))
|
2020-08-03 12:11:58 +00:00
|
|
|
app.use(compression())
|
|
|
|
app.set('view engine', 'pug')
|
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
|
|
|
res.render('index')
|
|
|
|
});
|
|
|
|
|
2020-08-21 16:14:01 +00:00
|
|
|
const getCountByName = async name=> {
|
|
|
|
if (name === 'demo') return '0123456789'
|
|
|
|
// console.log(name)
|
|
|
|
try {
|
|
|
|
const counter = await db.getNum(name) || { name, num: 0 }
|
|
|
|
const r = counter.num + 1
|
|
|
|
db.setNum(counter.name, r)
|
|
|
|
return r
|
|
|
|
} catch (error) {
|
|
|
|
console.log("get count by name is error: ", error)
|
|
|
|
const errorDefaultCount = 0
|
|
|
|
return errorDefaultCount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:11:58 +00:00
|
|
|
// get the image
|
|
|
|
app.get('/get/@:name', async (req, res) => {
|
|
|
|
const name = req.params.name
|
2020-08-12 10:03:35 +00:00
|
|
|
const theme = req.query.theme || 'moebooru'
|
|
|
|
let length = PLACES, count = 0
|
2020-08-03 12:11:58 +00: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 16:14:01 +00:00
|
|
|
count = await getCountByName(name)
|
|
|
|
|
2020-08-05 05:37:12 +00:00
|
|
|
if (name === 'demo') {
|
2020-08-09 16:18:18 +00:00
|
|
|
res.set({
|
|
|
|
'cache-control': 'max-age=31536000'
|
|
|
|
})
|
2020-08-05 05:37:12 +00:00
|
|
|
length = 10
|
|
|
|
}
|
|
|
|
|
2020-08-03 12:11:58 +00:00
|
|
|
// Send the generated SVG as the result
|
2020-08-21 16:14:01 +00:00
|
|
|
const renderSvg = themify.getCountImage({ count, theme, length })
|
|
|
|
res.send(renderSvg)
|
2020-08-03 12:11:58 +00: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-21 16:14:01 +00:00
|
|
|
let port = 3000
|
|
|
|
|
|
|
|
try {
|
|
|
|
let configPort = config.app.port
|
|
|
|
if (configPort) {
|
|
|
|
port = configPort
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
throw new Error(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
const listener = app.listen(port, () => {
|
2020-08-03 12:11:58 +00:00
|
|
|
console.log('Your app is listening on port ' + listener.address().port)
|
|
|
|
})
|