1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd-up: Rapid QEWD API Development |
|
5 | | |
|
6 | | Copyright (c) 2018-19 M/Gateway Developments Ltd, |
|
7 | | Redhill, Surrey UK. |
|
8 | | All rights reserved. |
|
9 | | |
|
10 | | http://www.mgateway.com |
|
11 | | Email: rtweed@mgateway.com |
|
12 | | |
|
13 | | |
|
14 | | Licensed under the Apache License, Version 2.0 (the "License"); |
|
15 | | you may not use this file except in compliance with the License. |
|
16 | | You may obtain a copy of the License at |
|
17 | | |
|
18 | | http://www.apache.org/licenses/LICENSE-2.0 |
|
19 | | |
|
20 | | Unless required by applicable law or agreed to in writing, software |
|
21 | | distributed under the License is distributed on an "AS IS" BASIS, |
|
22 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
23 | | See the License for the specific language governing permissions and |
|
24 | | limitations under the License. |
|
25 | ----------------------------------------------------------------------------
|
26 |
|
27 | 14 March 2019
|
28 |
|
29 | */
|
30 |
|
31 | var Route = require('route-parser');
|
32 |
|
33 | module.exports = function(docStoreEventsPath, rootPath) {
|
34 |
|
35 | var docStoreEvents;
|
36 |
|
37 | try {
|
38 | docStoreEvents = require(docStoreEventsPath).documents;
|
39 | console.log('** Loaded docStoreEvents definitions from ' + docStoreEventsPath);
|
40 | //console.log('docStoreEvents: ' + JSON.stringify(docStoreEvents, null, 2));
|
41 | }
|
42 | catch(err) {
|
43 | console.log('** Warning - unable to load docStoreEvents definitions from ' + docStoreEventsPath);
|
44 | console.log(err);
|
45 | return;
|
46 | }
|
47 |
|
48 | //var docStoreEvents = {};
|
49 |
|
50 | var path;
|
51 | var documentName;
|
52 | var indexObj;
|
53 | var handlerPath;
|
54 | var handler;
|
55 | var indexRoute;
|
56 | var obj;
|
57 |
|
58 | for (documentName in docStoreEvents) {
|
59 | docStoreEvents[documentName].indices = {};
|
60 | for (path in docStoreEvents[documentName].pathsToIndex) {
|
61 | indexObj = docStoreEvents[documentName].pathsToIndex[path];
|
62 | handler = undefined;
|
63 | if (indexObj.handler) {
|
64 | handlerPath = rootPath + 'docStoreEvents/' + indexObj.handler;
|
65 | try {
|
66 | handler = require(handlerPath);
|
67 | console.log('** Event handler successfully loaded from ' + handlerPath);
|
68 | }
|
69 | catch(err) {
|
70 | console.log('** Warning - unable to load ' + handlerPath);
|
71 | }
|
72 | }
|
73 | if (indexObj.path) {
|
74 | indexRoute = new Route(indexObj.path);
|
75 | }
|
76 | else {
|
77 | indexRoute = undefined;
|
78 | }
|
79 | obj = {
|
80 | documentName: indexObj.documentName || documentName,
|
81 | targetRoute: new Route(path),
|
82 | handler: handler,
|
83 | indexRoute: indexRoute,
|
84 | apply: indexObj.apply,
|
85 | value: indexObj.value
|
86 | };
|
87 | docStoreEvents[documentName].indices[path] = obj;
|
88 | }
|
89 | }
|
90 |
|
91 | return docStoreEvents;
|
92 | };
|
93 |
|
94 |
|
95 |
|