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

:sparkles: building up architecture for backend

master
j 5 лет назад
Родитель
Сommit
d4fe25e626

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

1
-const profile = require('./profile')
2
-module.exports = {
3
-    profile
4
-};

+ 0
- 19
backend/api/profile.js Просмотреть файл

1
-// https://github.com/connor11528/task-app-backend/blob/master/src/api/index.js
2
-
3
-const profileApi = {
4
-  get: {
5
-    async handler(request, h) {
6
-      try {
7
-        console.log('get handler for profile');
8
-
9
-        // You have to return SOMETHING
10
-        return { thing: 'profile' }
11
-
12
-      } catch (err) {
13
-        //   Boom.badImplementation(err);
14
-      }
15
-    }
16
-  }
17
-};
18
-
19
-module.exports = profileApi;

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

1
-'use strict';
2
-
3
-const Hapi = require('@hapi/hapi');
4
-
5
-const init = async () => {
6
-
7
-    const server = Hapi.server({
8
-        port: 3001,
9
-        host: 'localhost'
10
-    });
11
-
12
-    await server.start();
13
-    console.log('Server running on %s', server.info.uri);
14
-
15
-    server.route({
16
-        method: 'GET',
17
-        path: '/',
18
-        handler: (request, h) => {
19
-
20
-            return 'Hello World!';
21
-        }
22
-    });
23
-};
24
-
25
-process.on('unhandledRejection', (err) => {
26
-    console.log(err);
27
-    process.exit(1);
28
-});
29
-
30
-init();

+ 1
- 0
backend/package-lock.json Просмотреть файл

577
       "dependencies": {
577
       "dependencies": {
578
         "anymatch": "~3.1.1",
578
         "anymatch": "~3.1.1",
579
         "braces": "~3.0.2",
579
         "braces": "~3.0.2",
580
+        "fsevents": "~2.3.1",
580
         "glob-parent": "~5.1.0",
581
         "glob-parent": "~5.1.0",
581
         "is-binary-path": "~2.1.0",
582
         "is-binary-path": "~2.1.0",
582
         "is-glob": "~4.0.1",
583
         "is-glob": "~4.0.1",

+ 2
- 1
backend/package.json Просмотреть файл

14
   },
14
   },
15
   "devDependencies": {
15
   "devDependencies": {
16
     "nodemon": "^2.0.7"
16
     "nodemon": "^2.0.7"
17
-  }
17
+  },
18
+  "type": "module"
18
 }
19
 }

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

1
+import { profile } from './profile.js'
2
+
3
+const handlers = {
4
+    default: {
5
+        get: (request, h) => {
6
+            return { 
7
+                ok: true, 
8
+                handler: 'default',
9
+                data: {}
10
+            }
11
+        }
12
+    },
13
+    profile
14
+}
15
+
16
+export { handlers }

+ 22
- 0
backend/src/handlers/profile.js Просмотреть файл

1
+// https://github.com/connor11528/task-app-backend/blob/master/src/api/index.js
2
+
3
+const profile = {
4
+    get: (request, h) => {
5
+        try {
6
+            console.log('get handler for profile');
7
+
8
+            // You have to return SOMETHING
9
+            return { 
10
+                ok: true,
11
+                handler: 'profile',
12
+                data: { name: 'Chuck Charles' },
13
+            }
14
+
15
+        } 
16
+        catch (err) {
17
+            //   Boom.badImplementation(err);
18
+        }
19
+    }
20
+}
21
+
22
+export { profile }

+ 12
- 11
backend/src/index.js Просмотреть файл

1
-'use strict';
1
+import Hapi from '@hapi/hapi'
2
 
2
 
3
-const api = require('../api')
4
-const routes = require('./routes')
5
-
6
-const Hapi = require('@hapi/hapi');
3
+import { routes } from './routes.js'
4
+import { plugins } from './plugins/index.js'
7
 
5
 
8
 const server = Hapi.server({
6
 const server = Hapi.server({
9
     port: 3001,
7
     port: 3001,
13
 
11
 
14
 const init = async () => {
12
 const init = async () => {
15
     try {
13
     try {
16
-        routes.forEach(route => {
17
-            server.route(route);
18
-        })
14
+        routes.forEach(route => server.route(route))
19
 
15
 
20
-        await server.start();
16
+        for(const plugin of plugins) {
17
+            await server.register(plugin, {
18
+                routes: { prefix: '/plugins' }
19
+            })
20
+        }
21
 
21
 
22
+        await server.start();
22
         console.log('Server running on %s', server.info.uri);
23
         console.log('Server running on %s', server.info.uri);
23
     }
24
     }
24
 
25
 
33
     process.exit(1);
34
     process.exit(1);
34
 });
35
 });
35
 
36
 
36
-init();
37
+init()
37
 
38
 
38
-module.exports = server;
39
+export default server

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

1
+import { profilePlugin } from './profile.js'
2
+import { testPlugin } from './test.js'
3
+
4
+const plugins = [ testPlugin, profilePlugin ]
5
+
6
+export { plugins }

+ 15
- 0
backend/src/plugins/profile.js Просмотреть файл

1
+import { handlers } from '../handlers/index.js'
2
+
3
+const profilePlugin = {
4
+    name: 'profile-plugin',
5
+    version: '1.0.0',
6
+    register: async (server, options) => {
7
+        server.route({
8
+            method: 'GET',
9
+            path: '/profile',
10
+            handler: handlers.profile.get
11
+        })
12
+    }
13
+}
14
+
15
+export { profilePlugin }

+ 15
- 0
backend/src/plugins/test.js Просмотреть файл

1
+import { handlers } from '../handlers/index.js'
2
+
3
+const testPlugin = {
4
+    name: 'test-plugin',
5
+    version: '1.0.0',
6
+    register: async (server, options) => {
7
+        server.route({
8
+            method: 'GET',
9
+            path: '/test',
10
+            handler: handlers.default.get
11
+        })
12
+    }
13
+}
14
+
15
+export { testPlugin }

+ 4
- 11
backend/src/routes.js Просмотреть файл

1
-const api = require('../api')
1
+import { handlers } from './handlers/index.js'
2
 
2
 
3
 const routes = [
3
 const routes = [
4
     {
4
     {
5
         method: 'GET',
5
         method: 'GET',
6
-        path: '/api',
7
-        handler: (request, h) => {
8
-            return { success: true }
9
-        }
10
-    },
11
-    {
12
-        method: 'GET',
13
-        path: '/api/profiles',
14
-        options: api.profile.get
6
+        path: '/',
7
+        handler: handlers.default.get
15
     },
8
     },
16
 ]
9
 ]
17
 
10
 
18
-module.exports = routes
11
+export { routes }

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