-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (132 loc) · 4.06 KB
/
app.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var http = require('http'),
nowjs = require('now'),
express = require('express'),
moment = require('moment'),
kue = require('kue'),
amanda = require('amanda'),
lingua = require('lingua'),
passport = require('passport'),
BasicStrategy = require('passport-http').BasicStrategy,
piler = require('piler');
var redirect = require('node-force-domain').redirect('silkveiljs.no.de');
var mappings = require('./mappings.js');
var constraints = require('./constraints.js');
var actions = require('./actions.js');
var schemas = require('./schemas.js');
var app = express();
var jobs = kue.createQueue();
var validator = amanda('json');
var clientjs = piler.createJSManager();
var clientcss = piler.createCSSManager();
app.configure(function () {
clientjs.bind(app);
clientcss.bind(app);
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
app.use(lingua(app, {
defaultLocale: 'de-DE',
path: __dirname + '/i18n'
}));
app.use(passport.initialize());
app.use(redirect);
app.use(express.static(__dirname + '/public'));
app.use(deliverDefaultImage());
});
clientjs.addUrl('/nowjs/now.js');
clientjs.addFile(__dirname + '/public/scripts/jquery-1.7.2.min.js');
clientjs.addFile(__dirname + '/public/scripts/jade.min.js');
clientcss.addFile(__dirname + '/public/styles/reset.css');
clientcss.addFile(__dirname + '/public/styles/text.css');
clientcss.addFile(__dirname + '/public/styles/960.css');
clientcss.addFile(__dirname + '/public/styles/core.styl');
var users = {
golo: 'secret'
};
passport.use(new BasicStrategy(function (userid, password, done) {
if(!users[userid]) {
return done(null, false);
}
if(users[userid] !== password) {
return done(null, false);
}
done(null, { username: userid });
}));
function deliverDefaultImage() {
return function (req, res, next) {
if(req.url.indexOf('/snapshots/') !== 0) {
return next();
}
res.sendfile('empty.png');
};
}
app.get('/', passport.authenticate('basic', { session: false }), function (req, res) {
res.render('index', {
js: clientjs.renderTags(),
css: clientcss.renderTags()
});
});
app.get('/:alias', function (req, res) {
mappings.findOne(req.params.alias, function (err, result) {
var mapping = result || {
action: 'error',
statusCode: 404,
data: 'File not found'
};
mapping = constraints.verify(mapping) || {
action: 'error',
statusCode: 409,
data: 'Conflict'
};
actions[mapping.action](res, mapping);
});
});
var server = http.createServer(app).listen(process.env.PORT || 3000);
var everyone = nowjs.initialize(server);
jobs.process('snapshotCreated', function (job, done) {
everyone.now.snapshotCreated(job.data.fileName);
done();
});
nowjs.on('connect', function () {
var that = this;
mappings.find(function (err, result) {
that.now.initialize(result);
});
});
everyone.now.createMapping = function(mapping) {
var that = this;
validator.validate(mapping, schemas[mapping.action], { singleError: false }, function (err) {
if(err) {
var fields = {};
for(var i = 0; i < err.length; i++) {
var field = err[i].property.substring(Math.max(0, err[i].property.indexOf('.') + 1));
fields[field] = field;
}
that.now.showErrors(fields);
return;
}
that.now.clearInputForm();
if(mapping.constraints) {
mapping.constraints.validFrom && (mapping.constraints.validFrom = [
moment.utc(mapping.constraints.validFrom).toDate()
]);
mapping.constraints.validBefore && (mapping.constraints.validBefore = [
moment.utc(mapping.constraints.validBefore).toDate()
]);
}
mappings.create(mapping);
if(mapping.action === 'redirect') {
jobs.create('createSnapshot', {
title: mapping.url,
url: mapping.url,
width: 1366,
height: 768,
fileName: mapping.alias
}).save();
}
everyone.now.mappingCreated(mapping);
});
};
everyone.now.deleteMapping = function (alias) {
mappings.delete(alias);
everyone.now.mappingDeleted(alias);
};