-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (81 loc) · 3.34 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
const express = require('express');
const path = require('path');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
// miscellaneous passport things
const passport = require('passport');
// own middlewares
const error_prettier = require("./middlewares/errors-beautifier");
const default_error_handler = require("./middlewares/default_error_handler");
const not_found_handler = require("./middlewares/not_found");
const securityCheck = require("./middlewares/security");
const removeTempFiles = require("./middlewares/remove_temp_files");
// location of stored files to serve as static
const {FILES_FOLDER} = require("./config/storage_paths");
// helmet for classic security measures
const helmet = require('helmet');
// OpenAPI V3 validation middleware
const Enforcer = require("openapi-enforcer-middleware");
const enforcerMulter = require('openapi-enforcer-multer');
const spec = path.join(__dirname, 'api.yml');
const controllerDirectory = path.resolve(__dirname, 'controllers');
// Initialize passport ( Passport is a singleton )
require('./config/passport');
module.exports = new Promise((resolve, reject) => {
// storage for multer
const storage = require("./config/storage")();
let app = express();
// common middleware
app.use(helmet());
app.use(logger('dev'));
app.use(bodyParser.json({limit: '10mb'}));
app.use(bodyParser.urlencoded({extended: true}));
app.use(cookieParser());
// Gives the Swagger UI Viewer
/* istanbul ignore next */
app.use('/api-docs', function (_, res) {
res.redirect("http://petstore.swagger.io/?url=https://raw.githubusercontent.com/SourceCodeOER/sourcecode_api/master/api.yml");
});
// Serves stored files with this endpoint
/* istanbul ignore next */
app.use("/files", express.static(FILES_FOLDER));
// main API setup
try {
// initialize the enforcer
const enforcer = Enforcer(spec, {
componentOptions: {
requestBodyAllowedMethods: {"delete": true},
exceptionSkipCodes: ["WSCH001", "WMED001"]
},
resValidate: false
});
// check if the client is allowed on this endpoint
enforcer.use(securityCheck());
enforcer
.controllers(controllerDirectory)
.then(() => {
// Passport Js must have that
app.use(passport.initialize());
// add enforcer multer middleware
app.use(enforcerMulter(enforcer, storage));
// add the enforcer middleware runner to the express app
app.use(enforcer.middleware());
// remove temp files if an error occurs (if provided)
app.use(removeTempFiles());
// catch 404 and forward to error handler
app.use(not_found_handler());
// for production, hides Sequelize messages under "general" message
app.use(error_prettier());
// error handler
app.use(default_error_handler(app.get('env') === 'development'));
resolve(app);
})
.catch(/* istanbul ignore next */err => {
throw err;
});
} catch (err) {
/* istanbul ignore next */
reject(err);
}
});