orator 4.0.3 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +1 -0
- package/debug/Harness.js +3 -1
- package/debug/package.json +14 -0
- package/debug/site/Test.html +1 -1
- package/package.json +9 -7
- package/source/Orator-ServiceServer-IPC-SynthesizedResponse.js +11 -0
- package/source/Orator-ServiceServer-IPC.js +65 -1
- package/source/Orator.js +193 -4
package/.vscode/launch.json
CHANGED
package/debug/Harness.js
CHANGED
|
@@ -41,4 +41,6 @@ tmpServiceServer.invoke('GET', tmpURI, null,
|
|
|
41
41
|
(pError, pResponseData) =>
|
|
42
42
|
{
|
|
43
43
|
tmpServiceServer.log.info(`Response to [${tmpURI}] came back from IPC resulting in [${pResponseData}]!`)
|
|
44
|
-
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
tmpServiceServer.addStaticRoute('site', 'Test.html', '/*', '/');
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "orator-debug-server",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "Harness.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
8
|
+
},
|
|
9
|
+
"author": "",
|
|
10
|
+
"license": "ISC",
|
|
11
|
+
"dependencies": {
|
|
12
|
+
"orator-serviceserver-restify": "^2.0.3"
|
|
13
|
+
}
|
|
14
|
+
}
|
package/debug/site/Test.html
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
<html><head></head><body>
|
|
1
|
+
<html><head></head><body>Umm</body></html>
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "orator",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Unopinionated API
|
|
3
|
+
"version": "5.0.1",
|
|
4
|
+
"description": "Unopinionated API http server abstraction - REST or IPC",
|
|
5
5
|
"main": "source/Orator.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"start": "node source/Orator.js",
|
|
@@ -51,12 +51,14 @@
|
|
|
51
51
|
},
|
|
52
52
|
"homepage": "https://github.com/stevenvelozo/orator",
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"fable": "^3.0.
|
|
55
|
-
"quackage": "^1.0.
|
|
54
|
+
"fable": "^3.0.147",
|
|
55
|
+
"quackage": "^1.0.36"
|
|
56
56
|
},
|
|
57
57
|
"dependencies": {
|
|
58
|
-
"fable-serviceproviderbase": "^3.0.
|
|
59
|
-
"
|
|
60
|
-
"
|
|
58
|
+
"fable-serviceproviderbase": "^3.0.15",
|
|
59
|
+
"finalhandler": "^1.3.1",
|
|
60
|
+
"find-my-way": "^9.1.0",
|
|
61
|
+
"orator-serviceserver-base": "^1.0.1",
|
|
62
|
+
"serve-static": "^1.16.2"
|
|
61
63
|
}
|
|
62
64
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a synthesized server response for the Orator service server IPC.
|
|
3
|
+
*
|
|
4
|
+
* @class
|
|
5
|
+
*/
|
|
1
6
|
class OratorServiceServerIPCSynthesizedResponse
|
|
2
7
|
{
|
|
3
8
|
constructor(pHandler, pLog, pRequestGUID)
|
|
@@ -27,6 +32,12 @@ class OratorServiceServerIPCSynthesizedResponse
|
|
|
27
32
|
this.responseStatus = -1;
|
|
28
33
|
}
|
|
29
34
|
|
|
35
|
+
/**
|
|
36
|
+
* Sends data to the server.
|
|
37
|
+
*
|
|
38
|
+
* @param {string|object} pData - The data to be sent. It can be either a string or an object.
|
|
39
|
+
* @returns {boolean} - Returns true if the data was successfully sent, false otherwise.
|
|
40
|
+
*/
|
|
30
41
|
send(pData)
|
|
31
42
|
{
|
|
32
43
|
if (typeof(pData) == 'string')
|
|
@@ -6,6 +6,13 @@ const libOratorServiceServerIPCSynthesizedResponse = require('./Orator-ServiceSe
|
|
|
6
6
|
// This library is the default router for our services
|
|
7
7
|
const libFindMyWay = require('find-my-way');
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* OratorServiceServerIPC class.
|
|
11
|
+
*
|
|
12
|
+
* @class
|
|
13
|
+
* @extends libOratorServiceServerBase
|
|
14
|
+
* @classdesc Represents an IPC service server for Orator.
|
|
15
|
+
*/
|
|
9
16
|
class OratorServiceServerIPC extends libOratorServiceServerBase
|
|
10
17
|
{
|
|
11
18
|
constructor(pFable, pOptions, pServiceHash)
|
|
@@ -183,36 +190,85 @@ class OratorServiceServerIPC extends libOratorServiceServerBase
|
|
|
183
190
|
};
|
|
184
191
|
}
|
|
185
192
|
|
|
193
|
+
/**
|
|
194
|
+
* Handles the HTTP GET request for a specific route.
|
|
195
|
+
*
|
|
196
|
+
* @param {string} pRoute - The route to handle.
|
|
197
|
+
* @param {...Function} fRouteProcessingFunctions - The processing functions to execute for the route.
|
|
198
|
+
* @returns {Promise} A promise that resolves when the route processing is complete.
|
|
199
|
+
*/
|
|
186
200
|
doGet(pRoute, ...fRouteProcessingFunctions)
|
|
187
201
|
{
|
|
188
202
|
return this.addRouteProcessor('GET', pRoute, Array.from(fRouteProcessingFunctions));
|
|
189
203
|
}
|
|
190
204
|
|
|
205
|
+
/**
|
|
206
|
+
* Handles the PUT request for a specific route.
|
|
207
|
+
*
|
|
208
|
+
* @param {string} pRoute - The route to handle.
|
|
209
|
+
* @param {...Function} fRouteProcessingFunctions - The processing functions to execute for the route.
|
|
210
|
+
* @returns {Promise} - A promise that resolves when the route processing is complete.
|
|
211
|
+
*/
|
|
191
212
|
doPut(pRoute, ...fRouteProcessingFunctions)
|
|
192
213
|
{
|
|
193
214
|
return this.addRouteProcessor('PUT', pRoute, Array.from(fRouteProcessingFunctions));
|
|
194
215
|
}
|
|
195
216
|
|
|
217
|
+
/**
|
|
218
|
+
* Handles the HTTP POST request for a specific route.
|
|
219
|
+
*
|
|
220
|
+
* @param {string} pRoute - The route to handle.
|
|
221
|
+
* @param {...Function} fRouteProcessingFunctions - The processing functions to execute for the route.
|
|
222
|
+
* @returns {Promise} - A promise that resolves when the route processing is complete.
|
|
223
|
+
*/
|
|
196
224
|
doPost(pRoute, ...fRouteProcessingFunctions)
|
|
197
225
|
{
|
|
198
226
|
return this.addRouteProcessor('POST', pRoute, Array.from(fRouteProcessingFunctions));
|
|
199
227
|
}
|
|
200
228
|
|
|
229
|
+
/**
|
|
230
|
+
* Handles the HTTP DEL request for a specific route.
|
|
231
|
+
*
|
|
232
|
+
* @param {string} pRoute - The route to be deleted.
|
|
233
|
+
* @param {...Function} fRouteProcessingFunctions - The route processing functions to be added.
|
|
234
|
+
* @returns {Object} - The updated route processor object.
|
|
235
|
+
*/
|
|
201
236
|
doDel(pRoute, ...fRouteProcessingFunctions)
|
|
202
237
|
{
|
|
203
238
|
return this.addRouteProcessor('DELETE', pRoute, Array.from(fRouteProcessingFunctions));
|
|
204
239
|
}
|
|
205
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Adds a PATCH route processor to the service server.
|
|
243
|
+
*
|
|
244
|
+
* @param {string} pRoute - The route to be processed.
|
|
245
|
+
* @param {...Function} fRouteProcessingFunctions - The route processing functions.
|
|
246
|
+
* @returns {boolean} - Returns true if the route processor was added successfully, false otherwise.
|
|
247
|
+
*/
|
|
206
248
|
doPatch(pRoute, ...fRouteProcessingFunctions)
|
|
207
249
|
{
|
|
208
250
|
return this.addRouteProcessor('PATCH', pRoute, Array.from(fRouteProcessingFunctions));
|
|
209
251
|
}
|
|
210
252
|
|
|
253
|
+
/**
|
|
254
|
+
* Adds a route processor for the OPTIONS method.
|
|
255
|
+
*
|
|
256
|
+
* @param {string} pRoute - The route to add the processor to.
|
|
257
|
+
* @param {...Function} fRouteProcessingFunctions - The processing functions to be executed for the route.
|
|
258
|
+
* @returns {Object} - The updated Orator-ServiceServer-IPC object.
|
|
259
|
+
*/
|
|
211
260
|
doOpts(pRoute, ...fRouteProcessingFunctions)
|
|
212
261
|
{
|
|
213
262
|
return this.addRouteProcessor('OPTIONS', pRoute, Array.from(fRouteProcessingFunctions));
|
|
214
263
|
}
|
|
215
264
|
|
|
265
|
+
/**
|
|
266
|
+
* Handles the HEAD request for a specific route.
|
|
267
|
+
*
|
|
268
|
+
* @param {string} pRoute - The route to handle.
|
|
269
|
+
* @param {...Function} fRouteProcessingFunctions - The processing functions to execute for the route.
|
|
270
|
+
* @returns {Promise} - A promise that resolves when the route processing is complete.
|
|
271
|
+
*/
|
|
216
272
|
doHead(pRoute, ...fRouteProcessingFunctions)
|
|
217
273
|
{
|
|
218
274
|
return this.addRouteProcessor('HEAD', pRoute, Array.from(fRouteProcessingFunctions));
|
|
@@ -221,7 +277,15 @@ class OratorServiceServerIPC extends libOratorServiceServerBase
|
|
|
221
277
|
* End of Service Route Creation Functions
|
|
222
278
|
*/
|
|
223
279
|
|
|
224
|
-
|
|
280
|
+
/**
|
|
281
|
+
* Invokes a method on the IPC provider.
|
|
282
|
+
*
|
|
283
|
+
* @param {string} pMethod - The method to invoke.
|
|
284
|
+
* @param {string} pRoute - The route to invoke.
|
|
285
|
+
* @param {any} pData - The data to pass to the method.
|
|
286
|
+
* @param {Function} fCallback - The callback function to handle the response.
|
|
287
|
+
* @throws {Error} Throws an error if invoked without a callback function.
|
|
288
|
+
*/
|
|
225
289
|
invoke(pMethod, pRoute, pData, fCallback)
|
|
226
290
|
{
|
|
227
291
|
// If the data is skipped and a callback is parameter 3, do the right thing
|
package/source/Orator.js
CHANGED
|
@@ -11,8 +11,22 @@ const libFableServiceProviderBase = require('fable-serviceproviderbase');
|
|
|
11
11
|
|
|
12
12
|
const libDefaultOratorServiceServer = require('./Orator-Default-ServiceServer.js');
|
|
13
13
|
|
|
14
|
+
const libServeStatic = require('serve-static');
|
|
15
|
+
const libFinalHandler = require('finalhandler');
|
|
16
|
+
const libMime = require('mime');
|
|
17
|
+
|
|
14
18
|
const defaultOratorConfiguration = require('./Orator-Default-Configuration.js');
|
|
15
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @class Orator
|
|
22
|
+
* @extends libFableServiceProviderBase
|
|
23
|
+
*
|
|
24
|
+
* Represents the Orator service provider.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} pFable - The Fable instance.
|
|
27
|
+
* @param {Object} pOptions - The options for the Orator service.
|
|
28
|
+
* @param {string} pServiceHash - The hash of the service.
|
|
29
|
+
*/
|
|
16
30
|
class Orator extends libFableServiceProviderBase
|
|
17
31
|
{
|
|
18
32
|
constructor(pFable, pOptions, pServiceHash)
|
|
@@ -45,8 +59,20 @@ class Orator extends libFableServiceProviderBase
|
|
|
45
59
|
{
|
|
46
60
|
this.options.Product = defaultOratorConfiguration.Product;
|
|
47
61
|
}
|
|
62
|
+
|
|
63
|
+
// This is here because libMime has a breaking change from v1 to v2 and the lookup function was update to be getType per https://stackoverflow.com/a/60741078
|
|
64
|
+
// We don't want to introspect properties on this library every single time we need to check mime types.
|
|
65
|
+
// Therefore we are setting this boolean here and using it to branch.
|
|
66
|
+
this.oldLibMime = false;
|
|
67
|
+
if ('lookup' in libMime)
|
|
68
|
+
{
|
|
69
|
+
this.oldLibMime = true;
|
|
70
|
+
}
|
|
48
71
|
}
|
|
49
72
|
|
|
73
|
+
/**
|
|
74
|
+
* Lifecycle event that executes before initializing the service. For overloading.
|
|
75
|
+
*/
|
|
50
76
|
onBeforeInitialize()
|
|
51
77
|
{
|
|
52
78
|
if (this.fable.settings.LogNoisiness > 3)
|
|
@@ -54,6 +80,11 @@ class Orator extends libFableServiceProviderBase
|
|
|
54
80
|
this.log.trace(`Orator [${this.UUID}]::[${this.Hash}] ${this.options.Product} onBeforeInitialize:`);
|
|
55
81
|
}
|
|
56
82
|
}
|
|
83
|
+
/**
|
|
84
|
+
* Lifecycle event that executes before initializing the service. For overloading.
|
|
85
|
+
*
|
|
86
|
+
* @param {Function} fNext - The callback function to be called after the actions are executed.
|
|
87
|
+
*/
|
|
57
88
|
onBeforeInitializeAsync(fNext)
|
|
58
89
|
{
|
|
59
90
|
this.onBeforeInitialize();
|
|
@@ -92,12 +123,20 @@ class Orator extends libFableServiceProviderBase
|
|
|
92
123
|
this.log.trace(`Orator [${this.UUID}]::[${this.Hash}] ${this.options.Product} onInitialize:`);
|
|
93
124
|
}
|
|
94
125
|
}
|
|
126
|
+
/**
|
|
127
|
+
* Lifecycle event that executes at the moment of initializing the service. For overloading.
|
|
128
|
+
*
|
|
129
|
+
* @param {Function} fNext - The callback function to be executed after initialization.
|
|
130
|
+
*/
|
|
95
131
|
onInitializeAsync(fNext)
|
|
96
132
|
{
|
|
97
133
|
this.onInitialize();
|
|
98
134
|
return fNext();
|
|
99
135
|
}
|
|
100
136
|
|
|
137
|
+
/**
|
|
138
|
+
* Lifecycle event that executes after initializing the service. For overloading.
|
|
139
|
+
*/
|
|
101
140
|
onAfterInitialize()
|
|
102
141
|
{
|
|
103
142
|
if (this.fable.settings.LogNoisiness > 3)
|
|
@@ -105,12 +144,22 @@ class Orator extends libFableServiceProviderBase
|
|
|
105
144
|
this.log.trace(`Orator [${this.UUID}]::[${this.Hash}] ${this.options.Product} onAfterInitialize:`);
|
|
106
145
|
}
|
|
107
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Lifecycle event that executes after initializing the service. For overloading.
|
|
149
|
+
* @param {Function} fNext - The callback function to be called after executing onAfterInitialize.
|
|
150
|
+
* @returns {Promise} - A promise that resolves when the callback function is called.
|
|
151
|
+
*/
|
|
108
152
|
onAfterInitializeAsync(fNext)
|
|
109
153
|
{
|
|
110
154
|
this.onAfterInitialize();
|
|
111
155
|
return fNext();
|
|
112
156
|
}
|
|
113
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Initializes the Orator instance.
|
|
160
|
+
*
|
|
161
|
+
* @param {Function} fCallback - The callback function to be executed after initialization.
|
|
162
|
+
*/
|
|
114
163
|
initialize(fCallback)
|
|
115
164
|
{
|
|
116
165
|
// I hate this -- as long as we want to be "mostly" backwards compatible it needs to do it though
|
|
@@ -148,10 +197,22 @@ class Orator extends libFableServiceProviderBase
|
|
|
148
197
|
}
|
|
149
198
|
}
|
|
150
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Lifecycle event that executes before starting the service. For overloading.
|
|
202
|
+
*
|
|
203
|
+
* @param {Function} fNext - The function to be executed before starting the service.
|
|
204
|
+
* @returns {Promise} A promise that resolves when the function is completed.
|
|
205
|
+
*/
|
|
151
206
|
onBeforeStartService(fNext)
|
|
152
207
|
{
|
|
153
208
|
return fNext();
|
|
154
209
|
}
|
|
210
|
+
/**
|
|
211
|
+
* Lifecycle event that executes when the service starts. For overloading.
|
|
212
|
+
*
|
|
213
|
+
* @param {Function} fNext - The callback function to be called after the service starts.
|
|
214
|
+
* @returns {Promise} A promise that resolves when the service starts successfully, or rejects with an error.
|
|
215
|
+
*/
|
|
155
216
|
onStartService(fNext)
|
|
156
217
|
{
|
|
157
218
|
this.onAfterInitialize();
|
|
@@ -165,11 +226,22 @@ class Orator extends libFableServiceProviderBase
|
|
|
165
226
|
}
|
|
166
227
|
);
|
|
167
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Lifecycle event that executes after starting the service. For overloading.
|
|
231
|
+
*
|
|
232
|
+
* @param {Function} fNext - The callback function to be executed after the service starts.
|
|
233
|
+
* @returns {Promise} - A promise that resolves when the callback function is completed.
|
|
234
|
+
*/
|
|
168
235
|
onAfterStartService(fNext)
|
|
169
236
|
{
|
|
170
237
|
return fNext();
|
|
171
238
|
}
|
|
172
239
|
|
|
240
|
+
/**
|
|
241
|
+
* Starts the service.
|
|
242
|
+
*
|
|
243
|
+
* @param {Function} fNext - The callback function to be executed after the service has started.
|
|
244
|
+
*/
|
|
173
245
|
startService(fNext)
|
|
174
246
|
{
|
|
175
247
|
var tmpNext = (typeof(fNext) === 'function') ? fNext : ()=>{};
|
|
@@ -203,6 +275,12 @@ class Orator extends libFableServiceProviderBase
|
|
|
203
275
|
});
|
|
204
276
|
}
|
|
205
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Stops the service server.
|
|
280
|
+
*
|
|
281
|
+
* @param {Function} fCallback - The callback function to be executed after the service server is stopped.
|
|
282
|
+
* @returns {void}
|
|
283
|
+
*/
|
|
206
284
|
stopService(fCallback)
|
|
207
285
|
{
|
|
208
286
|
var tmpCallback = (typeof(fCallback) === 'function') ? fCallback : ()=>{};
|
|
@@ -224,28 +302,51 @@ class Orator extends libFableServiceProviderBase
|
|
|
224
302
|
return this.serviceServer.close(tmpCallback);
|
|
225
303
|
}
|
|
226
304
|
|
|
305
|
+
/**
|
|
306
|
+
* Programmatically invokes a method on the service server.
|
|
307
|
+
*
|
|
308
|
+
* @param {string} pMethod - The method to invoke.
|
|
309
|
+
* @param {string} pRoute - The route to invoke.
|
|
310
|
+
* @param {any} pData - The data to send with the invocation.
|
|
311
|
+
* @param {Function} fCallback - The callback function to execute after the invocation.
|
|
312
|
+
* @returns {any} - The result of the invocation.
|
|
313
|
+
*/
|
|
227
314
|
invoke(pMethod, pRoute, pData, fCallback)
|
|
228
315
|
{
|
|
229
316
|
//this.log.trace(`Orator [${this.UUID}]::[${this.Hash}] ${this.options.Product} invoking ${pMethod} ${pRoute}`);
|
|
230
317
|
return this.serviceServer.invoke(pMethod, pRoute, pData, fCallback);
|
|
231
318
|
}
|
|
232
319
|
|
|
233
|
-
|
|
234
320
|
/*
|
|
235
|
-
* Legacy Orator Functions
|
|
321
|
+
* Legacy Orator / Backwards Compatibility Functions
|
|
236
322
|
*************************************************************************/
|
|
323
|
+
/**
|
|
324
|
+
* Starts the web server.
|
|
325
|
+
*
|
|
326
|
+
* @param {Function} fNext - The callback function to be executed after the web server starts.
|
|
327
|
+
* @returns {Promise} A promise that resolves when the web server has started.
|
|
328
|
+
*/
|
|
237
329
|
startWebServer(fNext)
|
|
238
330
|
{
|
|
239
331
|
return this.startService(fNext);
|
|
240
332
|
}
|
|
241
333
|
|
|
242
|
-
|
|
334
|
+
/**
|
|
335
|
+
* Stops the web server.
|
|
336
|
+
*
|
|
337
|
+
* @param {Function} fNext - The callback function to be called after the web server is stopped.
|
|
338
|
+
* @returns {Promise} A promise that resolves when the web server is stopped.
|
|
339
|
+
*/
|
|
243
340
|
stopWebServer(fNext)
|
|
244
341
|
{
|
|
245
342
|
return this.stopService(fNext);
|
|
246
343
|
}
|
|
247
344
|
|
|
248
|
-
|
|
345
|
+
/**
|
|
346
|
+
* Retrieves the web server instance.
|
|
347
|
+
*
|
|
348
|
+
* @returns {WebServer} The web server instance.
|
|
349
|
+
*/
|
|
249
350
|
getWebServer()
|
|
250
351
|
{
|
|
251
352
|
// The old behavior was to lazily construct the service the first time
|
|
@@ -260,6 +361,94 @@ class Orator extends libFableServiceProviderBase
|
|
|
260
361
|
/*************************************************************************
|
|
261
362
|
* End of Legacy Orator Functions
|
|
262
363
|
*/
|
|
364
|
+
|
|
365
|
+
setMimeHeader(pFileName, pResponse)
|
|
366
|
+
{
|
|
367
|
+
let tmpHeader;
|
|
368
|
+
|
|
369
|
+
if (this.oldLibMime)
|
|
370
|
+
{
|
|
371
|
+
tmpHeader = libMime.lookup(pFileName);
|
|
372
|
+
}
|
|
373
|
+
else
|
|
374
|
+
{
|
|
375
|
+
tmpHeader = libMime.getType(pFileName);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (!tmpHeader)
|
|
379
|
+
{
|
|
380
|
+
tmpHeader = 'application/octet-stream';
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
pResponse.setHeader('Content-Type', tmpHeader);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Serve a static folder, optionally with magic subdomain masking.
|
|
388
|
+
*
|
|
389
|
+
* @param {string} pFilePath The path on disk that we are serving files from.
|
|
390
|
+
* @param {string?} pDefaultFile (optional) The default file served if no specific file is requested.
|
|
391
|
+
* @param {string?} pRoute (optional) The route matcher that will be used. Defaults to everything.
|
|
392
|
+
* @param {string?} pRouteStrip (optional) If provided, this prefix will be removed from URL paths before being served.
|
|
393
|
+
* @param {object?} pParams (optional) Additional parameters to pass to serve-static.
|
|
394
|
+
* @return {boolean} true if the handler was successfully installed, otherwise false.
|
|
395
|
+
*/
|
|
396
|
+
addStaticRoute(pFilePath, pDefaultFile, pRoute, pRouteStrip, pParams)
|
|
397
|
+
{
|
|
398
|
+
if (typeof(pFilePath) !== 'string')
|
|
399
|
+
{
|
|
400
|
+
this.fable.log.error('A file path must be passed in as part of the server.');
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// Default to just serving from root
|
|
405
|
+
const tmpRoute = (typeof(pRoute) === 'undefined') ? '/*' : pRoute;
|
|
406
|
+
const tmpRouteStrip = (typeof(pRouteStrip) === 'undefined') ? '/' : pRouteStrip;
|
|
407
|
+
|
|
408
|
+
// Default to serving index.html
|
|
409
|
+
const tmpDefaultFile = (typeof(pDefaultFile) === 'undefined') ? 'index.html' : pDefaultFile;
|
|
410
|
+
|
|
411
|
+
this.fable.log.info('Orator mapping static route to files: '+tmpRoute+' ==> '+pFilePath+' '+tmpDefaultFile);
|
|
412
|
+
|
|
413
|
+
// Add the route
|
|
414
|
+
this.serviceServer.get(tmpRoute,
|
|
415
|
+
(pRequest, pResponse, fNext) =>
|
|
416
|
+
{
|
|
417
|
+
// See if there is a magic subdomain put at the beginning of a request.
|
|
418
|
+
// If there is, then we need to see if there is a subfolder and add that to the file path
|
|
419
|
+
let tmpHostSet = pRequest.headers.host.split('.');
|
|
420
|
+
let tmpPotentialSubfolderMagicHost = false;
|
|
421
|
+
let servePath = pFilePath;
|
|
422
|
+
// Check if there are more than one host in the host header (this will be 127 a lot)
|
|
423
|
+
if (tmpHostSet.length > 1)
|
|
424
|
+
{
|
|
425
|
+
tmpPotentialSubfolderMagicHost = tmpHostSet[0];
|
|
426
|
+
}
|
|
427
|
+
if (tmpPotentialSubfolderMagicHost)
|
|
428
|
+
{
|
|
429
|
+
// Check if the subfolder exists -- this is only one dimensional for now
|
|
430
|
+
let tmpPotentialSubfolder = servePath + tmpPotentialSubfolderMagicHost;
|
|
431
|
+
if (this.fable.FilePersistence.libFS.existsSync(tmpPotentialSubfolder))
|
|
432
|
+
{
|
|
433
|
+
// If it does, then we need to add it to the file path
|
|
434
|
+
servePath = `${tmpPotentialSubfolder}/`;
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
pRequest.url = pRequest.url.split('?')[0].substr(tmpRouteStrip.length) || '/';
|
|
438
|
+
pRequest.path = function()
|
|
439
|
+
{
|
|
440
|
+
return pRequest.url;
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
this.setMimeHeader(pRequest.url, pResponse);
|
|
444
|
+
|
|
445
|
+
const tmpServe = libServeStatic(servePath, Object.assign({ index: tmpDefaultFile }, pParams));
|
|
446
|
+
tmpServe(pRequest, pResponse, libFinalHandler(pRequest, pResponse));
|
|
447
|
+
// TODO: This may break things if a post request send handler is setup...
|
|
448
|
+
//fNext();
|
|
449
|
+
});
|
|
450
|
+
return true;
|
|
451
|
+
}
|
|
263
452
|
}
|
|
264
453
|
|
|
265
454
|
module.exports = Orator;
|