-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (81 loc) · 3.51 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
const dotenv = require('dotenv');
if (!process.env.HEROKU_ENV){
dotenv.config();
}
// in locale uso un file .env, recupero eventuali variabili d'ambiente in quel file
//verifica environment
if (!process.env.PORT) {
console.error("\n\nERROR: No port specified! Verify that environment variable PORT exists!\n\n");
process.exit(1);
}
if (!process.env.DATABASE_URL) {
console.error("\n\nERROR: Can't find DATABASE_URL environment variable!\n\n");
process.exit(1);
}
const fs = require('fs'),
path = require('path'),
http = require('http'),
utils = require('./utils/writer.js');
const express = require('express'),
app = express(),
swaggerTools = require('swagger-tools');
const jsyaml = require('js-yaml'),
serverPort = process.env.PORT;
// swaggerRouter configuration
var options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
var optionsUi = {
swaggerUi: "/backend/swaggerui"
};
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);
//Salvo il file zip della repo e lo rendo disponibile staticamente
app.use('/backend/app.zip', express.static('app.zip'));
swaggerTools.initializeMiddleware(swaggerDoc, function(middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger II
app.use(middleware.swaggerUi(optionsUi));
//Serve the static web pages
app.use(express.static(path.join(__dirname, "./public")));
//Serve statically the api specs and the documentation
app.use('/backend/', express.static(path.join(__dirname, "./other/backend/")));
//==============================================
//Handle 404 errors. Leave this as last app.use()
app.use(function(req, res, next) {
console.log('404 error on ' + req.url);
res.status(404);
if (req.url.startsWith('/api')) {
utils.writeJson(res, {
message: "API endpoint not found!"
}, 404);
} else if (req.accepts('text/html')) {
req.url = '/pages/404.html';
app.handle(req, res);
} else {
res.send('404 Not Found');
}
});
// Start the server
http.createServer(app).listen(serverPort, function() {
console.log('\n');
console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
console.log('\n');
console.log(' # Your server is listening on port %d (%s:%d)\n', serverPort, process.env.BASE_URL, serverPort);
console.log(' # Swagger-ui is available at %s:%d/docs\n', process.env.BASE_URL, serverPort);
console.log(' # Homepage is available at %s:%d/\n', process.env.BASE_URL, serverPort);
console.log(' # Api specifications in YAML format are available at %s:%d/backend/spec.yaml', process.env.BASE_URL, serverPort);
console.log('\n');
console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%');
console.log('\n');
});
})