-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathindex.js
114 lines (91 loc) · 3.81 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
109
110
111
112
113
114
'use strict';
// Require the restify npm module
const restify = require('restify'),
// Require the cors middleware for restify
corsMiddleware = require('restify-cors-middleware'),
// Require swagger to build an API documentation
swagger = require('swagger-restify'),
// Require node.js' path module
path = require('path');
// Require the services
const services = require('../service'),
// Require configuration
config = require('../config'),
// Require the controllers
controllers = require('../controllers'),
// Require the reference token validation service
referenceTokenValidation = require('./referenceTokenValidation'),
// Require the database to configure it
// use /database/{databaseType}, where databaseType [postgres, mongo]
database = require(`../database/${config.db.type}`);
/**
* Restify server exposing some APIs to manipulate customer data
*
* @public
* @constructor
*/
function Server() {
/**
* Starts the server on the given port
* @param {number} port - The port where the server should listen on
*/
this.start = port => {
// Create a new restify server
const server = restify.createServer();
// Enable cors for restify
const cors = corsMiddleware({
allowHeaders: ['Authorization']
});
initializeSwagger(server, port);
// server.pre runs before other server.use middlewares. It will run before all http requests, so we can handle CORS preflights
server.pre(cors.preflight);
// Validate all request for an valid token
server.pre(referenceTokenValidation.validate());
// Allow cors on all routes
server.use(cors.actual);
// Include a query parser middleware which will expose all parsed query parameters on a special "req.params" object
server.use(restify.queryParser());
// Include a body parser middleware which will parse the body to json objects (in case of application/json)
// Will parse other content types like application/form-data or application/x-www-form-urlencoded
server.use(restify.bodyParser());
// Initialize all controllers
controllers.initialize(server);
// Configure the database to use PostgreSQL or Mongodb
database.configure(config.db.connectionString);
// Configure services to use database as backend storage
services.configure(false);
// Start the server on the given port and output a console message, if it started successfully
server.listen(port, () => {
console.log(`Server is up and running on port ${port}`);
});
};
function initializeSwagger(server, port) {
swagger.init(server, {
swagger: '2.0',
baseUrl: '/',
info: {
version: 'v1',
title: 'Sample Node.js Web API',
description: 'Sample Web API to showcase Node.js compared to ASP.NET Core 1.0',
contact: {
name: 'Thinktecture AG',
email: '[email protected]',
url: 'http://thinktecture.com'
}
},
host: `localhost:${port}`,
// Reference the files containing the swagger definitions
apis: [
path.join(__dirname, '..', 'database', config.db.type, 'customerModel.js'),
path.join(__dirname, '..', 'controllers', 'customer.js')
],
produces: ['application/json'],
consumes: ['application/json'],
swaggerURL: '/docs',
swaggerJSON: '/swagger.json',
swaggerUI: path.join(__dirname, '..', 'public')
});
}
}
// Expose the Server, so it can be used outside of this JavaScript file. Like a "public class Server..."
module.exports = Server;