-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
185 lines (160 loc) · 6.27 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const mailgunContainer = require('mailgun-js');
const restful = require('restful-node');
const app = express();
app.disable('x-powered-by');
app.set('trust-proxy', 'loopback'); // Trust the proxy with localhost IPs
app.use(app.get('env') === 'production' ?
morgan('combined', {
skip: function (req, res) {
return res.statusCode < 400 // Only log requests of type error
}
}) :
morgan('dev')
);
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: false }));
app.use(cookieParser());
////////////////////////////////////////
// LOAD CONFIG //
////////////////////////////////////////
const config = require('./bin/config/_config.json');
// Crash server if config is missing
if (!config) {
throw new Error('Missing config file "./bin/config/_config.json"');
}
app.use(restful.setup.settings(app, config)); // attach config to `req.settings`, and `app.restful.settings`
////////////////////////////////////////
// STATIC PATHS //
// In production will Nginx be //
// responsible to serve this content //
////////////////////////////////////////
if (app.get('env') === 'development') {
app.use(express.static(path.join(__dirname, 'client', 'dist'))); // Angular
app.use('/resources', express.static(path.join(__dirname, 'resources')));
}
////////////////////////////////////////
// EMAIL SETUP //
// Use Mailgun as email provider //
// for our project, atleast under //
// development. //
////////////////////////////////////////
let mailConfig, Mailgun;
try {
mailConfig = require('./bin/config/_email.json');
} catch (e) {
console.warn('[WARN] Missing _email.json in config. You will not receive email warnings')
}
if (mailConfig) {
Mailgun = mailgunContainer({
apiKey: mailConfig['api-key'],
domain: mailConfig.domain
});
}
// Store mailgun in request
app.use((req, res, next) => {
req.email = {};
req.email.Mailgun = Mailgun;
req.email.config = {
from: mailConfig.user,
to: mailConfig.distribution,
domain: mailConfig.domain
};
next();
});
////////////////////////////////////////
// MONGOOSE SETUP //
////////////////////////////////////////
mongoose.Promise = global.Promise;
const { database } = config;
if (!database) {
throw new Error('Database Config Error: Cannot resolve property "database" in _config.json');
}
// restful-node expects `config` to have a 'database' field,
// most of our dev code has used 'db'
if (!database.database && database.db) {
database.database = database.db;
}
restful.database.setupMongoose(mongoose, database)
.then((db) => console.log(`Database Connected (${new Date()})`))
.catch((err) => {
console.error('Database Connection Error\n');
console.error(err);
// We will only try to notify through email,
// if env is in production (prevents spamming)
if (Mailgun && app.get('env') === 'production') {
Mailgun.messages().send({
from: `Server Chriba <${mailConfig.user}>`,
to: mailConfig.distribution.join(','),
subject: '[Chriba] Database Connection Error',
text: `The server could not connect to a database.\n
Timestamp: ${new Date()}\n
Following error occured:\n\t${err.message}`,
html: ` <h1>Database connection error</h1>
<p>The server could not connect to the database.</p>
<pre>Timestamp: ${new Date()}</pre>
<p>Following error occured</p>
<p></p>
<pre>${err.message}</pre>`
}, (err) => {
if (err) {
console.error('\nEmail Distribution Error\n');
console.error(err);
}
});
}
});
////////////////////////////////////////
// REST-CONTROLLERS //
// Loads all the restful-node //
// controllers, and connects them //
// to routes //
////////////////////////////////////////
const AdminController = require('./controllers/admin.controller');
const TokenController = require('./controllers/token.controller');
const EstateController = require('./controllers/estate.controller');
const ProjectController = require('./controllers/project.controller');
const GeneralController = require('./controllers/general.controller');
const EstateThumbController = require('./controllers/estate-thumb.controller');
const ProjectThumbController = require('./controllers/project-thumb.controller');
const tokenConfig = {
secret: config['token-secret'],
debug: app.get('env') !== 'production',
ttl: 43200, // 12 hours
root: config.media.url
};
// Register routes
restful.routes.urls(app, '/api', [
{ url: '/admin/token', controller: new TokenController(tokenConfig) },
{ url: '/admin', controller: new AdminController(tokenConfig) },
{ url: '/estates/thumb', controller: new EstateThumbController(tokenConfig)},
{ url: '/projects/thumb', controller: new ProjectThumbController(tokenConfig)},
{ url: '/estates', controller: new EstateController(tokenConfig)},
{ url: '/projects', controller: new ProjectController(tokenConfig)},
{ url: '/general', controller: new GeneralController(tokenConfig)}
]);
////////////////////////////////////////
// DEV ROUTES //
// //
// Catches 404 errors on resources/ //
// and serves the client-code, //
// in development mode //
////////////////////////////////////////
if (app.get('env') === 'development') {
app.all('/resources/*', (req, res) => {
res.sendStatus(404);
});
// Serves the client
// MUST be last (it catches everything)
app.all('*', (req, res) => {
res.sendFile(
path.join('client', 'dist', 'index.html'),
{ root: __dirname });
});
}
module.exports = app;