Skip to content

Commit

Permalink
restructure app.js and add basic http-auth functionality
Browse files Browse the repository at this point in the history
This functionality isn't included in the electron version
(http auth doesn't make sense in the use case for electron).
It is mainly provided for use when hosting stretto so you can
lock down your server with basic authentication.
  • Loading branch information
benkaiser committed Jul 27, 2016
1 parent d81ca7e commit 12bcfb9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
91 changes: 56 additions & 35 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Module dependencies.
*/

var async = require('async');
var express = require('express.oi');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
Expand All @@ -12,6 +13,7 @@ var path = require('path');
var util = require(__dirname + '/util.js');
var mkdirp = require('mkdirp');
var proxy = require('express-http-proxy');
var basicAuth = require('basic-auth-connect');

var app = express().http().io();

Expand All @@ -22,16 +24,26 @@ var sessionOpts = {
};
app.io.session(sessionOpts);

app.io.set('authorization', function(handshakeData, accept) {
app.io.set('authorization', function handleAuth(handshakeData, accept) {
// accept all requests
accept(null, true);
});

// fetch the config directory
app.set('configDir', process.env.configDir || __dirname);

// make sure the dbs directory is present
mkdirp(app.get('configDir') + '/dbs/covers', function() {
// all variables to be shared throughout the app
app.set('port', process.env.PORT || 2000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.set('root', __dirname);
app.set('started', Date.now());
app.engine('html', require('swig').renderFile);

async.series([function createDatabaseDirectory(next) {
// make sure the dbs directory is present
mkdirp(app.get('configDir') + '/dbs/covers', next);
}, function databaseDirectoryCreated(next) {
// attach the db to the app
require(__dirname + '/db.js')(app);

Expand All @@ -40,38 +52,47 @@ mkdirp(app.get('configDir') + '/dbs/covers', function() {

// attach the config
app.set('config', require(__dirname + '/config')(app));
});

// all environments
app.set('port', process.env.PORT || 2000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
app.set('root', __dirname);
app.set('started', Date.now());
app.engine('html', require('swig').renderFile);
app.use(favicon(__dirname + '/static/images/favicon.ico'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.session(sessionOpts));
app.use('/static', express.static(__dirname + '/static'));

// proxy for itunes requests
app.use('/proxy', proxy('https://itunes.apple.com', {
forwardPath: function(req, res) {
return require('url').parse(req.url).path;
},
}));

// development only
if (app.get('env') == 'development') {
app.use(errorhandler());
}

require(__dirname + '/routes').createRoutes(app);

app.listen(app.get('port'), function() {
console.log('Express server listening on port ' + app.get('port'));
});
next();
}, function setupAuth(next) {
var config = app.get('config');

// auth is only intended for use outside of electron
if (config.auth &&
config.auth.username !== undefined &&
config.auth.password !== undefined &&
!process.env.ELECTRON_ENABLED) {
app.use(basicAuth(config.auth.username, config.auth.password));
}
next();
}, function setupEverythinElse(next) {
// middleware to use in the app
app.use(favicon(__dirname + '/static/images/favicon.ico'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.session(sessionOpts));
app.use('/static', express.static(__dirname + '/static'));

// proxy for itunes requests
app.use('/proxy', proxy('https://itunes.apple.com', {
forwardPath: function (req, res) {
return require('url').parse(req.url).path;
},
}));

// development only
if (app.get('env') == 'development') {
app.use(errorhandler());
}

require(__dirname + '/routes').createRoutes(app);

app.listen(app.get('port'), function () {
console.log('Express server listening on port ' + app.get('port'));
});

next();
}]);

module.exports = app;
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ function Config(app) {
parallel_download: 5,
};

// // Uncomment this block to set your own basic http authentication
// this.auth = {
// username: 'username',
// password: 'password',
// };

// used for itunes metadata fetching (to select the store to search)
this.country_code = 'us';

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"archiver": "^1.0.0",
"async": "*",
"basic-auth-connect": "^1.0.0",
"body-parser": "^1.14.1",
"cookie-parser": "^1.4.0",
"errorhandler": "^1.4.2",
Expand Down

0 comments on commit 12bcfb9

Please sign in to comment.