forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnections-sep-docs.js
107 lines (90 loc) · 2.38 KB
/
connections-sep-docs.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
'use strict';
// `connections-sep-docs.js` - how to have API on one connection and documentation on another
const Blipp = require('blipp');
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const HapiSwagger = require('../');
let server = new Hapi.Server();
// the API is on 3000 and the documentation is on 3001
server.connection({ host: 'localhost', port: 3000, labels: 'api', routes: { cors: true } });
server.connection({ host: 'localhost', port: 3001, labels: 'docs' });
let testPlugin = function (plugin, options, next) {
plugin.route({
method: 'GET',
path: '/plugin1',
config: {
handler: function (request, reply) {
reply({ 'msg':'Hi from plugin 1' }).type('application/json');
},
description: 'plugin1',
tags: ['api']
}
});
next();
};
testPlugin.attributes = { name: 'plugin1' };
let swaggerOptions = {
schemes: ['http'],
info: {
'title': 'Test API Documentation',
'description': 'This is a sample example of API documentation.',
'version': '1.0.0',
'termsOfService': 'https://github.com/glennjones/hapi-swagger/',
'contact': {
'email': 'glennjonesnet@gmail.com'
},
'license': {
'name': 'MIT',
'url': 'https://raw.githubusercontent.com/glennjones/hapi-swagger/master/license.txt'
}
},
connectionLabel: 'api'
};
//connectionToDocument: server.select(['api']),
const goodOptions = {
ops: {
interval: 1000
},
reporters: {
console: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{
log: '*',
response: '*'
}]
}, {
module: 'good-console'
}, 'stdout']
}
};
server.register([
Inert,
Vision,
Blipp,
{
register: testPlugin,
select: ['api']
},
{
register: HapiSwagger,
options: swaggerOptions,
select: ['docs']
}, {
register: require('good'),
options: goodOptions,
select: ['docs', 'api']
}
], (err) => {
if (err) {
console.log(err);
}
server.start((err) => {
if (err) {
console.log(err);
} else {
console.log('Server running');
}
});
});