-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·130 lines (118 loc) · 3.42 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
* App
*
* Initialises a Sapling instance and handles incoming requests
*/
/* System dependencies */
import async from 'async';
/* Internal dependencies */
import { console } from './lib/Cluster.js';
import Response from './lib/Response.js';
import Utils from './lib/Utils.js';
import parseMethodRouteKey from './core/parseMethodRouteKey.js';
import runHook from './core/runHook.js';
/**
* The App class
*/
class App {
/**
* Load and construct all aspects of the app
*
* @param {string} dir Directory for the site files
* @param {object} opts Optional options to override the defaults and filesystem ones
* @param {function} next Callback after initialisation
*/
constructor(dir, options, next) {
/* Global vars */
this.dir = dir;
options = options || {};
this.opts = options;
/* Define an admin session for big ops */
this.adminSession = {
user: { role: 'admin' },
};
/* Load utility functions */
this.utils = new Utils(this);
/* Load everything */
async.series([
async callback => {
const { default: loadConfig } = await import('./core/loadConfig.js');
await loadConfig.call(this, callback);
},
async callback => {
if (options.loadServer !== false) {
const { default: loadServer } = await import('./core/loadServer.js');
await loadServer.call(this, options, callback);
}
},
async callback => {
if (options.loadModel !== false) {
const { default: loadModel } = await import('./core/loadModel.js');
await loadModel.call(this, callback);
}
},
async callback => {
if (options.loadPermissions !== false) {
const { default: loadPermissions } = await import('./core/loadPermissions.js');
await loadPermissions.call(this, callback);
}
},
async callback => {
if (options.loadController !== false) {
const { default: loadController } = await import('./core/loadController.js');
await loadController.call(this, callback);
}
},
async callback => {
if (options.loadHooks !== false) {
const { default: loadHooks } = await import('./core/loadHooks.js');
await loadHooks.call(this, callback);
}
},
async callback => {
if (options.loadViews !== false) {
const { default: loadCustomTags } = await import('./core/loadCustomTags.js');
await loadCustomTags.call(this, callback);
}
},
async callback => {
const { default: loadModules } = await import('./core/loadModules.js');
await loadModules.call(this, callback);
},
async callback => {
if (options.loadViews !== false) {
for (const route in this.controller) {
if (Object.prototype.hasOwnProperty.call(this.controller, route)) {
const { default: initRoute } = await import('./core/initRoute.js');
await initRoute.call(this, route, this.controller[route]);
}
}
}
if (options.loadREST !== false) {
const { default: loadRest } = await import('./core/loadRest.js');
await loadRest.call(this, callback);
}
},
callback => {
this.server.use((request, response) => {
new Response(this, request, response, null, false);
});
callback();
},
], error => {
if (error) {
console.error('Error starting Sapling');
console.error(error);
console.error(error.stack);
return false;
}
if (next) {
next();
}
});
}
/* Load remaining methods */
parseMethodRouteKey = parseMethodRouteKey;
runHook = runHook;
}
export default App;