Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
journey-ad committed Aug 3, 2020
0 parents commit cbbf373
Show file tree
Hide file tree
Showing 7 changed files with 1,762 additions and 0 deletions.
Binary file added count.db
Binary file not shown.
57 changes: 57 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const sqlite3 = require('sqlite3')

const db = new sqlite3.Database('count.db')

db.run(`CREATE TABLE IF NOT EXISTS tb_count (
id INTEGER PRIMARY KEY AUTOINCREMENT
NOT NULL
UNIQUE,
name VARCHAR (32) NOT NULL
UNIQUE,
num BIGINT NOT NULL
DEFAULT (0)
);`)

function getNum(name) {
return new Promise((resolve, reject) => {
db.get('SELECT `name`, `num` from tb_count WHERE `name` = ?', name, (err, row) => {
if (err) reject(err)

resolve(row || { name, num: 0 })
})
})
}

function getAll(name) {
return new Promise((resolve, reject) => {
db.get('SELECT * from tb_count', (err, row) => {
if (err) reject(err)

resolve(row)
})
})
}

function setNum(name, num) {
return new Promise((resolve, reject) => {
db.run(`INSERT INTO tb_count(\`name\`, \`num\`)
VALUES($name, $num)
ON CONFLICT(name) DO
UPDATE SET \`num\` = $num;`
, {
$name: name,
$num: num
}
, (err, row) => {
if (err) reject(err)

resolve(row)
})
})
}

module.exports = {
getNum,
getAll,
setNum
}
74 changes: 74 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict'

const fs = require('fs')
const express = require('express')
const compression = require('compression')

const db = require('./db')

const numList = require('./num-list')

const PLACES = 7

function getCountImage(count) {
// 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="data:image/gif;base64,${numList[next]}" />
`, '')

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(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.name

if (!name) return

const counter = await db.getNum(name)
const num = counter.num + 1

db.setNum(counter.name, num)

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

// Send the generated SVG as the result
res.send(getCountImage(num))
console.log(counter)
})

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(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port)
})
12 changes: 12 additions & 0 deletions num-list.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cbbf373

Please sign in to comment.