forked from PTFS-Europe/ems-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
114 lines (101 loc) · 3.59 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
const fs = require('fs');
const path = require('path');
const express = require('express');
const cors = require('cors');
const { json, urlencoded } = require('body-parser');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const multer = require('multer');
const helmet = require('helmet');
const logger = require('morgan');
const OpenApiValidator = require('express-openapi-validator');
const apiRouter = require('./api');
const { errorFallback } = require('./middleware/error-handler');
const { initialiseAuthentication } = require('./auth');
process.env.UPLOADS_DIR = `uploads/${process.env.SCHEMA}`;
module.exports = {
init: async () => {
const app = express();
// Set up logging
const accessLogStream = fs.createWriteStream(
path.join(process.env.LOG_FILE),
{
flags: 'a'
}
);
// File downloads
// Ensure our upload directory exists
if (!fs.existsSync(process.env.UPLOADS_DIR)) {
fs.mkdirSync(process.env.UPLOADS_DIR);
}
// Handle download requests
app.get('/download/*', (req, res) => {
const file = `${process.env.UPLOADS_DIR}/${req.params[0]}`;
res.download(file);
});
// Middleware
//
// CORS - here we are allowing the client to access the Authorization
// header. We should only need CORS when in development, we want to
// not allow CORS when in production as a partial mitigation against
// CSRF
if (process.env.NODE_ENV === 'development') {
app.use(
cors((req, cb) =>
cb(null, {
exposedHeaders: 'Authorization',
credentials: true,
origin: `${process.env.CLIENT_HOST}:${process.env.CLIENT_PORT}`
})
)
);
}
app.use(helmet());
app.use(cookieParser());
app.use(urlencoded({ extended: true }));
app.use(json());
app.use(logger('combined', { stream: accessLogStream }));
app.use(passport.initialize());
// Configure how we are storing our file uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, process.env.UPLOADS_DIR);
},
filename: (req, file, cb) => {
const ext = path.extname(file.originalname);
const filename = path.basename(file.originalname, ext);
const suffix =
Date.now() + '-' + Math.round(Math.random() * 1e9);
cb(null, filename + '-' + suffix + ext);
}
});
// Install the OpenAPI validator
app.use(
OpenApiValidator.middleware({
apiSpec: './api_spec/ems.yaml',
validateRequests: true,
validateResponses: true,
fileUploader: {
storage
}
})
);
/*
await new OpenApiValidator({
apiSpec: './api_spec/ems.yaml',
validateRequests: true,
validateResponses: true,
fileUploader: {
storage
}
}).install(app);
*/
// API route
app.use('/api', apiRouter);
// Ensure we have the authentication methods we need
initialiseAuthentication(app);
// If we reach this middleware, we need to log an error
app.use(errorFallback);
return app;
}
};