From a14c6cd90be4b083d6b13a51692b7f0e79549004 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Sat, 30 Apr 2016 16:52:43 -0700 Subject: [PATCH] Use separate modules, get config from env --- modules/ServerUtils.js | 19 +++++++++++++ modules/StatsUtils.js | 27 ++++++++++++++++++ package.json | 13 ++++----- server.js | 62 ++++++++++-------------------------------- 4 files changed, 67 insertions(+), 54 deletions(-) create mode 100644 modules/ServerUtils.js create mode 100644 modules/StatsUtils.js diff --git a/modules/ServerUtils.js b/modules/ServerUtils.js new file mode 100644 index 0000000..38ff027 --- /dev/null +++ b/modules/ServerUtils.js @@ -0,0 +1,19 @@ +import cors from 'cors' +import express from 'express' +import { createRequestHandler } from 'npm-http-server' +import { logStats } from './StatsUtils' + +export const createServer = (options = {}) => { + const app = express() + + app.disable('x-powered-by') + app.use(cors()) + app.use(express.static('public', { maxAge: 60000 })) + + if (options.redisURL) + app.use(logStats(options.redisURL)) + + app.use(createRequestHandler(options)) + + return app +} diff --git a/modules/StatsUtils.js b/modules/StatsUtils.js new file mode 100644 index 0000000..39b2630 --- /dev/null +++ b/modules/StatsUtils.js @@ -0,0 +1,27 @@ +import redis from 'redis' +import onFinished from 'on-finished' + +const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/ + +export const logStats = (redisURL) => { + const redisClient = redis.createClient(redisURL) + + return (req, res, next) => { + onFinished(res, () => { + const path = req.path + + if (res.statusCode === 200 && path.charAt(path.length - 1) !== '/') { + redisClient.zincrby([ 'request-paths', 1, path ]) + + const match = URLFormat.exec(path) + + if (match) { + const packageName = match[1] + redisClient.zincrby([ 'package-requests', 1, packageName ]) + } + } + }) + + next() + } +} diff --git a/package.json b/package.json index ca63236..98c5a39 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,8 @@ { "description": "The website for npmcdn.com", "author": "Michael Jackson", - "config": { - "registryURL": "https://registry.npmjs.org", - "bowerBundle": "/bower.zip", - "redirectTTL": 500, - "autoIndex": true, - "port": 5000 - }, "dependencies": { + "babel-register": "^6.7.2", "cors": "^2.7.1", "express": "^4.13.4", "npm-http-server": "^2.9.1", @@ -21,5 +15,10 @@ }, "engines": { "node": "5.8.x" + }, + "babel": { + "presets": [ + "es2015" + ] } } diff --git a/server.js b/server.js index fdacb44..7f9eb93 100644 --- a/server.js +++ b/server.js @@ -1,56 +1,24 @@ -const cors = require('cors') -const express = require('express') -const createRequestHandler = require('npm-http-server').createRequestHandler -const onFinished = require('on-finished') -const redis = require('redis') +require('babel-register')({ + only: require('path').resolve(__dirname, './modules') +}) -const registryURL = process.env.npm_package_config_registryURL -const bowerBundle = process.env.npm_package_config_bowerBundle -const redirectTTL = process.env.npm_package_config_redirectTTL -const autoIndex = process.env.npm_package_config_autoIndex -const port = process.env.PORT || process.env.npm_package_config_port +const createServer = require('./modules/ServerUtils').createServer -const URLFormat = /^\/((?:@[^\/@]+\/)?[^\/@]+)(?:@([^\/]+))?(\/.*)?$/ +const port = process.env.PORT || 5000 +const registryURL = process.env.REGISTRY_URL || 'https://registry.npmjs.org' +const bowerBundle = process.env.BOWER_BUNDLE || '/bower.zip' +const redirectTTL = process.env.REDIRECT_TTL || 500 +const autoIndex = !process.env.DISABLE_INDEX +const redisURL = process.env.REDIS_URL -const logStats = (redisURL) => { - const redisClient = redis.createClient(redisURL) - - return (req, res, next) => { - onFinished(res, () => { - const path = req.path - - if (res.statusCode === 200 && path.charAt(path.length - 1) !== '/') { - redisClient.zincrby([ 'request-paths', 1, path ]) - - const match = URLFormat.exec(path) - - if (match) { - const packageName = match[1] - redisClient.zincrby([ 'package-requests', 1, packageName ]) - } - } - }) - - next() - } -} - -const app = express() - -app.disable('x-powered-by') -app.use(cors()) -app.use(express.static('public', { maxAge: 60000 })) - -if (process.env.REDIS_URL) - app.use(logStats(process.env.REDIS_URL)) - -app.use(createRequestHandler({ +const server = createServer({ registryURL: registryURL, bowerBundle: bowerBundle, redirectTTL: redirectTTL, - autoIndex: autoIndex -})) + autoIndex: autoIndex, + redisURL: redisURL +}) -app.listen(port, () => { +server.listen(port, () => { console.log('Server started on port %s, Ctrl+C to quit', port) })