Switch to Standard style

parent 27e58ec4
// ----------------------------------------------------------------------------
"use strict"
'use strict'
// core
const path = require('path')
......@@ -8,7 +8,6 @@ const path = require('path')
// npm
const pg = require('pg')
const pgpatcher = require('pg-patcher')
const pgx = require('pg-x')
const log = require('logfmtr').default()
// local
......@@ -22,15 +21,15 @@ const databasePatchLevel = 3
// create a pool
const pool = new pg.Pool({
connectionString: env.databaseUrl,
idleTimeoutMillis: env.isProd ? 1000 : 60 * 1000,
idleTimeoutMillis: env.isProd ? 1000 : 60 * 1000
})
function patch(callback) {
log.info({ level : databasePatchLevel }, 'patching-database')
function patch (callback) {
log.info({ level: databasePatchLevel }, 'patching-database')
const opts = {
dir: path.join(__dirname, '..', 'schema'),
logger: console.log.bind(console),
logger: console.log.bind(console)
}
pgpatcher(pool, databasePatchLevel, opts, callback)
}
......@@ -41,7 +40,7 @@ module.exports = {
// `pg.Pool`
pool,
// patch
patch,
patch
}
// ----------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// core
const fs = require('fs')
const path = require('path')
// npm
......@@ -12,7 +11,6 @@ const favicon = require('serve-favicon')
const errorHandler = require('errorhandler')
const LogFmtr = require('logfmtr')
const yid = require('yid')
const zid = require('zid')
// local
const pkg = require('../package.json')
......@@ -24,9 +22,11 @@ const stats = require('./stats.js')
const log = LogFmtr.default()
const faviconFilename = path.join(__dirname, '..', 'public', 'favicon.ico')
// create the sitemap
const sitemap = [
`${env.baseUrl}/`,
`${env.baseUrl}/`
]
const sitemapTxt = sitemap.join('\n') + '\n'
......@@ -47,7 +47,7 @@ app.locals.env = env
// middleware
app.use(compression())
app.use(favicon(path.join(__dirname, '..', 'public', 'favicon.ico')))
app.use(favicon(faviconFilename))
app.use(express.static('public'))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
......@@ -82,74 +82,83 @@ app.get(
(req, res) => {
stats.home.inc()
res.render('index', {
menu: 'home',
menu: 'home'
})
}
)
app.post(
'/',
(req, res) => {
stats.shorten.inc()
res.render('index', {
menu: 'home'
})
}
)
app.get(
'/stats',
(req, res, next) => {
let finished = false
let got = 0
let currentStats = {}
// get some bits
stats.pages.forEach((hitName) => {
stats[hitName].values((err, data) => {
if ( finished ) return
if (err) {
finished = true
return next(err)
}
got += 1
// save this hit
data.forEach((hit) => {
currentStats[hit.ts] = currentStats[hit.ts] || {}
currentStats[hit.ts][hitName] = hit.val
})
// if we've got all the results, render the page
if ( got === stats.pages.length ) {
finished = true
res.render('stats', { stats : currentStats, title : 'stats' })
}
})
'/stats',
(req, res, next) => {
let finished = false
let got = 0
let currentStats = {}
// get some bits
stats.pages.forEach((hitName) => {
stats[hitName].values((err, data) => {
if (finished) return
if (err) {
finished = true
return next(err)
}
got += 1
// save this hit
data.forEach((hit) => {
currentStats[hit.ts] = currentStats[hit.ts] || {}
currentStats[hit.ts][hitName] = hit.val
})
}
// if we've got all the results, render the page
if (got === stats.pages.length) {
finished = true
res.render('stats', { stats: currentStats, title: 'stats' })
}
})
})
}
)
app.get(
'/sitemap.txt',
(req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.send(sitemapTxt)
}
'/sitemap.txt',
(req, res) => {
res.setHeader('Content-Type', 'text/plain')
res.send(sitemapTxt)
}
)
app.use((err, req, res, next) => {
// connect.multipart() says the file is too big
if (err.status === 413 ) {
if ( req.xhr ) {
const data = {
ok : false,
msg : 'File is too big, should be less than 5MB.',
}
return res.json(data)
}
else {
res.setHeader('Content-Type', 'text/plain')
return res.status(413).send('File is too big, should be less than 5MB.')
}
// connect.multipart() says the file is too big
if (err.status === 413) {
if (req.xhr) {
const data = {
ok: false,
msg: 'File is too big, should be less than 5MB.'
}
return res.json(data)
} else {
res.setHeader('Content-Type', 'text/plain')
return res.status(413).send('File is too big, should be less than 5MB.')
}
next(err)
}
next(err)
})
// error handlers
if (!env.isProd) {
app.use(errorHandler({ dumpExceptions: true, showStack: true }))
app.use(errorHandler({ dumpExceptions: true, showStack: true }))
}
// --------------------------------------------------------------------------------------------------------------------
......
......@@ -28,7 +28,7 @@ module.exports = {
isProd,
isDev,
databaseUrl,
googleAnalytics,
googleAnalytics
}
// ----------------------------------------------------------------------------
......@@ -14,15 +14,15 @@ const client = redis.createClient()
const stats = {}
const pages = [ 'home', 'shorten', 'expand' ];
const pages = [ 'home', 'shorten', 'expand' ]
pages.forEach((name) => {
stats[name] = rustle({
client : client,
domain : pkg.name, // \
category : 'hits', // >- Keys: "<domain>:<category>:<name>"
name : name, // /
period : 24 * 60 * 60, // one day
aggregation : 'sum',
client: client,
domain: pkg.name, // \
category: 'hits', // >- Keys: "<domain>:<category>:<name>"
name: name, // /
period: 24 * 60 * 60, // one day
aggregation: 'sum'
})
})
......
This diff is collapsed.
......@@ -7,7 +7,7 @@
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js",
"test": "echo 'No tests yet!'"
"test": "standard"
},
"dependencies": {
"body-parser": "^1.18.3",
......@@ -27,7 +27,8 @@
"zid": "^0.3.0"
},
"devDependencies": {
"nodemon": "^1.18.10"
"nodemon": "^1.18.10",
"standard": "^12.0.1"
},
"author": {
"name": "Andrew Chilton",
......
......@@ -2,7 +2,6 @@
// core
const http = require('http')
const path = require('path')
// npm
const log = require('logfmtr').default()
......@@ -41,7 +40,7 @@ if (env.isDev) {
// })
// }
function cleanup(callback) {
function cleanup (callback) {
setTimeout(callback, 100)
}
......@@ -49,6 +48,10 @@ function cleanup(callback) {
process.once('SIGUSR2', () => {
log.info('SIGUSR2')
cleanup((err) => {
console.log('Finished')
if (err) {
console.warn(err)
}
process.kill(process.pid, 'SIGUSR2')
})
})
......@@ -57,6 +60,9 @@ process.on('SIGTERM', () => {
log.info('SIGTERM')
cleanup((err) => {
console.log('Finished')
if (err) {
console.warn(err)
}
process.exit(err ? 2 : 0)
})
})
......@@ -65,6 +71,9 @@ process.on('SIGINT', () => {
log.info('SIGINT')
cleanup((err) => {
console.log('Finished')
if (err) {
console.warn(err)
}
process.exit(err ? 2 : 0)
})
})
......@@ -88,7 +97,6 @@ api.patch((err) => {
server.listen(port, () => {
log.info({ port }, 'listening')
})
})
// ----------------------------------------------------------------------------
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment