@thzero/library_server_fastify 0.15.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- package/boot/index.js +283 -0
- package/boot/plugins/admin/news.js +11 -0
- package/boot/plugins/admin/users.js +11 -0
- package/boot/plugins/api.js +16 -0
- package/boot/plugins/apiFront.js +11 -0
- package/boot/plugins/news.js +11 -0
- package/boot/plugins/users.js +11 -0
- package/boot/plugins/usersExtended.js +11 -0
- package/license.md +9 -0
- package/package.json +43 -0
- package/plugins/apiKey.js +49 -0
- package/plugins/responseTime.js +107 -0
- package/plugins/settings.js +13 -0
- package/plugins/usageMetrics.js +25 -0
- package/routes/admin/index.js +123 -0
- package/routes/admin/news.js +22 -0
- package/routes/admin/users.js +26 -0
- package/routes/baseNews.js +38 -0
- package/routes/baseUsers.js +103 -0
- package/routes/home.js +28 -0
- package/routes/index.js +39 -0
- package/routes/news.js +6 -0
- package/routes/plans.js +40 -0
- package/routes/users.js +6 -0
- package/routes/utility.js +51 -0
- package/routes/version.js +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+
[](https://opensource.org/licenses/MIT)
|
|
4
|
+
|
|
5
|
+
# library_server_fastify
|
|
6
|
+
|
|
7
|
+
An opinionated library of common functionality to bootstrap a Fastify based API application.
|
|
8
|
+
|
|
9
|
+
### Installation
|
|
10
|
+
|
|
11
|
+
[](https://npmjs.org/package/@thzero/library_server_fastify)
|
|
12
|
+
|
|
13
|
+
### Requirements
|
|
14
|
+
|
|
15
|
+
#### library_server
|
package/boot/index.js
ADDED
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
|
|
3
|
+
import Fastify from 'fastify';
|
|
4
|
+
import fastifyAuth from 'fastify-auth';
|
|
5
|
+
import fastifyCors from 'fastify-cors';
|
|
6
|
+
import fastifyHelmet from 'fastify-helmet';
|
|
7
|
+
import fastifyStatic from 'fastify-static';
|
|
8
|
+
|
|
9
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
10
|
+
|
|
11
|
+
import injector from '@thzero/library_common/utility/injector';
|
|
12
|
+
|
|
13
|
+
import BootMain from '@thzero/library_server/boot';
|
|
14
|
+
|
|
15
|
+
import pluginApiKey from '@thzero/library_server_fastify/plugins/apiKey';
|
|
16
|
+
import pluginResponseTime from '@thzero/library_server_fastify/plugins/responseTime';
|
|
17
|
+
import pluginSettings from '@thzero/library_server_fastify/plugins/settings';
|
|
18
|
+
import pluginUsageMetrics from '@thzero/library_server_fastify/plugins/usageMetrics';
|
|
19
|
+
|
|
20
|
+
class FastifyBootMain extends BootMain {
|
|
21
|
+
async _initApp(args, plugins) {
|
|
22
|
+
|
|
23
|
+
// const serverFactory = (handler, opts) => {
|
|
24
|
+
// const server = http.createServer((req, res) => {
|
|
25
|
+
// handler(req, res)
|
|
26
|
+
// });
|
|
27
|
+
|
|
28
|
+
// return server;
|
|
29
|
+
// };
|
|
30
|
+
|
|
31
|
+
// const fastify = Fastify({ serverFactory, logger: true });
|
|
32
|
+
const fastify = Fastify({ logger: true });
|
|
33
|
+
const serverHttp = fastify.server;
|
|
34
|
+
|
|
35
|
+
// // https://github.com/koajs/cors
|
|
36
|
+
// app.use(koaCors({
|
|
37
|
+
// allowMethods: 'GET,POST,DELETE',
|
|
38
|
+
// maxAge : 7200,
|
|
39
|
+
// allowHeaders: `${LibraryConstants.Headers.AuthKeys.API}, ${LibraryConstants.Headers.AuthKeys.AUTH}, ${LibraryConstants.Headers.CorrelationId}, Content-Type`,
|
|
40
|
+
// credentials: true,
|
|
41
|
+
// origin: '*'
|
|
42
|
+
// }));
|
|
43
|
+
// https://github.com/fastify/fastify-cors
|
|
44
|
+
fastify.register(fastifyCors, {
|
|
45
|
+
allowMethods: 'GET,POST,DELETE',
|
|
46
|
+
maxAge : 7200,
|
|
47
|
+
allowHeaders: `${LibraryConstants.Headers.AuthKeys.API}, ${LibraryConstants.Headers.AuthKeys.AUTH}, ${LibraryConstants.Headers.CorrelationId}, Content-Type`,
|
|
48
|
+
credentials: true,
|
|
49
|
+
origin: '*'
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// // https://www.npmjs.com/package/koa-helmet
|
|
53
|
+
// app.use(koaHelmet());
|
|
54
|
+
// https://github.com/fastify/fastify-helmet
|
|
55
|
+
fastify.register(
|
|
56
|
+
fastifyHelmet,
|
|
57
|
+
// Example disables the `contentSecurityPolicy` middleware but keeps the rest.
|
|
58
|
+
{
|
|
59
|
+
// contentSecurityPolicy: false
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
// // error
|
|
64
|
+
// app.use(async (ctx, next) => {
|
|
65
|
+
// try {
|
|
66
|
+
// await next();
|
|
67
|
+
// }
|
|
68
|
+
// catch (err) {
|
|
69
|
+
// ctx.status = err.status || 500;
|
|
70
|
+
// if (err instanceof TokenExpiredError) {
|
|
71
|
+
// ctx.status = 401;
|
|
72
|
+
// ctx.response.header['WWW-Authenticate'] = 'Bearer error="invalid_token", error_description="The access token expired"'
|
|
73
|
+
// }
|
|
74
|
+
// ctx.app.emit('error', err, ctx);
|
|
75
|
+
// await this.usageMetricsServiceI.register(ctx, err).catch(() => {
|
|
76
|
+
// this.loggerServiceI.exception('KoaBootMain', 'start', err);
|
|
77
|
+
// });
|
|
78
|
+
// }
|
|
79
|
+
// });
|
|
80
|
+
fastify.register(async (instance, opts, done) => {
|
|
81
|
+
// try {
|
|
82
|
+
// done();
|
|
83
|
+
// }
|
|
84
|
+
// catch (err) {
|
|
85
|
+
// let status = err.status || 500;
|
|
86
|
+
// if (err instanceof TokenExpiredError) {
|
|
87
|
+
// status = 401;
|
|
88
|
+
// response.header['WWW-Authenticate'] = 'Bearer error="invalid_token", error_description="The access token expired"'
|
|
89
|
+
// }
|
|
90
|
+
// app.emit('error', err, ctx);
|
|
91
|
+
// await this.usageMetricsServiceI.register(ctx, err).catch(() => {
|
|
92
|
+
// this.loggerServiceI.exception('KoaBootMain', 'start', err);
|
|
93
|
+
// });
|
|
94
|
+
// }
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// app.on('error', (err, ctx) => {
|
|
98
|
+
// this.loggerServiceI.error('KoaBootMain', 'start', 'Uncaught Exception', err);
|
|
99
|
+
// });
|
|
100
|
+
|
|
101
|
+
// // config
|
|
102
|
+
// app.use(async (ctx, next) => {
|
|
103
|
+
// ctx.config = this._appConfig;
|
|
104
|
+
// await next();
|
|
105
|
+
// });
|
|
106
|
+
// // correlationId
|
|
107
|
+
// app.use(async (ctx, next) => {
|
|
108
|
+
// ctx.correlationId = ctx.request.header[LibraryConstants.Headers.CorrelationId];
|
|
109
|
+
// await next();
|
|
110
|
+
// });
|
|
111
|
+
// fastify.register(async (instance, opts, done) => {
|
|
112
|
+
// instance.addHook('onRequest', (request, reply, next) => {
|
|
113
|
+
// request.config = this._appConfig;
|
|
114
|
+
// request.correlationId = ctx.request.header[LibraryConstants.Headers.CorrelationId];
|
|
115
|
+
// next();
|
|
116
|
+
// });
|
|
117
|
+
|
|
118
|
+
// done();
|
|
119
|
+
// });
|
|
120
|
+
fastify.register(pluginSettings, {
|
|
121
|
+
config: this._appConfig
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// // logger
|
|
125
|
+
// app.use(async (ctx, next) => {
|
|
126
|
+
// await next();
|
|
127
|
+
// const rt = ctx.response.get(ResponseTime);
|
|
128
|
+
// this.loggerServiceI.info2(`${ctx.method} ${ctx.url} - ${rt}`);
|
|
129
|
+
// });
|
|
130
|
+
|
|
131
|
+
// // x-response-time
|
|
132
|
+
// app.use(async (ctx, next) => {
|
|
133
|
+
// const start = Utility.timerStart();
|
|
134
|
+
// await next();
|
|
135
|
+
// const delta = Utility.timerStop(start, true);
|
|
136
|
+
// ctx.set(ResponseTime, delta);
|
|
137
|
+
// });
|
|
138
|
+
// https://github.com/lolo32/fastify-response-time
|
|
139
|
+
fastify.register(pluginResponseTime, {
|
|
140
|
+
logger: this.loggerServiceI
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// app.use(koaStatic('./public'));
|
|
144
|
+
// https://github.com/fastify/fastify-static
|
|
145
|
+
const __dirname = path.resolve();
|
|
146
|
+
fastify.register(fastifyStatic, {
|
|
147
|
+
root: path.join(__dirname, 'public'),
|
|
148
|
+
prefix: '/public/', // optional: default '/'
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
this._initPreAuth(fastify);
|
|
152
|
+
|
|
153
|
+
// // auth-api-token
|
|
154
|
+
// app.use(async (ctx, next) => {
|
|
155
|
+
// if (ctx.originalUrl === '/favicon.ico') {
|
|
156
|
+
// await next();
|
|
157
|
+
// return;
|
|
158
|
+
// }
|
|
159
|
+
|
|
160
|
+
// const key = ctx.get(LibraryConstants.Headers.AuthKeys.API);
|
|
161
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
162
|
+
// if (!String.isNullOrEmpty(key)) {
|
|
163
|
+
// const auth = ctx.config.get('auth');
|
|
164
|
+
// if (auth) {
|
|
165
|
+
// const apiKey = auth.apiKey;
|
|
166
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
167
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
168
|
+
// if (key === apiKey) {
|
|
169
|
+
// ctx.state.apiKey = key;
|
|
170
|
+
// await next();
|
|
171
|
+
// return;
|
|
172
|
+
// }
|
|
173
|
+
// }
|
|
174
|
+
// }
|
|
175
|
+
|
|
176
|
+
// (async () => {
|
|
177
|
+
// await this.usageMetricsServiceI.register(ctx).catch((err) => {
|
|
178
|
+
// this.loggerServiceI.error('KoaBootMain', 'start', 'usageMetrics', err);
|
|
179
|
+
// });
|
|
180
|
+
// })();
|
|
181
|
+
|
|
182
|
+
// console.log('Unauthorized... auth-api-token failure');
|
|
183
|
+
// ctx.throw(401);
|
|
184
|
+
// });
|
|
185
|
+
// fastify.register((instance, opts, done) => {
|
|
186
|
+
// instance.addHook('onRequest', (request, reply, next) => {
|
|
187
|
+
// if (request.originalUrl === '/favicon.ico') {
|
|
188
|
+
// next();
|
|
189
|
+
// return;
|
|
190
|
+
// }
|
|
191
|
+
|
|
192
|
+
// const key = request.get(LibraryConstants.Headers.AuthKeys.API);
|
|
193
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
194
|
+
// if (!String.isNullOrEmpty(key)) {
|
|
195
|
+
// const auth = request.config.get('auth');
|
|
196
|
+
// if (auth) {
|
|
197
|
+
// const apiKey = auth.apiKey;
|
|
198
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
199
|
+
// // this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
200
|
+
// if (key === apiKey) {
|
|
201
|
+
// request.state.apiKey = key;
|
|
202
|
+
// next();
|
|
203
|
+
// return;
|
|
204
|
+
// }
|
|
205
|
+
// }
|
|
206
|
+
// }
|
|
207
|
+
|
|
208
|
+
// (async () => {
|
|
209
|
+
// await this.usageMetricsServiceI.register(ctx).catch((err) => {
|
|
210
|
+
// this.loggerServiceI.error('FastifyBootMain', 'start', 'usageMetrics', err);
|
|
211
|
+
// });
|
|
212
|
+
// })();
|
|
213
|
+
|
|
214
|
+
// console.log('Unauthorized... auth-api-token failure');
|
|
215
|
+
// request.throw(401);
|
|
216
|
+
// next();
|
|
217
|
+
// });
|
|
218
|
+
|
|
219
|
+
// done();
|
|
220
|
+
// });
|
|
221
|
+
fastify.register(pluginApiKey, {
|
|
222
|
+
logger: this.loggerServiceI,
|
|
223
|
+
usageMetrics: this.usageMetricsServiceI
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
fastify.register(fastifyAuth, {
|
|
227
|
+
logger: this.loggerServiceI,
|
|
228
|
+
usageMetrics: this.usageMetricsServiceI
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
this._initPostAuth(fastify);
|
|
232
|
+
|
|
233
|
+
this._routes = [];
|
|
234
|
+
|
|
235
|
+
this._initPreRoutes(fastify);
|
|
236
|
+
|
|
237
|
+
for (const pluginRoute of plugins)
|
|
238
|
+
await pluginRoute.initRoutes(this._routes);
|
|
239
|
+
|
|
240
|
+
await this._initRoutes();
|
|
241
|
+
|
|
242
|
+
console.log();
|
|
243
|
+
|
|
244
|
+
for (const route of this._routes) {
|
|
245
|
+
await route.init(injector, fastify, this._appConfig);
|
|
246
|
+
|
|
247
|
+
console.log([ route.id ]);
|
|
248
|
+
|
|
249
|
+
// for (let i = 0; i < route.router.stack.length; i++)
|
|
250
|
+
// console.log([ route.router.stack[i].path, route.router.stack[i].methods ]);
|
|
251
|
+
|
|
252
|
+
console.log();
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// // usage metrics
|
|
256
|
+
// app.use(async (ctx, next) => {
|
|
257
|
+
// await next();
|
|
258
|
+
// await this.usageMetricsServiceI.register(ctx).catch((err) => {
|
|
259
|
+
// this.loggerServiceI.error('KoaBootMain', 'start', 'usageMetrics', err);
|
|
260
|
+
// });
|
|
261
|
+
// });
|
|
262
|
+
fastify.register(pluginUsageMetrics, {
|
|
263
|
+
logger: this.loggerServiceI,
|
|
264
|
+
usageMetrics: this.usageMetricsServiceI
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
return { app: fastify, server: serverHttp, listen: fastify.listen };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
_initAppListen(app, server, port, err) {
|
|
271
|
+
app.listen(port, err);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async _initAppPost(app, args) {
|
|
275
|
+
this._initPostRoutes(app);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
_initRoute(route) {
|
|
279
|
+
this._routes.push(route);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
export default FastifyBootMain;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseNewsAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/news';
|
|
2
|
+
|
|
3
|
+
import adminNewsRoute from '../../../routes/admin/news';
|
|
4
|
+
|
|
5
|
+
class NewsAdminBootPlugin extends BaseNewsAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminNews() {
|
|
7
|
+
return new adminNewsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsAdminBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseUsersAdminBootPlugin from '@thzero/library_server/boot/plugins/admin/users';
|
|
2
|
+
|
|
3
|
+
import adminUsersRoute from '../../../routes/admin/users'
|
|
4
|
+
|
|
5
|
+
class UsersAdminBootPlugin extends BaseUsersAdminBootPlugin {
|
|
6
|
+
_initRoutesAdminUsers() {
|
|
7
|
+
return new adminUsersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersAdminBootPlugin;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import BaseApiBootPlugin from '@thzero/library_server/boot/plugins/api';
|
|
2
|
+
|
|
3
|
+
import homeRoute from '../../routes/home';
|
|
4
|
+
import versionRoute from '../../routes/version';
|
|
5
|
+
|
|
6
|
+
class ApiBootPlugin extends BaseApiBootPlugin {
|
|
7
|
+
_initRoutesHome() {
|
|
8
|
+
return new homeRoute();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
_initRoutesVersion() {
|
|
12
|
+
return new versionRoute();
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export default ApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseApiFrontBootPlugin from '@thzero/library_server/boot/plugins/apiFront';
|
|
2
|
+
|
|
3
|
+
import utilityRoute from '../../routes/utility';
|
|
4
|
+
|
|
5
|
+
class FrontApiBootPlugin extends BaseApiFrontBootPlugin {
|
|
6
|
+
_initRoutesUtility() {
|
|
7
|
+
return new utilityRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default FrontApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseNewsApiBootPlugin from '@thzero/library_server/boot/plugins/news';
|
|
2
|
+
|
|
3
|
+
import newsRoute from '../../routes/news';
|
|
4
|
+
|
|
5
|
+
class NewsApiBootPlugin extends BaseNewsApiBootPlugin {
|
|
6
|
+
_initRoutesNews() {
|
|
7
|
+
return new newsRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default NewsApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseUsersApiBootPlugin from '@thzero/library_server/boot/plugins/users';
|
|
2
|
+
|
|
3
|
+
import usersRoute from '../../routes/users';
|
|
4
|
+
|
|
5
|
+
class UsersApiBootPlugin extends BaseUsersApiBootPlugin {
|
|
6
|
+
_initRoutesUsers() {
|
|
7
|
+
return new usersRoute();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default UsersApiBootPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import BaseExtendedUsersApiBootPlugin from '@thzero/library_server/boot/plugins/usersExtended';
|
|
2
|
+
|
|
3
|
+
import plansService from '../../service/plans';
|
|
4
|
+
|
|
5
|
+
class ExtendedUsersApiBootPlugin extends BaseExtendedUsersApiBootPlugin {
|
|
6
|
+
_initServicesPlans() {
|
|
7
|
+
return new plansService();
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default ExtendedUsersApiBootPlugin;
|
package/license.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020-2022 thZero.com
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@thzero/library_server_fastify",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.15.32",
|
|
5
|
+
"version_major": 0,
|
|
6
|
+
"version_minor": 15,
|
|
7
|
+
"version_patch": 32,
|
|
8
|
+
"version_date": "04/18/2022",
|
|
9
|
+
"description": "An opinionated library of common functionality to bootstrap a Fastify based API application.",
|
|
10
|
+
"author": "thZero",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/thzero/library_server_fastify.git"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/thzero/library_server_fastify/issues"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://github.com/thzero/library_server_fastify#readme",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=12.8.3"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"cli-update": "./node_modules/.bin/library-cli --updateversion --pi",
|
|
25
|
+
"cli-update-w": ".\\node_modules\\.bin\\library-cli --updateversion --pi",
|
|
26
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"async-mutex": "^0.3.2",
|
|
30
|
+
"fastify": "^3.28.0",
|
|
31
|
+
"fastify-auth": "^1.1.0",
|
|
32
|
+
"fastify-cors": "^6.0.3",
|
|
33
|
+
"fastify-helmet": "^7.0.1",
|
|
34
|
+
"fastify-static": "^4.6.1"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@thzero/library_cli": "^0.15"
|
|
38
|
+
},
|
|
39
|
+
"peerDependencies": {
|
|
40
|
+
"@thzero/library_common": "^0.15",
|
|
41
|
+
"@thzero/library_common_service": "^0.15"
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
4
|
+
|
|
5
|
+
export default fastifyPlugin((instance, opts, done) => {
|
|
6
|
+
instance.addHook('onRequest', (request, reply, next) => {
|
|
7
|
+
if (request.originalUrl === '/favicon.ico') {
|
|
8
|
+
next();
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const key = request.headers[LibraryConstants.Headers.AuthKeys.API];
|
|
13
|
+
// opts.logger.debug('KoaBootMain', 'start', 'auth-api-token.key', key);
|
|
14
|
+
if (!String.isNullOrEmpty(key)) {
|
|
15
|
+
const auth = request.config.get('auth');
|
|
16
|
+
if (auth) {
|
|
17
|
+
const apiKey = auth.apiKey;
|
|
18
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.apiKey', apiKey);
|
|
19
|
+
// this.loggerServiceI.debug('KoaBootMain', 'start', 'auth-api-token.key===apiKey', (key === apiKey));
|
|
20
|
+
if (key === apiKey) {
|
|
21
|
+
request.apiKey = key;
|
|
22
|
+
next();
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
(async () => {
|
|
29
|
+
const usageMetrics = {
|
|
30
|
+
url: request.routerPath,
|
|
31
|
+
correlationId: request.correlationId,
|
|
32
|
+
href: request.url,
|
|
33
|
+
headers: request.headers,
|
|
34
|
+
host: request.hostname,
|
|
35
|
+
hostname: request.hostname,
|
|
36
|
+
querystring: request.query,
|
|
37
|
+
token: request.token
|
|
38
|
+
};
|
|
39
|
+
await opts.usageMetrics.register(usageMetrics).catch((err) => {
|
|
40
|
+
opts.logger.error('FastifyBootMain', 'start', 'usageMetrics', err);
|
|
41
|
+
});
|
|
42
|
+
})();
|
|
43
|
+
|
|
44
|
+
console.log('Unauthorized... auth-api-token failure');
|
|
45
|
+
reply.status(401).send();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
done();
|
|
49
|
+
});
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// https://github.com/lolo32/fastify-response-time
|
|
2
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
3
|
+
|
|
4
|
+
const symbolRequestTime = Symbol('RequestTimer');
|
|
5
|
+
const symbolServerTiming = Symbol('ServerTiming');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param {string} name
|
|
10
|
+
* @param {number|string} duration
|
|
11
|
+
* @param {string} description
|
|
12
|
+
* @return {string}
|
|
13
|
+
*/
|
|
14
|
+
const genTick = (name, duration, description) => {
|
|
15
|
+
let val = name;
|
|
16
|
+
// Parse duration. If could not be converted to float, does not add it
|
|
17
|
+
duration = parseFloat(duration);
|
|
18
|
+
if (!isNaN(duration)) {
|
|
19
|
+
val += `;dur=${duration}`;
|
|
20
|
+
}
|
|
21
|
+
// Parse the description. If empty, doest not add it. If string with space, double quote value
|
|
22
|
+
if ('string' === typeof description) {
|
|
23
|
+
val += `;desc=${description.includes(' ') ? `'${description}'` : description}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return val;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Decorators
|
|
31
|
+
*
|
|
32
|
+
* @param {fastify} instance
|
|
33
|
+
* @param {function} instance.decorateReply
|
|
34
|
+
* @param {Object} opts
|
|
35
|
+
* @param {function} next
|
|
36
|
+
*/
|
|
37
|
+
export default fastifyPlugin((instance, opts, done) => {
|
|
38
|
+
// Check the options, and corrects with the default values if inadequate
|
|
39
|
+
if (isNaN(opts.digits) || 0 > opts.digits) {
|
|
40
|
+
opts.digits = 2;
|
|
41
|
+
}
|
|
42
|
+
opts.header = opts.header || 'X-Response-Time';
|
|
43
|
+
|
|
44
|
+
// Hook to be triggered on request (start time)
|
|
45
|
+
instance.addHook('onRequest', (request, reply, next) => {
|
|
46
|
+
// Store the start timer in nanoseconds resolution
|
|
47
|
+
// istanbul ignore next
|
|
48
|
+
if (request.raw && reply.raw) {
|
|
49
|
+
// support fastify >= v2
|
|
50
|
+
request.raw[symbolRequestTime] = process.hrtime();
|
|
51
|
+
reply.raw[symbolServerTiming] = {};
|
|
52
|
+
}
|
|
53
|
+
else if (request.req && reply.res) {
|
|
54
|
+
// support fastify >= v2
|
|
55
|
+
request.req[symbolRequestTime] = process.hrtime();
|
|
56
|
+
reply.res[symbolServerTiming] = {};
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
request[symbolRequestTime] = process.hrtime();
|
|
60
|
+
reply[symbolServerTiming] = {};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
next();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Hook to be triggered just before response to be send
|
|
67
|
+
instance.addHook('onSend', (request, reply, payload, next) => {
|
|
68
|
+
// check if Server-Timing need to be added
|
|
69
|
+
const serverTiming = (reply.raw ? reply.raw[symbolServerTiming] : reply.res[symbolServerTiming]);
|
|
70
|
+
const headers = [];
|
|
71
|
+
for (const name of Object.keys(serverTiming)) {
|
|
72
|
+
headers.push(serverTiming[name]);
|
|
73
|
+
}
|
|
74
|
+
if (headers.length) {
|
|
75
|
+
reply.header('Server-Timing', headers.join(','));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Calculate the duration, in nanoseconds …
|
|
79
|
+
const hrDuration = (request.raw ? request.raw[symbolRequestTime] : request.req[symbolRequestTime]);
|
|
80
|
+
// … convert it to milliseconds …
|
|
81
|
+
const duration = (hrDuration[0] * 1e3 + hrDuration[1] / 1e6).toFixed(opts.digits);
|
|
82
|
+
// … add the header to the response
|
|
83
|
+
reply.header(opts.header, duration);
|
|
84
|
+
|
|
85
|
+
opts.logger.info2(`${request.method} ${request.url} - ${duration}`);
|
|
86
|
+
|
|
87
|
+
next();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Can be used to add custom timing information
|
|
91
|
+
instance.decorateReply('setServerTiming', function (name, duration, description) {
|
|
92
|
+
// Reference to the res object storing values …
|
|
93
|
+
const serverTiming = this.res[symbolServerTiming];
|
|
94
|
+
// … return if value already exists (all subsequent occurrences MUST be ignored without signaling an error) …
|
|
95
|
+
if (serverTiming.hasOwnProperty(name)) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
// … add the value the the list to send later
|
|
99
|
+
serverTiming[name] = genTick(name, duration, description);
|
|
100
|
+
// … return true, the value was added to the list
|
|
101
|
+
return true;
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
done();
|
|
105
|
+
});
|
|
106
|
+
// Not before 0.31 (onSend hook added to this version)
|
|
107
|
+
// }, '>= 0.31');
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
4
|
+
|
|
5
|
+
export default fastifyPlugin((instance, opts, done) => {
|
|
6
|
+
instance.addHook('onRequest', (request, reply, next) => {
|
|
7
|
+
request.config = opts.config;
|
|
8
|
+
request.correlationId = request.headers[LibraryConstants.Headers.CorrelationId];
|
|
9
|
+
next();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
done();
|
|
13
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
export default fastifyPlugin((instance, opts, done) => {
|
|
4
|
+
instance.addHook('onSend', (request, reply, payload, next) => {
|
|
5
|
+
(async () => {
|
|
6
|
+
const usageMetrics = {
|
|
7
|
+
url: request.routerPath,
|
|
8
|
+
correlationId: request.correlationId,
|
|
9
|
+
href: request.url,
|
|
10
|
+
headers: request.headers,
|
|
11
|
+
host: request.hostname,
|
|
12
|
+
hostname: request.hostname,
|
|
13
|
+
querystring: request.query,
|
|
14
|
+
token: request.token
|
|
15
|
+
};
|
|
16
|
+
await opts.usageMetrics.register(usageMetrics).catch((err) => {
|
|
17
|
+
opts.logger.error('usageMetrics', 'start', 'usageMetrics', err);
|
|
18
|
+
});
|
|
19
|
+
})();
|
|
20
|
+
|
|
21
|
+
next();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
done();
|
|
25
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from '../index';
|
|
6
|
+
|
|
7
|
+
import authentication from '../../middleware/authentication';
|
|
8
|
+
import authorization from '../../middleware/authorization';
|
|
9
|
+
|
|
10
|
+
class AdminBaseRoute extends BaseRoute {
|
|
11
|
+
constructor(urlFragment, role, serviceKey) {
|
|
12
|
+
if (!urlFragment)
|
|
13
|
+
throw Error('Invalid url fragment');
|
|
14
|
+
|
|
15
|
+
super(`/admin/${urlFragment}`);
|
|
16
|
+
|
|
17
|
+
this._options = {
|
|
18
|
+
role: role,
|
|
19
|
+
serviceKey: serviceKey
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// this._service = null;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async init(injector, config) {
|
|
26
|
+
const router = await super.init(injector, config);
|
|
27
|
+
router.service = injector.getService(this._options.serviceKey);
|
|
28
|
+
// this._service = injector.getService(this._options.serviceKey);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
_allowsCreate() {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_allowsDelete() {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
_allowsUpdate() {
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_initializeRoutesCreate(router) {
|
|
44
|
+
const self = this;
|
|
45
|
+
router.post('/',
|
|
46
|
+
authentication(true),
|
|
47
|
+
authorization([ `${self._options.role}.create` ]),
|
|
48
|
+
koaBody({
|
|
49
|
+
text: false,
|
|
50
|
+
}),
|
|
51
|
+
// eslint-disable-next-line
|
|
52
|
+
async (ctx, next) => {
|
|
53
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
54
|
+
// const response = (await router.service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
55
|
+
const response = (await ctx.router.service.create(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
56
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_initializeRoutesDelete(router) {
|
|
62
|
+
const self = this;
|
|
63
|
+
router.delete('/:id',
|
|
64
|
+
authentication(true),
|
|
65
|
+
authorization([ `${self._options.role}.delete` ]),
|
|
66
|
+
// eslint-disable-next-line
|
|
67
|
+
async (ctx, next) => {
|
|
68
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
69
|
+
// const response = (await service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
|
|
70
|
+
const response = (await ctx.router.service.delete(ctx.correlationId, ctx.state.user, ctx.params.id)).check(ctx);
|
|
71
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_initializeRoutesUpdate(router) {
|
|
77
|
+
const self = this;
|
|
78
|
+
router.post('/:id',
|
|
79
|
+
authentication(true),
|
|
80
|
+
authorization([ `${self._options.role}.update` ]),
|
|
81
|
+
koaBody({
|
|
82
|
+
text: false,
|
|
83
|
+
}),
|
|
84
|
+
// eslint-disable-next-line
|
|
85
|
+
async (ctx, next) => {
|
|
86
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
87
|
+
// const response = (await service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
|
|
88
|
+
const response = (await ctx.router.service.update(ctx.correlationId, ctx.state.user, ctx.params.id, ctx.request.body)).check(ctx);
|
|
89
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
_initializeRoutes(router) {
|
|
95
|
+
if (this._allowsDelete)
|
|
96
|
+
this._initializeRoutesDelete(router);
|
|
97
|
+
|
|
98
|
+
router.post('/search',
|
|
99
|
+
authentication(true),
|
|
100
|
+
authorization([ `${this._options.role}.search` ]),
|
|
101
|
+
koaBody({
|
|
102
|
+
text: false,
|
|
103
|
+
}),
|
|
104
|
+
// eslint-disable-next-line
|
|
105
|
+
async (ctx, next) => {
|
|
106
|
+
// const service = this._injector.getService(this._options.serviceKey);
|
|
107
|
+
// const response = (await service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
108
|
+
const response = (await ctx.router.service.search(ctx.correlationId, ctx.state.user, ctx.request.body)).check(ctx);
|
|
109
|
+
this._jsonResponse(ctx, Utility.stringify(response));
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
if (this._allowsUpdate())
|
|
114
|
+
this._initializeRoutesUpdate(router);
|
|
115
|
+
|
|
116
|
+
if (this._allowsCreate())
|
|
117
|
+
this._initializeRoutesCreate(router);
|
|
118
|
+
|
|
119
|
+
return router;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default AdminBaseRoute;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import LibraryConstants from '../../constants';
|
|
2
|
+
|
|
3
|
+
import AdminRoute from './index'
|
|
4
|
+
|
|
5
|
+
class NewsAdminRoute extends AdminRoute {
|
|
6
|
+
constructor(urlFragment, role, serviceKey) {
|
|
7
|
+
urlFragment = urlFragment ? urlFragment : 'news';
|
|
8
|
+
role = role ? role : 'news';
|
|
9
|
+
serviceKey = serviceKey ? serviceKey : LibraryConstants.InjectorKeys.SERVICE_ADMIN_NEWS;
|
|
10
|
+
super(urlFragment, role, serviceKey);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get id() {
|
|
14
|
+
return 'admin-news';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
get _version() {
|
|
18
|
+
return 'v1';
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default NewsAdminRoute;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import LibraryConstants from '../../constants';
|
|
2
|
+
|
|
3
|
+
import AdminRoute from './index'
|
|
4
|
+
|
|
5
|
+
class UsersAdminRoute extends AdminRoute {
|
|
6
|
+
constructor(urlFragment, role, serviceKey) {
|
|
7
|
+
urlFragment = urlFragment ? urlFragment : 'users';
|
|
8
|
+
role = role ? role : 'users';
|
|
9
|
+
serviceKey = serviceKey ? serviceKey : LibraryConstants.InjectorKeys.SERVICE_ADMIN_USERS;
|
|
10
|
+
super(urlFragment, role, serviceKey);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
get id() {
|
|
14
|
+
return 'admin-users';
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_allowsCreate() {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
get _version() {
|
|
22
|
+
return 'v1';
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default UsersAdminRoute;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import LibraryConstants from '../constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
import authentication from '../middleware/authentication';
|
|
8
|
+
|
|
9
|
+
class BaseNewsRoute extends BaseRoute {
|
|
10
|
+
constructor(prefix, version) {
|
|
11
|
+
super(prefix ? prefix : '/news');
|
|
12
|
+
|
|
13
|
+
// this._serviceNews = null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async init(injector, app, config) {
|
|
17
|
+
await super.init(injector, app, config);
|
|
18
|
+
// this._serviceNews = injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
19
|
+
this._inject(app, injector, LibraryConstants.InjectorKeys.SERVICE_NEWS, LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
get id() {
|
|
23
|
+
return 'news';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
_initializeRoutes(router) {
|
|
27
|
+
// authentication(false),
|
|
28
|
+
router.get(this._join('/latest/:date'),
|
|
29
|
+
async (request, reply) => {
|
|
30
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_NEWS);
|
|
31
|
+
// const response = (await service.latest(ctx.correlationId, ctx.state.user, parseInt(ctx.params.date))).check(ctx);
|
|
32
|
+
const response = (await request.serviceNews.latest(request.correlationId, request.user, parseInt(request.params.date))).check(request);
|
|
33
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export default BaseNewsRoute;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '../constants';
|
|
4
|
+
|
|
5
|
+
import Utility from '@thzero/library_common/utility';
|
|
6
|
+
|
|
7
|
+
import BaseRoute from './index';
|
|
8
|
+
|
|
9
|
+
import authentication from '../middleware/authentication';
|
|
10
|
+
import authorization from '../middleware/authorization';
|
|
11
|
+
|
|
12
|
+
class BaseUsersRoute extends BaseRoute {
|
|
13
|
+
constructor(prefix, version) {
|
|
14
|
+
super(prefix ? prefix : '/users');
|
|
15
|
+
|
|
16
|
+
// this._serviceUsers = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async init(injector, app, config) {
|
|
20
|
+
await super.init(injector, app, config);
|
|
21
|
+
// this._serviceUsers = injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
22
|
+
this._inject(app, injector, LibraryConstants.InjectorKeys.SERVICE_USERS, LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get id() {
|
|
26
|
+
return 'users';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_initializeRoutes(router) {
|
|
30
|
+
this._initializeRoutesGamerById(router);
|
|
31
|
+
this._initializeRoutesGamerByTag(router);
|
|
32
|
+
this._initializeRoutesRefreshSettings(router);
|
|
33
|
+
this._initializeRoutesUpdate(router);
|
|
34
|
+
this._initializeRoutesUpdateSettings(router);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_initializeRoutesGamerById(router) {
|
|
38
|
+
return router.get('/gamerId/:gamerId',
|
|
39
|
+
// authentication(false),
|
|
40
|
+
// // authorization('user'),
|
|
41
|
+
// eslint-disable-next-line
|
|
42
|
+
async (request, reply) => {
|
|
43
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
44
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].fetchByGamerId(request.correlationId, request.params.gamerId)).check(request);
|
|
45
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
46
|
+
}
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_initializeRoutesGamerByTag(router) {
|
|
51
|
+
return router.get('/gamerTag/:gamerTag',
|
|
52
|
+
// authentication(false),
|
|
53
|
+
// // authorization('user'),
|
|
54
|
+
// eslint-disable-next-line
|
|
55
|
+
async (request, reply) => {
|
|
56
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
57
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].fetchByGamerTag(request.correlationId, request.params.gamerTag)).check(request);
|
|
58
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_initializeRoutesRefreshSettings(router) {
|
|
64
|
+
return router.post('/refresh/settings',
|
|
65
|
+
// authentication(true),
|
|
66
|
+
// authorization('user'),
|
|
67
|
+
// eslint-disable-next-line
|
|
68
|
+
async (request, reply) => {
|
|
69
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
70
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].refreshSettings(request.correlationId, request.body)).check(request);
|
|
71
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
_initializeRoutesUpdate(router) {
|
|
77
|
+
return router.post('/update',
|
|
78
|
+
// authentication(true),
|
|
79
|
+
// authorization('user'),
|
|
80
|
+
// eslint-disable-next-line
|
|
81
|
+
async (request, reply) => {
|
|
82
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
83
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].update(request.correlationId, request.body)).check(request);
|
|
84
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
_initializeRoutesUpdateSettings(router) {
|
|
90
|
+
return router.post('/update/settings',
|
|
91
|
+
// authentication(true),
|
|
92
|
+
// authorization('user'),
|
|
93
|
+
// eslint-disable-next-line
|
|
94
|
+
async (request, reply) => {
|
|
95
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_USERS);
|
|
96
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_USERS].updateSettings(request.correlationId, request.body)).check(request);
|
|
97
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export default BaseUsersRoute;
|
package/routes/home.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import BaseRoute from './index';
|
|
2
|
+
|
|
3
|
+
class HomeRoute extends BaseRoute {
|
|
4
|
+
constructor(prefix) {
|
|
5
|
+
super(prefix ? prefix : '');
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
get id() {
|
|
9
|
+
return 'home';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_initializeRoutes(router) {
|
|
13
|
+
// eslint-disable-next-linethis._prefix
|
|
14
|
+
router.get(this._join('/'), (request, reply) => {
|
|
15
|
+
reply.status(401).send();
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
get _ignoreApi() {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
get _version() {
|
|
24
|
+
return '';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default HomeRoute;
|
package/routes/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import fastifyPlugin from 'fastify-plugin';
|
|
2
|
+
|
|
3
|
+
import BaseRoute from'@thzero/library_server/routes/index';
|
|
4
|
+
|
|
5
|
+
class FastifyBaseRoute extends BaseRoute {
|
|
6
|
+
_initializeRouter(app, config) {
|
|
7
|
+
return app;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async _inject(app, injector, key, name) {
|
|
11
|
+
const service = injector.getService(key);
|
|
12
|
+
if (!service)
|
|
13
|
+
throw Error(`Invalid service for '${key}'.`);
|
|
14
|
+
|
|
15
|
+
app.register(fastifyPlugin((instance, opts, done) => {
|
|
16
|
+
instance.decorate(opts.name, opts.service);
|
|
17
|
+
done();
|
|
18
|
+
}), {
|
|
19
|
+
name: name,
|
|
20
|
+
service: service
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_join(path) {
|
|
25
|
+
return this._prefix + path;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
_jsonResponse(reply, json) {
|
|
29
|
+
if (!reply)
|
|
30
|
+
throw Error('Invalid context for response.');
|
|
31
|
+
|
|
32
|
+
reply
|
|
33
|
+
.code(200)
|
|
34
|
+
.header('Content-Type', 'application/json; charset=utf-8')
|
|
35
|
+
.send(json);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export default FastifyBaseRoute;
|
package/routes/news.js
ADDED
package/routes/plans.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import LibraryConstants from '../constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
class PlansRoute extends BaseRoute {
|
|
8
|
+
constructor(prefix) {
|
|
9
|
+
super(prefix ? prefix : '/plans');
|
|
10
|
+
|
|
11
|
+
// this._servicePlans = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async init(injector, app, config) {
|
|
15
|
+
await super.init(injector, app, config);
|
|
16
|
+
// this._servicePlans = injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
17
|
+
this._inject(app, injector, LibraryConstants.InjectorKeys.SERVICE_PLANS, LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get id() {
|
|
21
|
+
return 'plans';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_initializeRoutes(router) {
|
|
25
|
+
router.get(this._join('/'),
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
async (ctx, next) => {
|
|
28
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_PLANS);
|
|
29
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_PLANS].logger(request.correlationI)).check(request);
|
|
30
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get _version() {
|
|
36
|
+
return 'v1';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default PlansRoute;
|
package/routes/users.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import koaBody from 'koa-body';
|
|
2
|
+
|
|
3
|
+
import LibraryConstants from '../constants';
|
|
4
|
+
|
|
5
|
+
import Utility from '@thzero/library_common/utility';
|
|
6
|
+
|
|
7
|
+
import BaseRoute from './index';
|
|
8
|
+
|
|
9
|
+
import authentication from '../middleware/authentication';
|
|
10
|
+
// import authorization from '../middleware/authorization';
|
|
11
|
+
|
|
12
|
+
class UtilityRoute extends BaseRoute {
|
|
13
|
+
constructor(prefix) {
|
|
14
|
+
super(prefix ? prefix : '/utility');
|
|
15
|
+
|
|
16
|
+
// this._serviceUtility = null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
async init(injector, app, config) {
|
|
20
|
+
await super.init(injector, app, config);
|
|
21
|
+
// this._serviceUtility = injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
22
|
+
this._inject(app, injector, LibraryConstants.InjectorKeys.SERVICE_UTILITY, LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
get id() {
|
|
26
|
+
return 'utility';
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
_initializeRoutes(router) {
|
|
30
|
+
router.post('/logger',
|
|
31
|
+
authentication(false),
|
|
32
|
+
// authorization('utility'),
|
|
33
|
+
// eslint-disable-next-line,
|
|
34
|
+
koaBody({
|
|
35
|
+
text: false,
|
|
36
|
+
}),
|
|
37
|
+
async (ctx, next) => {
|
|
38
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_UTILITY);
|
|
39
|
+
// const response = (await service.logger(ctx.correlationId, ctx.request.body)).check(ctx);
|
|
40
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_UTILITY].logger(request.correlationI, request.body)).check(request);
|
|
41
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
42
|
+
}
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
get _version() {
|
|
47
|
+
return 'v1';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export default UtilityRoute;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import LibraryConstants from '@thzero/library_server/constants';
|
|
2
|
+
|
|
3
|
+
import Utility from '@thzero/library_common/utility';
|
|
4
|
+
|
|
5
|
+
import BaseRoute from './index';
|
|
6
|
+
|
|
7
|
+
class VersionRoute extends BaseRoute {
|
|
8
|
+
constructor(prefix) {
|
|
9
|
+
super(prefix ? prefix : '');
|
|
10
|
+
|
|
11
|
+
// this._serviceVersion = null;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async init(injector, app, config) {
|
|
15
|
+
await super.init(injector, app, config);
|
|
16
|
+
// this._serviceVersion = injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
17
|
+
this._inject(app, injector, LibraryConstants.InjectorKeys.SERVICE_VERSION, LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get id() {
|
|
21
|
+
return 'version';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_initializeRoutes(router) {
|
|
25
|
+
router.get(this._join('/version'),
|
|
26
|
+
// eslint-disable-next-line
|
|
27
|
+
async (request, reply) => {
|
|
28
|
+
// const service = this._injector.getService(LibraryConstants.InjectorKeys.SERVICE_VERSION);
|
|
29
|
+
const response = (await router[LibraryConstants.InjectorKeys.SERVICE_VERSION].version(request.correlationId)).check(request);
|
|
30
|
+
this._jsonResponse(reply, Utility.stringify(response));
|
|
31
|
+
}
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get _version() {
|
|
36
|
+
return 'v1';
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default VersionRoute;
|