Skip to content

Commit

Permalink
extracting out routes, general clear up, better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bahamas10 committed Oct 28, 2012
1 parent 28327b4 commit de1b52b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
10 changes: 10 additions & 0 deletions router.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,10 @@
var router = new require('routes').Router();

module.exports = router;

var _static = require('./routes/static');
router.addRoute('/', _static);
router.addRoute('/static/*', _static);

router.addRoute('/cache/*', require('./routes/cache'));
router.addRoute('/api/:type?/:filter?/:new?', require('./routes/api'));
65 changes: 35 additions & 30 deletions server.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@ var util = require('util');
var open = require('open'); var open = require('open');
var request = require('request'); var request = require('request');
var async = require('async'); var async = require('async');
var router = new require('routes').Router();
var AmpacheSession = require('ampache'); var AmpacheSession = require('ampache');


var decorate = require('./decorate'); var decorate = require('./decorate');
var router = require('./router');


var cache_ready = false; var cache_ready = false;


// populated when this module is called // populated when this module is called
var conn, conf, o, cache_dir, cache = {}; var conn, conf, o, cache_dir, cache = {};


// make the routes
var _static = require('./routes/static');
router.addRoute('/', _static);
router.addRoute('/static/*', _static);
router.addRoute('/cache/*', require('./routes/cache'));
router.addRoute('/api/:type?/:filter?/:new?', require('./routes/api'));

// Export the function to create the server // Export the function to create the server
module.exports = function(config) { module.exports = function(_conf) {
// set the global variables, these were set from ./bin/webamp.js // set the global variables, these were set from ./bin/webamp.js
conf = config; conf = _conf;
cache_dir = path.join(conf.webamp_dir, 'cache'); cache_dir = path.join(conf.webamp_dir, 'cache');


// prepend timestamps to all logs // prepend timestamps to all logs
Expand Down Expand Up @@ -56,8 +49,14 @@ module.exports = function(config) {
setInterval(function() { setInterval(function() {
console.log('Sending Keep Alive'); console.log('Sending Keep Alive');
conn.ping(function(err, body) { conn.ping(function(err, body) {
if (body && body.session_expire) console.log('Sessions expires: %s', body.session_expire); // ping success
if (err || !body.session_expire) conn.authenticate(function(err, body) { if (body && body.session_expire)
return console.log('Sessions expires: %s',
body.session_expire.toJSON());

// ping failed
if (err || !body.session_expire)
return conn.authenticate(function(err, body) {
if (err) throw err; if (err) throw err;
console.log('Session Expired: Reauthentication successful'); console.log('Session Expired: Reauthentication successful');
}); });
Expand Down Expand Up @@ -110,16 +109,17 @@ function on_request(req, res) {
function populate_cache(body) { function populate_cache(body) {
console.log('Populating cache'); console.log('Populating cache');
var funcs = { var funcs = {
'artists': AmpacheSession.prototype.get_artists, artists: AmpacheSession.prototype.get_artists,
'albums': AmpacheSession.prototype.get_albums, albums: AmpacheSession.prototype.get_albums,
'songs': AmpacheSession.prototype.get_songs songs: AmpacheSession.prototype.get_songs
}, };
to_get = {}, var to_get = {};
albums_by_artist = 0, var albums_by_artist = 0;
songs_by_album = 0; var songs_by_album = 0;


// if the cache is up to date, try to load in the cache
if (cache_up_to_date(body)) { if (cache_up_to_date(body)) {
['artists', 'albums', 'songs'].forEach(function(key) { Object.keys(funcs).forEach(function(key) {
try { try {
cache[key] = require(path.join(cache_dir, key + '.json')); cache[key] = require(path.join(cache_dir, key + '.json'));
console.log('Loaded %s from local cache', key); console.log('Loaded %s from local cache', key);
Expand All @@ -133,13 +133,14 @@ function populate_cache(body) {
to_get = funcs; to_get = funcs;
} }


// Loop the caches to build from remote source // Loop the things that need to be cached
Object.keys(to_get).forEach(function(key) { Object.keys(to_get).forEach(function(key) {
to_get[key].call(conn, function(err, body) { to_get[key].call(conn, function(err, body) {
if (err) throw err; if (err) throw err;
cache[key] = body; cache[key] = body;
// Save the cache // Save the cache, do it async... whatever
fs.writeFile(path.join(cache_dir, key + '.json'), JSON.stringify(body), function(err) { fs.writeFile(path.join(cache_dir, key + '.json'),
JSON.stringify(body), function(err) {
if (err) return console.error(err); if (err) return console.error(err);
}); });
console.log('Loaded %s from remote source', key); console.log('Loaded %s from remote source', key);
Expand All @@ -161,8 +162,8 @@ function populate_cache(body) {


// Grab all of the album art to cache locally // Grab all of the album art to cache locally
function cache_album_art() { function cache_album_art() {
var art_dir = path.join(cache_dir, 'media', 'art'), var art_dir = path.join(cache_dir, 'media', 'art');
queue = async.queue(q, 200); var queue = async.queue(q, 200);


Object.keys(cache.albums).forEach(function(album) { Object.keys(cache.albums).forEach(function(album) {
var filename = path.join(art_dir, album) + '.jpg'; var filename = path.join(art_dir, album) + '.jpg';
Expand All @@ -175,9 +176,10 @@ function cache_album_art() {
}); });


function q(task, cb) { function q(task, cb) {
var r = request(task.url), var r = request(task.url);
s = fs.createWriteStream(task.file); var s = fs.createWriteStream(task.file);
r.pipe(s, {'end': false});
r.pipe(s, {end: false});
r.on('end', function() { r.on('end', function() {
s.end(); s.end();
cb(); cb();
Expand All @@ -204,7 +206,9 @@ function caches_ready(body) {
console.log('All caches ready'); console.log('All caches ready');


// Save the auth data for the update/add/clean times // Save the auth data for the update/add/clean times
fs.writeFile(path.join(cache_dir, 'update.json'), JSON.stringify(body), function(err) { fs.writeFile(path.join(cache_dir, 'update.json'),
JSON.stringify(body), function(err) {

if (err) return console.error(err); if (err) return console.error(err);
}); });


Expand All @@ -223,7 +227,8 @@ function cache_up_to_date(body) {


['add', 'update', 'clean'].forEach(function(key) { ['add', 'update', 'clean'].forEach(function(key) {
if (body[key].toJSON() !== old_body[key]) { if (body[key].toJSON() !== old_body[key]) {
console.log('Cache not up-to-date - pulling from remote source (%s)', key); console.log('Cache not up-to-date - pulling from remote source (%s)',
key);
ok = false; ok = false;
} }
}); });
Expand Down

0 comments on commit de1b52b

Please sign in to comment.