Просмотр исходного кода

working to display date on endpoint

tags/0.0.1^2
juancarbajal98 3 лет назад
Родитель
Сommit
c9ef5ecab9

+ 5
- 0
backend/lib/index.js Просмотреть файл

3
 const SurveyPlugin = require('./plugins/survey')
3
 const SurveyPlugin = require('./plugins/survey')
4
 const ProfilePlugin = require('./plugins/profile')
4
 const ProfilePlugin = require('./plugins/profile')
5
 const NotificationPlugin = require('./plugins/notification')
5
 const NotificationPlugin = require('./plugins/notification')
6
+const HealthPlugin = require('./plugins/health')
6
 
7
 
7
 /**
8
 /**
8
  * A Hapi server instance
9
  * A Hapi server instance
45
         await server.register(NotificationPlugin, {
46
         await server.register(NotificationPlugin, {
46
             routes: { prefix: '/notification' },
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 Просмотреть файл

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

+ 14
- 15
backend/lib/routes/health/get.js Просмотреть файл

2
 
2
 
3
 const apiSchema = require('../../schemas/api')
3
 const apiSchema = require('../../schemas/api')
4
 const errorSchema = require('../../schemas/errors')
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
 const pluginConfig = {
7
 const pluginConfig = {
8
     handlerType: 'health',
8
     handlerType: 'health',
12
     }
12
     }
13
 }
13
 }
14
 
14
 
15
+const validators = {}
16
+
15
 const responseSchemas = {
17
 const responseSchemas = {
16
-    api: apiSchema.single,
18
+    health: healthSchema.stats,
17
     error: errorSchema.single
19
     error: errorSchema.single
18
 }
20
 }
19
 
21
 
27
         cors: true,
29
         cors: true,
28
         handler: async function (request, h) {
30
         handler: async function (request, h) {
29
             const { healthService } = request.server.services()
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
             try {
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
             } catch (err) {
39
             } catch (err) {
42
                 return h
40
                 return h
43
                     .response({
41
                     .response({
49
             }
47
             }
50
         },
48
         },
51
         validate: {
49
         validate: {
50
+            ...validators,
52
             failAction: 'log'
51
             failAction: 'log'
53
         },
52
         },
54
 
53
 
55
         response: {
54
         response: {
56
             status: {
55
             status: {
57
                 200: apiSchema.single
56
                 200: apiSchema.single
58
-                // .append({
59
-                //   data: responseSchemas.api,
60
-                // })
57
+                    .append({
58
+                        data: responseSchemas.health,
59
+                    })
61
                     .label('api_single_res'),
60
                     .label('api_single_res'),
62
                 409: apiSchema.single
61
                 409: apiSchema.single
63
                     .append({
62
                     .append({

+ 9
- 0
backend/lib/schemas/health.js Просмотреть файл

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 Просмотреть файл

5
         super(...args)
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
 }

Загрузка…
Отмена
Сохранить