aboutsummaryrefslogtreecommitdiff
path: root/index.js
blob: 641348b5846c320d8d4cd262972b32f3defb73d7 (plain) (blame)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
'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}`, `ref: ${req.get('Referrer') || null}`, `ua: ${req.get('User-Agent') || null}`)
})

// 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)
})

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)
  }
}

async function getCountByName(name) {
  const defaultCount = { name, num: 0 }

  if (name === 'demo') return { name, num: '0123456789' }

  try {
    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] }

  } catch (error) {
    console.log("get count by name is error: ", error)
    return defaultCount

  }
}