forked from inaes-tic/mbc-caspa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (124 loc) · 4.57 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var express = require('express'),
path = require('path'),
exec = require('child_process').exec,
i18n = require('i18n-abide'),
_ = require('underscore'),
backboneio = require('backbone.io');
var dirs = {
pub : path.join(__dirname, 'public'),
views : path.join(__dirname, 'views') ,
styles : path.join(__dirname, 'styles'),
models : path.join(__dirname, 'models'),
vendor : path.join(__dirname, 'vendor'),
uploads: path.join(__dirname, 'public/uploads/incoming')
};
/* make sure at runtime that we atempt to get the dirs we need */
for (d in dirs) {
/* HACK: but I'm not going to waist time writing mkdir -p */
exec ('mkdir -p ' + dirs[d], function (error, stdout, stderr) {
if (error !== null) {
console.log('exec error: ' + error);
}
});
}
var app = express();
app.configure(function () {
app.use(i18n.abide({
supported_languages: ['en-US', 'es', 'db-LB', 'it-CH'],
default_lang: 'es',
debug_lang: 'it-CH',
locale_directory: 'locale'
}));
app.set('port', process.env.PORT || 3000);
app.set('views', dirs.views);
app.set('view engine', 'jade');
app.use(express.logger('dev'));
/* app.use('/uploads', upload({
tmpDir: dirs.uploads + '/incoming',
uploadDir: dirs.upolads,
uploadUrl: '/uploads/',
safeFileTypes: /\.(webm|mkv|mov|mp4|avi|ogg)$/i,
}));*/
app.use(express.bodyParser({
uploadDir: dirs.uploads,
maxFieldsSize: 10 * 1024 * 1024
})); /* */
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({
src: dirs.styles,
dest: dirs.pub,
compress: true}
));
app.use(express.static(dirs.pub));
app.use('/models', express.static(dirs.models));
app.use('/lib', express.static(dirs.vendor));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('io.loglevel', 100);
app.set('minify', false);
});
app.configure('production', function(){
app.use(express.errorHandler());
app.set('io.loglevel', 1);
app.set('minify', true);
});
var appModel = require('./routes')(app);
var media = require('./routes/media')(app);
function debug_backend (backend) {
console.log ('Debugging backend: ', backend);
backend.use(function(req, res, next) {
console.log(req.backend);
console.log(req.method);
console.log(JSON.stringify(req.model));
next();
});
}
var db = require('mongoskin').db('localhost:27017/mediadb?auto_reconnect', {safe:true});
var mubsub = require ('mubsub');
var client = mubsub (db);
var channel = client.channel('messages', { size: 10000000, max: 50000 });
var mediabackend = backboneio.createBackend();
mediabackend.use(backboneio.middleware.mongoStore(db, 'medias'));
var blockbackend = backboneio.createBackend();
blockbackend.use(backboneio.middleware.memoryStore(db, 'blocks'));
var listbackend = backboneio.createBackend();
listbackend.use(backboneio.middleware.mongoStore (db, 'lists'));
var schedbackend = backboneio.createBackend();
schedbackend.use(function (req, res, next) {
console.log ("schedbackend handler", req);
channel.publish ({method: req.method, backend: req.backend, model: req.model});
next();
});
schedbackend.use(backboneio.middleware.mongoStore(db, 'scheds'));
channel.subscribe ({channel: 'schedbackend'}, function (sched) {
schedbackend.emit('updated', sched.model);
});
_([mediabackend, listbackend]).each (debug_backend);
backboneio.listen(app.listen(app.get('port'), function(){
console.log("Express server listening on port %d in %s mode", app.get('port'), app.settings.env);
}), { mediabackend: mediabackend,
blockbackend: blockbackend,
listbackend: listbackend,
schedbackend: schedbackend,
});
var utils = require('./utils');
//var mlt = new melted({reconnect: true});
if (process.env.MBC_SCRAPE) {
setTimeout(function () {
utils.scrape_files (process.env.HOME + "/Downloads/Gardel", function (model) {
db.collection('medias').insert(model, {safe:true}, function(err, result) {
if (err) {
console.error ('error','An error has occurred' + err);
} else {
mediabackend.emit('created', model);
}
});
});
}, 300);
} else {
console.log ("not scrapping");
}