Przeglądaj źródła

Merge branch 'api-health-route' of fyindr/siimee into dev

tags/0.0.1^2
jcarbajal 3 lat temu
rodzic
commit
94b57f0e06

+ 2
- 1
backend/knexfile.js Wyświetl plik

@@ -1,4 +1,5 @@
1
-require('dotenv').config()
1
+require('dotenv').config({path: './server/.env'})
2
+// require('dotenv').config()
2 3
 
3 4
 const local = {
4 5
     host: process.env.DB_HOST,

+ 5
- 0
backend/lib/index.js Wyświetl plik

@@ -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
 }

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

@@ -0,0 +1,9 @@
1
+const HealthRoute = require('../routes/health/get')
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
+}

+ 2
- 0
backend/lib/plugins/user.js Wyświetl plik

@@ -14,6 +14,7 @@ const UserSignupRoute = require('../routes/user/signup')
14 14
 
15 15
 const UserService = require('../services/user')
16 16
 const DisplayService = require('../services/display')
17
+const HealthService = require('../services/health')
17 18
 
18 19
 module.exports = {
19 20
     name: 'user-plugin',
@@ -38,6 +39,7 @@ module.exports = {
38 39
         await server.register(Schmervice)
39 40
         server.registerService(UserService)
40 41
         server.registerService(DisplayService)
42
+        server.registerService(HealthService)
41 43
 
42 44
         await server.route(UserCurrentRoute)
43 45
         await server.route(UserLoginRoute)

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

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

+ 10
- 0
backend/lib/schemas/health.js Wyświetl plik

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

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

@@ -0,0 +1,18 @@
1
+const Schmervice = require('@hapipal/schmervice')
2
+
3
+module.exports = class HealthService extends Schmervice.Service {
4
+    constructor(...args){
5
+        super(...args)
6
+    }
7
+
8
+    /**
9
+     * Returns date and number of users
10
+     * @returns {object}
11
+     */
12
+    async getStats(){
13
+        const { User } = this.server.models()
14
+        const users = await User.query()
15
+        const date = new Date()
16
+        return { date: date.toString(), users: users.length }
17
+    }
18
+}

+ 14507
- 6241
backend/package-lock.json
Plik diff jest za duży
Wyświetl plik


+ 1
- 3
backend/server/manifest.js Wyświetl plik

@@ -1,12 +1,10 @@
1
-const Dotenv = require('dotenv')
1
+const Dotenv = require('dotenv').config({path: './server/.env'})
2 2
 const Confidence = require('@hapipal/confidence')
3 3
 const Inert = require('@hapi/inert')
4 4
 const Vision = require('@hapi/vision')
5 5
 const Schwifty = require('@hapipal/schwifty')
6 6
 const HapiSwagger = require('hapi-swagger')
7 7
 
8
-/** Pull .env into process.env */
9
-Dotenv.config({ path: `${__dirname}/../.env` })
10 8
 
11 9
 /** Glue manifest as a confidence store */
12 10
 module.exports = new Confidence.Store({

+ 7742
- 862
frontend/package-lock.json
Plik diff jest za duży
Wyświetl plik


Ładowanie…
Anuluj
Zapisz