-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (52 loc) · 1.9 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
const express = require('express');
const cors = require('cors');
const path = require('path');
const dotenv = require('dotenv');
dotenv.config();
global.__basedir = __dirname;
const postmanToOpenApi = require('postman-to-openapi');
const YAML = require('yamljs');
const swaggerUi = require('swagger-ui-express');
const listEndpoints = require('express-list-endpoints');
let logger = require('morgan');
const passport = require('passport');
const { adminPassportStrategy } = require('./middleware');
const { devicePassportStrategy } = require('./middleware');
const { clientPassportStrategy } = require('./middleware');
const app = express();
const corsOptions = { origin: process.env.ALLOW_ORIGIN, };
app.use(cors(corsOptions));
//template engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
//all routes
const routes = require('./routes');
adminPassportStrategy(passport);
devicePassportStrategy(passport);
clientPassportStrategy(passport);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use(routes);
//swagger Documentation
postmanToOpenApi('postman/postman-collection.json', path.join('postman/swagger.yml'), { defaultTag: 'General' }).then(data => {
let result = YAML.load('postman/swagger.yml');
result.servers[0].url = '/';
app.use('/swagger', swaggerUi.serve, swaggerUi.setup(result));
}).catch(e=>{
console.log('Swagger Generation stopped due to some error');
});
app.get('/', (req, res) => {
res.render('index');
});
if (process.env.NODE_ENV !== 'test' ) {
const seeder = require('./seeders');
const allRegisterRoutes = listEndpoints(app);
seeder(allRegisterRoutes).then(()=>{console.log('Seeding done.');});
app.listen(process.env.PORT,()=>{
console.log(`your application is running on ${process.env.PORT}`);
});
} else {
module.exports = app;
}