This repository was archived by the owner on Oct 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
95 lines (81 loc) · 2.9 KB
/
server.js
File metadata and controls
95 lines (81 loc) · 2.9 KB
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
'use strict';
// Some modules
const express = require('express');
const path = require('path');
const dbInit = require(path.resolve(__dirname, 'db', 'init'));
const morgan = require('morgan'); // Logging middleware
const bodyParser = require('body-parser'); // Parse data
const checkHeadersMid = require(path.resolve(__dirname, 'middlewares', 'checkHeaders')); // Check request headers
const setHeadersMid = require(path.resolve(__dirname, 'middlewares', 'setHeaders')); // Set response headers
const authMid = require(path.resolve(__dirname, 'middlewares', 'auth')); // Authentication middleware
// Load config
const config = require(path.resolve(__dirname, 'config.json'));
// Import routes
const apiRoutes = require(path.resolve(__dirname, 'api', 'routes'));
const mattermostRoutes = require(path.resolve(__dirname, 'mattermost', 'routes'));
const slackRoutes = require(path.resolve(__dirname, 'slack', 'routes'));
// CONSTANTS
const baseApiPath = '/api/v1';
const baseMattermostPath = '/mattermost';
const baseSlackPath = '/slack';
const protectedRoutes = [
baseApiPath
];
/**
* Main function
*/
async function main() {
// Check database state
await dbInit.checkDatabase();
// Create Express.js server
const app = express();
// MIDDLEWARES
// Logging
app.use(morgan('combined'));
// Static files
app.use(express.static('static'));
// Validate Accept Header
app.use(checkHeadersMid.validateAcceptHeader);
// Set response headers
app.use(setHeadersMid.setApiHeaders);
// Trust proxy if needed
if (config.server.behindProxy) {
app.enable('trust proxy');
}
// Parse JSON data
app.use(bodyParser.json());
// Protect admin routes (non-GET requests)
app.use(authMid.checkAuth(protectedRoutes));
// ROUTES
app.get(baseApiPath + '/quote/random', apiRoutes.getRandomQuote);
app.get(baseApiPath + '/quote/:id', apiRoutes.getQuoteById);
app.get(baseApiPath + '/character/:id', apiRoutes.getCharacterById);
app.get(baseApiPath + '/character/:id/random', apiRoutes.getQuoteByAuthor);
// Admin routes
app.post(baseApiPath + '/quote', apiRoutes.createQuote);
app.patch(baseApiPath + '/quote/:id', apiRoutes.modifyQuote);
app.delete(baseApiPath + '/quote/:id', apiRoutes.deleteQuote);
// Mattermost routes
if (config.mattermost.enable) {
// Parse body
app.use(bodyParser.urlencoded({ extended: true }));
// Verify token if necessary
if (config.mattermost.token) {
app.use(authMid.validateMattermost);
}
app.post(baseMattermostPath, mattermostRoutes.getQuote);
}
// Slack routes
if (config.slack.enable) {
// Parse body
app.use(bodyParser.urlencoded({ extended: true }));
app.post(baseSlackPath, slackRoutes.getQuote);
}
// Return 404 if incorrect request route
app.use(function (req, res) {
res.status(404).json({ 'status': 404, 'message': 'Not found' });
});
app.listen(3000);
console.log('Listening on port 3000...');
}
main();