app.js 4.78 KB
Newer Older
mntmn's avatar
mntmn committed
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
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";

require('./models/schema');
require("log-timestamp");

const config = require('config');
const redis = require('./helpers/redis');
const websockets = require('./helpers/websockets');

const http = require('http');
const path = require('path');

const _ = require('underscore');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const swig = require('swig');
const i18n = require('i18n-2');
const helmet = require('helmet');

const express = require('express');
const app = express();

const isProduction = app.get('env') === 'production';

console.log("Booting Spacedeck Open… (environment: " + app.get('env') + ")");

app.use(logger(isProduction ? 'combined' : 'dev'));

i18n.expressBind(app, {
  locales: ["en", "de", "fr"],
  defaultLocale: "en",
  cookieName: "spacedeck_locale",
  devMode: (app.get('env') == 'development')
});

swig.setDefaults({
  varControls: ["[[", "]]"] // otherwise it's not compatible with vue.js
});

swig.setFilter('cdn', function(input, idx) {
  return input;
});

app.engine('html', swig.renderFile);
app.set('view engine', 'html');

if (app.get('env') != 'development') {
  app.set('views', path.join(__dirname, 'build', 'views'));
  app.use(favicon(path.join(__dirname, 'build', 'assets', 'images', 'favicon.png')));
  app.use(express.static(path.join(__dirname, 'build', 'assets')));
} else {
  app.set('views', path.join(__dirname, 'views'));
  app.use(favicon(path.join(__dirname, 'public', 'images', 'favicon.png')));
  app.use(express.static(path.join(__dirname, 'public')));
}

app.use(bodyParser.json({
  limit: '50mb'
}));

app.use(bodyParser.urlencoded({
  extended: false,
  limit: '50mb'
}));

app.use(cookieParser());
app.use(helmet.noCache())
app.use(helmet.frameguard())
app.use(helmet.xssFilter())
app.use(helmet.hsts({
  maxAge: 7776000000,
  includeSubdomains: true
}))
app.disable('x-powered-by');
app.use(helmet.noSniff())

// CUSTOM MIDDLEWARES

app.use(require("./middlewares/templates"));
app.use(require("./middlewares/error_helpers"));
app.use(require("./middlewares/setuser"));
app.use(require("./middlewares/subdomain"));
app.use(require("./middlewares/cors"));
app.use(require("./middlewares/i18n"));
app.use("/api", require("./middlewares/api_helpers"));
app.use('/api/spaces/:id', require("./middlewares/space_helpers"));
app.use('/api/spaces/:id/artifacts/:artifact_id', require("./middlewares/artifact_helpers"));
app.use('/api/teams', require("./middlewares/team_helpers"));

// REAL ROUTES

app.use('/api/users', require('./routes/api/users'));
app.use('/api/memberships', require('./routes/api/memberships'));

const spaceRouter = require('./routes/api/spaces');
app.use('/api/spaces', spaceRouter);

spaceRouter.use('/:id/artifacts', require('./routes/api/space_artifacts'));
spaceRouter.use('/:id/memberships', require('./routes/api/space_memberships'));
spaceRouter.use('/:id/messages', require('./routes/api/space_messages'));
spaceRouter.use('/:id/digest', require('./routes/api/space_digest'));
spaceRouter.use('/:id', require('./routes/api/space_exports'));

app.use('/api/sessions', require('./routes/api/sessions'));
app.use('/api/teams', require('./routes/api/teams'));
app.use('/api/webgrabber', require('./routes/api/webgrabber'));
app.use('/', require('./routes/root'));

// catch 404 and forward to error handler
app.use(require('./middlewares/404'));
if (app.get('env') == 'development') {
  app.set('view cache', false);
  swig.setDefaults({cache: false});
} else {
  app.use(require('./middlewares/500'));
}

module.exports = app;

// CONNECT TO DATABASE
const mongoHost = process.env.MONGO_PORT_27017_TCP_ADDR || 'localhost';
mongoose.connect('mongodb://' + mongoHost + '/spacedeck');

// START WEBSERVER
const port = 9000;

const server = http.Server(app).listen(port, () => {

  if ("send" in process) {
    process.send('online');
  }

}).on('listening', () => {
  
  const host = server.address().address;
  const port = server.address().port;
  console.log('Spacedeck Open listening at http://%s:%s', host, port);

}).on('error', (error) => {

  if (error.syscall !== 'listen') {
    throw error;
  }

  const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
});

//WEBSOCKETS & WORKER
websockets.startWebsockets(server);
redis.connectRedis();

process.on('message', (message) => {
  console.log("Process message:", message);
  if (message === 'shutdown') {
    console.log("Exiting spacedeck.");
    process.exit(0);
  }
});