Przeglądaj źródła

progress on health route

tags/0.0.1^2
juancarbajal98 3 lat temu
rodzic
commit
f65e23d2e0

+ 9
- 0
backend/lib/plugins/health.js Wyświetl plik

1
+const HealthRoute = require('../routes/health')
2
+
3
+module.exports = {
4
+    name: 'health-plugin',
5
+    version: '1.0.0',
6
+    register: async (server, options) => {
7
+        await server.route(HealthRoute)
8
+    },
9
+}

+ 70
- 0
backend/lib/routes/health/get.js Wyświetl plik

1
+'use strict'
2
+
3
+const apiSchema = require('../../schemas/api')
4
+const errorSchema = require('../../schemas/errors')
5
+/* const healthSchema = require('../../schemas/health') // todo: maybe write healthSchema? */
6
+
7
+const pluginConfig = {
8
+    handlerType: 'health',
9
+    docs: {
10
+        description: 'Get server stats',
11
+        notes: 'Returns stats on server status'
12
+    }
13
+}
14
+
15
+const responseSchemas = {
16
+    api: apiSchema.single,
17
+    error: errorSchema.single
18
+}
19
+
20
+module.exports = {
21
+    method: 'GET',
22
+    path: '/',
23
+    options:{
24
+        ...pluginConfig.docs,
25
+        tags: ['api'],
26
+        auth: false,
27
+        cors: true,
28
+        handler: async function (request, h) {
29
+            const { healthService } = request.server.services()
30
+
31
+            const res = {
32
+                ok: true,
33
+                handler: pluginConfig.handlerType,
34
+                data:null
35
+            }
36
+
37
+            res.data = await healthService.getStats()
38
+
39
+            try {
40
+                return h.response(res).code(200)
41
+            } catch (err) {
42
+                return h
43
+                    .response({
44
+                        ok: false,
45
+                        handler: pluginConfig.handlerType,
46
+                        data: {error: `${err}`}
47
+                    })
48
+                    .code(409)
49
+            }
50
+        },
51
+        validate: {
52
+            failAction: 'log'
53
+        },
54
+
55
+        response: {
56
+            status: {
57
+                200: apiSchema.single
58
+                // .append({
59
+                //   data: responseSchemas.api,
60
+                // })
61
+                    .label('api_single_res'),
62
+                409: apiSchema.single
63
+                    .append({
64
+                        data: responseSchemas.error,
65
+                    })
66
+                    .label('error_single_res'),
67
+            },
68
+        },
69
+    },
70
+}

+ 11
- 0
backend/lib/services/health.js Wyświetl plik

1
+const Schmervice = require('@hapipal/schmervice')
2
+
3
+module.exports = class HealthService extends Schmervice.Service {
4
+    constructor(...args){
5
+        super(...args)
6
+    }
7
+
8
+    async getStats(){
9
+    // todo: define and return server stats object
10
+    }
11
+}

Ładowanie…
Anuluj
Zapisz