forked from journey-ad/Moe-Counter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
89 lines (66 loc) · 1.9 KB
/
index.js
File metadata and controls
89 lines (66 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
'use strict'
const fs = require('fs')
const config = require('config-yml')
const express = require('express')
const compression = require('compression')
const db = require('./db')
const themify = require('./utils/themify')
const PLACES = 7
const app = express()
app.use(express.static('assets'))
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
const { theme = 'moebooru' } = req.query
let length = PLACES
// 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'
})
const data = await getCountByName(name)
if (name === 'demo') {
res.set({
'cache-control': 'max-age=31536000'
})
length = 10
}
// Send the generated SVG as the result
const renderSvg = themify.getCountImage({ count: data.num, theme, length })
res.send(renderSvg)
console.log(data, `theme: ${theme}`)
})
// JSON record
app.get('/record/@:name', async (req, res) => {
const { name } = req.params
const data = await getCountByName(name)
res.json(data)
})
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 || 3000, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
async function getCountByName(name) {
const defaultCount = { name, num: 0 }
if (name === 'demo') return { name, num: '0123456789' }
try {
const counter = await db.getNum(name) || defaultCount
const num = counter.num + 1
db.setNum(counter.name, num)
return counter
} catch (error) {
console.log("get count by name is error: ", error)
return defaultCount
}
}