Bläddra i källkod

working to display date on endpoint

tags/0.0.1^2
juancarbajal98 3 år sedan
förälder
incheckning
c9ef5ecab9

+ 5
- 0
backend/lib/index.js Visa fil

@@ -3,6 +3,7 @@ const MembershipPlugin = require('./plugins/membership')
3 3
 const SurveyPlugin = require('./plugins/survey')
4 4
 const ProfilePlugin = require('./plugins/profile')
5 5
 const NotificationPlugin = require('./plugins/notification')
6
+const HealthPlugin = require('./plugins/health')
6 7
 
7 8
 /**
8 9
  * A Hapi server instance
@@ -45,5 +46,9 @@ exports.plugin = {
45 46
         await server.register(NotificationPlugin, {
46 47
             routes: { prefix: '/notification' },
47 48
         })
49
+
50
+        await server.register(HealthPlugin, {
51
+            routes: { prefix: '/health' },
52
+        })
48 53
     },
49 54
 }

+ 3
- 1
backend/lib/plugins/health.js Visa fil

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

+ 14
- 15
backend/lib/routes/health/get.js Visa fil

@@ -2,7 +2,7 @@
2 2
 
3 3
 const apiSchema = require('../../schemas/api')
4 4
 const errorSchema = require('../../schemas/errors')
5
-/* const healthSchema = require('../../schemas/health') // todo: maybe write healthSchema? */
5
+const healthSchema = require('../../schemas/health') // todo: maybe write healthSchema?
6 6
 
7 7
 const pluginConfig = {
8 8
     handlerType: 'health',
@@ -12,8 +12,10 @@ const pluginConfig = {
12 12
     }
13 13
 }
14 14
 
15
+const validators = {}
16
+
15 17
 const responseSchemas = {
16
-    api: apiSchema.single,
18
+    health: healthSchema.stats,
17 19
     error: errorSchema.single
18 20
 }
19 21
 
@@ -27,17 +29,13 @@ module.exports = {
27 29
         cors: true,
28 30
         handler: async function (request, h) {
29 31
             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
-
32
+            const stats =  healthService.getStats()
39 33
             try {
40
-                return h.response(res).code(200)
34
+                return h.response(({
35
+                    ok:true,
36
+                    handler: pluginConfig.handlerType,
37
+                    data: stats
38
+                })).code(200)
41 39
             } catch (err) {
42 40
                 return h
43 41
                     .response({
@@ -49,15 +47,16 @@ module.exports = {
49 47
             }
50 48
         },
51 49
         validate: {
50
+            ...validators,
52 51
             failAction: 'log'
53 52
         },
54 53
 
55 54
         response: {
56 55
             status: {
57 56
                 200: apiSchema.single
58
-                // .append({
59
-                //   data: responseSchemas.api,
60
-                // })
57
+                    .append({
58
+                        data: responseSchemas.health,
59
+                    })
61 60
                     .label('api_single_res'),
62 61
                 409: apiSchema.single
63 62
                     .append({

+ 9
- 0
backend/lib/schemas/health.js Visa fil

@@ -0,0 +1,9 @@
1
+'use strict'
2
+
3
+const Joi = require('Joi')
4
+
5
+const stats = Joi.object({
6
+    date: Joi.string().required(),
7
+}).label('stats')
8
+
9
+module.exports = { stats }

+ 3
- 2
backend/lib/services/health.js Visa fil

@@ -5,7 +5,8 @@ module.exports = class HealthService extends Schmervice.Service {
5 5
         super(...args)
6 6
     }
7 7
 
8
-    async getStats(){
9
-    // todo: define and return server stats object
8
+    getStats(){
9
+        const date = new Date()
10
+        return { date: date.toString() }
10 11
     }
11 12
 }

Laddar…
Avbryt
Spara