forked from hapi-swagger/hapi-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
executable file
·155 lines (142 loc) · 3.29 KB
/
options.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// `options.js` - how to use many of the plug-in options
const BearerToken = require('hapi-auth-bearer-token');
const Blipp = require('blipp');
const Hapi = require('@hapi/hapi');
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');
const HapiSwagger = require('../');
const Pack = require('../package');
let Routes = require('./assets/routes-complex.js');
/*
const goodOptions = {
ops: {
interval: 1000
},
reporters: {
console: [
{
module: '@hapi/good-squeeze',
name: 'Squeeze',
args: [
{
log: '*',
response: '*'
}
]
},
{
module: 'good-console'
},
'stdout'
]
}
};
*/
let swaggerOptions = {
basePath: '/v1',
pathPrefixSize: 2,
info: {
title: 'Test API Documentation',
description: 'This is a sample example of API documentation.',
version: Pack.version,
termsOfService: 'https://github.com/glennjones/hapi-swagger/',
contact: {
email: '[email protected]'
},
license: {
name: 'MIT',
url: 'https://raw.githubusercontent.com/glennjones/hapi-swagger/master/license.txt'
}
},
tags: [
{
name: 'sum',
description: 'working with maths',
externalDocs: {
description: 'Find out more',
url: 'http://example.org'
}
},
{
name: 'store',
description: 'storing data',
externalDocs: {
description: 'Find out more',
url: 'http://example.org'
}
},
{
name: 'properties',
description: 'test the use of extended hapi/joi properties',
externalDocs: {
description: 'Find out more',
url: 'http://example.org'
}
}
],
securityDefinitions: {
Bearer: {
type: 'apiKey',
name: 'Authorization',
in: 'header',
'x-keyPrefix': 'Bearer '
}
},
security: [{ Bearer: [] }],
deReference: false,
cache: {
expiresIn: 24 * 60 * 60 * 1000
}
};
const ser = async () => {
const server = Hapi.Server({
host: 'localhost',
port: 3000
});
await server.register(BearerToken);
server.auth.strategy('bearer', 'bearer-access-token', {
accessTokenName: 'access_token',
validate: async (request, token) => {
const isValid = token === '12345';
let credentials = null;
let artifacts = null;
if (isValid) {
credentials = { token };
artifacts = {
user: {
username: 'glennjones',
name: 'Glenn Jones',
groups: ['admin', 'user']
}
};
return { isValid, credentials, artifacts };
}
}
});
// Blipp and Good - Needs updating for Hapi v17.x
await server.register([
Inert,
Vision,
Blipp,
{
plugin: HapiSwagger,
options: swaggerOptions
}
]);
server.route(Routes);
server.views({
path: 'examples/assets',
engines: { html: require('handlebars') },
isCached: false
});
await server.start();
return server;
};
ser()
.then(server => {
console.log(`Server listening on ${server.info.uri}`);
})
.catch(err => {
console.error(err);
process.exit(1);
});