2015-02-24 14:28:16 -05:00
|
|
|
var express = require('express')
|
|
|
|
var Error = require('http-errors')
|
|
|
|
var compression = require('compression')
|
|
|
|
var Auth = require('./auth')
|
|
|
|
var Logger = require('./logger')
|
|
|
|
var Config = require('./config')
|
|
|
|
var Middleware = require('./middleware')
|
|
|
|
var Cats = require('./status-cats')
|
|
|
|
var Storage = require('./storage')
|
|
|
|
var PackageProvider = require('./packages')
|
2014-02-01 03:08:48 -05:00
|
|
|
|
2013-06-07 20:16:28 -05:00
|
|
|
module.exports = function(config_hash) {
|
2014-12-22 12:58:25 -05:00
|
|
|
Logger.setup(config_hash.logs)
|
|
|
|
|
2015-02-24 14:28:16 -05:00
|
|
|
var config = Config(config_hash)
|
|
|
|
var storage = Storage(config)
|
|
|
|
var auth = Auth(config)
|
|
|
|
var packages = PackageProvider(config)
|
|
|
|
var app = express()
|
2014-11-12 06:14:37 -05:00
|
|
|
|
|
|
|
// run in production mode by default, just in case
|
|
|
|
// it shouldn't make any difference anyway
|
|
|
|
app.set('env', process.env.NODE_ENV || 'production')
|
|
|
|
|
|
|
|
function error_reporting_middleware(req, res, next) {
|
|
|
|
res.report_error = res.report_error || function(err) {
|
|
|
|
if (err.status && err.status >= 400 && err.status < 600) {
|
|
|
|
if (!res.headersSent) {
|
|
|
|
res.status(err.status)
|
2014-11-13 13:32:31 -05:00
|
|
|
next({ error: err.message || 'unknown error' })
|
2014-11-12 06:14:37 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Logger.logger.error( { err: err }
|
|
|
|
, 'unexpected error: @{!err.message}\n@{err.stack}')
|
|
|
|
if (!res.status || !res.send) {
|
|
|
|
Logger.logger.error('this is an error in express.js, please report this')
|
|
|
|
res.destroy()
|
|
|
|
} else if (!res.headersSent) {
|
|
|
|
res.status(500)
|
2014-11-13 13:32:31 -05:00
|
|
|
next({ error: 'internal server error' })
|
2014-11-12 06:14:37 -05:00
|
|
|
} else {
|
|
|
|
// socket should be already closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
|
2014-11-13 13:32:31 -05:00
|
|
|
app.use(Middleware.log)
|
2014-11-12 06:14:37 -05:00
|
|
|
app.use(error_reporting_middleware)
|
|
|
|
app.use(function(req, res, next) {
|
|
|
|
res.setHeader('X-Powered-By', config.user_agent)
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
app.use(Cats.middleware)
|
2014-11-13 11:15:50 -05:00
|
|
|
app.use(compression())
|
2013-05-31 01:26:11 -05:00
|
|
|
|
2014-11-12 11:45:40 -05:00
|
|
|
app.get('/favicon.ico', function(req, res, next) {
|
|
|
|
req.url = '/-/static/favicon.png'
|
|
|
|
next()
|
|
|
|
})
|
2014-11-12 11:18:30 -05:00
|
|
|
|
2014-05-12 10:43:18 -05:00
|
|
|
/* app.get('/-/all', function(req, res) {
|
2014-11-12 06:14:37 -05:00
|
|
|
var https = require('https')
|
|
|
|
var JSONStream = require('JSONStream')
|
|
|
|
var request = require('request')({
|
|
|
|
url: 'https://registry.npmjs.org/-/all',
|
|
|
|
})
|
|
|
|
.pipe(JSONStream.parse('*'))
|
|
|
|
.on('data', function(d) {
|
|
|
|
console.log(d)
|
|
|
|
})
|
|
|
|
})*/
|
|
|
|
|
|
|
|
// hook for tests only
|
|
|
|
if (config._debug) {
|
2014-11-24 19:22:05 -05:00
|
|
|
app.get('/-/_debug', function(req, res, next) {
|
2014-11-12 06:14:37 -05:00
|
|
|
var do_gc = typeof(global.gc) !== 'undefined'
|
|
|
|
if (do_gc) global.gc()
|
2014-11-13 13:32:31 -05:00
|
|
|
next({
|
2014-11-12 06:14:37 -05:00
|
|
|
pid : process.pid,
|
|
|
|
main : process.mainModule.filename,
|
|
|
|
conf : config.self_path,
|
|
|
|
mem : process.memoryUsage(),
|
|
|
|
gc : do_gc,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-24 14:28:16 -05:00
|
|
|
app.use(require('./index-api')(config, auth, storage, packages))
|
2014-11-13 12:13:37 -05:00
|
|
|
|
2014-11-16 08:32:46 -05:00
|
|
|
if (config.web && config.web.enable === false) {
|
2015-01-26 04:27:41 -05:00
|
|
|
app.get('/', function(req, res, next) {
|
|
|
|
next( Error[404]('web interface is disabled in the config file') )
|
|
|
|
})
|
2014-11-12 11:45:40 -05:00
|
|
|
} else {
|
2015-02-24 14:28:16 -05:00
|
|
|
app.use(require('./index-web')(config, auth, storage, packages))
|
2014-11-12 11:45:40 -05:00
|
|
|
}
|
|
|
|
|
2014-11-13 11:15:50 -05:00
|
|
|
app.get('/*', function(req, res, next) {
|
2014-11-12 11:25:33 -05:00
|
|
|
next( Error[404]('file not found') )
|
|
|
|
})
|
2014-11-12 06:14:37 -05:00
|
|
|
|
2014-11-13 11:15:50 -05:00
|
|
|
app.use(function(err, req, res, next) {
|
2014-11-13 13:32:31 -05:00
|
|
|
if (Object.prototype.toString.call(err) !== '[object Error]') return next(err)
|
2014-11-13 12:13:37 -05:00
|
|
|
if (err.code === 'ECONNABORT' && res.statusCode === 304) return next()
|
2014-11-13 11:15:50 -05:00
|
|
|
if (typeof(res.report_error) !== 'function') {
|
|
|
|
// in case of very early error this middleware may not be loaded before error is generated
|
|
|
|
// fixing that
|
|
|
|
error_reporting_middleware(req, res, function(){})
|
|
|
|
}
|
|
|
|
res.report_error(err)
|
|
|
|
})
|
|
|
|
|
2014-11-13 13:32:31 -05:00
|
|
|
app.use(Middleware.final)
|
|
|
|
|
2014-11-12 06:14:37 -05:00
|
|
|
return app
|
2013-10-26 07:18:36 -05:00
|
|
|
}
|
2013-05-31 01:26:11 -05:00
|
|
|
|