aboutsummaryrefslogtreecommitdiff
path: root/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'index.js')
-rw-r--r--index.js39
1 files changed, 10 insertions, 29 deletions
diff --git a/index.js b/index.js
index 7f20e1d..81d7364 100644
--- a/index.js
+++ b/index.js
@@ -1,34 +1,15 @@
'use strict'
const fs = require('fs')
+const config = require('config-yml')
const express = require('express')
const compression = require('compression')
-const db = require('./utils/db')
+const db = require('./db')
const themify = require('./utils/themify')
const PLACES = 7
-function getCountImage({ count, theme='konachan', PLACES=PLACES }) {
- // This is not the greatest way for generating an SVG but it'll do for now
- const countArray = count.toString().padStart(PLACES, '0').split('')
-
- const parts = countArray.reduce((acc, next, index) => `
- ${acc}
- <image x="${index * 45}" y="0" width="45px" height="100px" xlink:href="${themify.wrap(next, theme)}" />
-`, '')
-
- return `<?xml version="1.0" encoding="UTF-8"?>
-<svg width="${PLACES * 45}" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
- <title>Kawaii Count</title>
- <g>
- ${parts}
- </g>
-</svg>
-`
-}
-
-
const app = express()
app.use(express.static('assets'))
app.use(compression())
@@ -41,8 +22,8 @@ app.get('/', (req, res) => {
// get the image
app.get('/get/@:name', async (req, res) => {
const name = req.params.name
- const theme = req.query.theme || 'konachan'
- let length = PLACES, num = 0
+ const theme = req.query.theme || 'moebooru'
+ let length = PLACES, count = 0
// This helps with GitHub's image cache
res.set({
@@ -54,19 +35,19 @@ app.get('/get/@:name', async (req, res) => {
res.set({
'cache-control': 'max-age=31536000'
})
- num = '0123456789'
+ count = '0123456789'
length = 10
} else {
- const counter = await db.getNum(name)
- num = counter.num + 1
+ const counter = await db.getNum(name) || { name, num: 0 }
+ count = counter.num + 1
- db.setNum(counter.name, num)
+ db.setNum(counter.name, count)
console.log(counter, `theme: ${theme}`)
}
// Send the generated SVG as the result
- res.send(getCountImage({ count: num, theme, PLACES: length }))
+ res.send(themify.getCountImage({ count, theme, length }))
})
app.get('/heart-beat', (req, res) => {
@@ -78,6 +59,6 @@ app.get('/heart-beat', (req, res) => {
console.log('heart-beat')
});
-const listener = app.listen(process.env.PORT, () => {
+const listener = app.listen(config.app.port, () => {
console.log('Your app is listening on port ' + listener.address().port)
})