-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
87 lines (74 loc) · 2.43 KB
/
index.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
'use strict';
const path = require('path');
const http = require('http');
const cors = require('cors');
const dotenv = require('dotenv');
const express = require("express");
const oas3Tools = require('@haimkastner/oas3-tools');
const dataStore = require('./data/dataStore');
dotenv.config();
const serverPort = +process.env.PORT || 8080;
const localDevMode = !!process.env.DEV_MODE;
console.info(
`
_______ _______ _______ _______ ______ _______ _______
| _ | _ | _ | _ | _ \\| _ | |
|. 1___|. 1 | 1___|. 1 |. | |. 1___|.| | |
|. |___|. _ |____ |. _ |. | |. __)_'-|. |-'
|: 1 |: | |: 1 |: | |: | |: 1 | |: |
|::.. . |::.|:. |::.. . |::.|:. |::.| |::.. . | |::.|
'-------'--- ---'-------'--- ---'--- ---'-------' '---'
__
.--------.-----.----| |--______.-----.-----.----.--.--.-----.----.
| | _ | __| |______|__ --| -__| _| | | -__| _|
|__|__|__|_____|____|__|__| |_____|_____|__| \___/|_____|__|
`
);
const app = express();
/** Verify user session */
function validate(request, scopes, schema) {
// Get the session from the header
const session = request.headers.authentication;
// Get session data
const sessionData = dataStore.getSessionData(session);
// Keep session directly on the req object
request.session = session;
// On DEV mode generate new session on demand
if (localDevMode && !sessionData) {
dataStore.addSessionData(session, 'dev@dev');
return true;
}
// Verify there is data for this session, else, mean mock user not connected
return !!sessionData;
}
var options = {
app,
routing: {
controllers: path.join(__dirname, './controllers'),
useStubs: false
},
openApiValidator: {
validateSecurity: {
handlers: {
userAuth: validate,
adminAuth: validate,
}
}
}
};
// Open cors for all, including credentials
app.use(
cors({
credentials: true,
origin: (origin, callback) => {
callback(null, true);
},
}),
);
// Init SWAGGER route
oas3Tools.expressAppConfig(path.join(__dirname, 'api/openapi.yaml'), options);
// Initialize the Swagger middleware
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});