-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (73 loc) · 2.35 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
var flatiron = require('flatiron')
, path = require('path')
, fs = require('fs')
, connect = require('connect')
, RedisStore = require('connect-redis')(connect);
// Initialize flatiron application
var app = flatiron.app;
// Setup the different configuration stores that are used by the application
app.config.env();
app.config.file({ file: path.join(__dirname, 'config.json') });
var environment = app.config.get('NODE_ENV') || 'development'
, production = environment === 'production'
, config = app.config.get(environment)
, store = new RedisStore(config.redis)
, appCache = {};
// Set correct configuration
app.config.set('app', config);
// Setup plugins
app.use(flatiron.plugins.log);
app.use(flatiron.plugins.http, {
buffer: false,
route: {
stream: true
},
before: [
require('./middleware'),
connect.bodyParser(),
connect.cookieParser('iluvrandomstringz'),
connect.session({ store: store, secret: 'ireallyluvthem' })
]
});
app.use(flatiron.plugins.static, { dir: path.join(__dirname, 'public') });
// Root route to server index.html
app.router.get('/', function () {
this.res.html(200, fs.readFileSync(path.join(__dirname, 'public', 'index.html'), 'utf8'));
});
// Serve all the other routes
require('./lib/sessions');
require('./lib/filebrowser');
// Serve apps
[
'terminal',
'filebrowser'
].forEach(function (e) {
appCache[e] = fs.readFileSync(path.join(__dirname, 'public', 'app', e, 'index.html'), 'utf8');
app.router.get('/' + e, function () {
if (this.req.session && this.req.session.logged) {
this.res.html(200, appCache[e]);
} else {
this.res.redirect('/');
}
});
});
// The connect-redis module doesn't listen for errors on the redis client so we
// need to handle that our selfs if we don't want our app to crash with silly
// death messages.
store.client.on('error', function redisClient(err) {
if (err.message.indexOf('ECONNREFUSED')) {
return app.log.error([
'Unable to connect to a redis instance'
, 'make sure that you have a redis server running on ' + config.redis.host + ':' + config.redis.port
].join(', '));
}
app.log.error('The redis client recieved an error', {
stack: err.stack
, message: err.message
});
});
app.start(9466, function (err) {
console.log("Server running at http://localhost:9466");
});
// TTY for terminal
require('./lib/tty');