1 | /*
|
2 |
|
3 | ----------------------------------------------------------------------------
|
4 | | qewd: Quick and Easy Web Development |
|
5 | | |
|
6 | | Copyright (c) 2017-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 | 8 October 2019
|
28 |
|
29 | */
|
30 |
|
31 | var sessions = require('ewd-session');
|
32 | var build = require('./build');
|
33 | var resilientMode = require('./resilientMode');
|
34 | var qewdSessionByJWT = require('./qewdSessionByJWT');
|
35 |
|
36 | module.exports = function() {
|
37 |
|
38 | this.xpress = {
|
39 | build: build
|
40 | };
|
41 |
|
42 | this.on('DocumentStoreStarted', function() {
|
43 | sessions.init(this.documentStore, this.userDefined.config.sessionDocumentName);
|
44 | this.sessions = sessions;
|
45 | this.jwt = this.userDefined.config.jwt;
|
46 | sessions.garbageCollector(this, 60);
|
47 | this.handlers = {};
|
48 | this.beforeHandlers = {};
|
49 | this.afterHandlers = {};
|
50 | this.servicesAllowed = {};
|
51 | this.qewdSessionByJWT = qewdSessionByJWT;
|
52 | if (this.userDefined.config.resilientMode) {
|
53 | resilientMode.garbageCollector.call(this);
|
54 | }
|
55 | var q = this;
|
56 | this.db.use = function(documentName, ...subscripts) {
|
57 | if (subscripts.length === 1 && Array.isArray(subscripts[0])) subscripts = subscripts[0];
|
58 | return new q.documentStore.DocumentNode(documentName, subscripts);
|
59 | };
|
60 | });
|
61 |
|
62 | this.on('start', function(isFirst) {
|
63 |
|
64 | // load up dynamic app handler mechanism
|
65 |
|
66 | require('./appHandler').call(this);
|
67 |
|
68 | // connect Cache or GT.M
|
69 |
|
70 | var db = this.userDefined.config.database;
|
71 | if (db) {
|
72 | var type = db.type;
|
73 | if (type === 'cache' || type === 'gtm' || type === 'redis' || type === 'iris' || type === 'dbx') {
|
74 | require('ewd-qoper8-' + type)(this, db.params);
|
75 | }
|
76 | }
|
77 | this.isFirst = isFirst;
|
78 | });
|
79 |
|
80 | this.setCustomErrorResponse = function(params) {
|
81 | var application = params.application;
|
82 | if (!application || application === '') return false;
|
83 | var type = params.errorType;
|
84 | if (!type || type === '') return false;
|
85 | var text = params.text || 'Unspecified Error';
|
86 | var statusCode = params.statusCode || '400';
|
87 | if (!this.errorMessages) this.errorMessages = {};
|
88 | if (!this.errorMessages[application]) this.errorMessages[application] = {};
|
89 | this.errorMessages[application][type] = {
|
90 | text: text,
|
91 | statusCode: statusCode
|
92 | };
|
93 | };
|
94 |
|
95 | };
|