-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.9 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
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import logger from 'morgan';
import expressValidator from 'express-validator';
import swaggerJSDoc from 'swagger-jsdoc';
import routes from './server/routes';
const app = express();
const port = process.env.PORT || 3000
// swagger definition
const swaggerDefinition = {
info: {
title: 'Doksmanager API',
version: '1.0.0',
description: 'Doksmanager API is the API for a document management system, complete with roles and privileges. Each document defines access rights; the document defines which roles can access it. Also, each document specifies the date it was published. Users are categorized by roles. Each user must have a role defined for them.',
contact: {
email: '[email protected]',
},
license: {
name: 'MIT',
url: 'http://www.apache.org/licenses/LICENSE-2.0.html',
}
},
host: 'doksmanager.herokuapp.com',
basePath: '/',
};
// options for the swagger docs
const options = {
// import swaggerDefinitions
swaggerDefinition,
// path to the API docs
apis: [path.join(__dirname, './server/routes/*.js')],
};
const swaggerSpec = swaggerJSDoc(options);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
// initialize swagger-jsdoc
app.use(express.static('public/api-doc'));
// serve swagger
app.get('/swagger.json', (req, res) => {
res.setHeader('Content-Type', 'application/json');
res.send(swaggerSpec);
});
app.use((req, res, next) => {
const send = res.send;
let sent = false;
res.send = (data) => {
if (sent) return;
send.bind(res)(data);
sent = true;
};
next();
});
// mount all routes on /api path
app.use('/api/v1', routes);
const server = app.listen(port, () => {
console.log('API Server started and listening on port 3000');
});
export default server;