orator-http-proxy 1.0.0 → 1.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/README.md
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
# Orator
|
|
1
|
+
# Orator Static File Server
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
of servers, /1.1/ to another and /1.0/CustomApi* to a third.
|
|
3
|
+
Very basic static file server for local development and lightweight tasks.
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
web servers, not an industrial-grade API routing service.
|
|
5
|
+
Leverages restify static server.
|
package/debug/Harness.js
CHANGED
|
@@ -4,7 +4,9 @@ const defaultFableSettings = (
|
|
|
4
4
|
{
|
|
5
5
|
Product:'Orator-Proxy',
|
|
6
6
|
ProductVersion: '1.0.0',
|
|
7
|
-
APIServerPort: 8765
|
|
7
|
+
APIServerPort: 8765,
|
|
8
|
+
|
|
9
|
+
OratorHTTPProxyDestinationURL: 'http://127.0.0.1:8086/'
|
|
8
10
|
});
|
|
9
11
|
|
|
10
12
|
// Initialize Fable
|
|
@@ -33,7 +35,7 @@ tmpAnticipate.anticipate(
|
|
|
33
35
|
// Create an endpoint. This can also be done after the service is started.
|
|
34
36
|
_Orator.serviceServer.get
|
|
35
37
|
(
|
|
36
|
-
'/test/:hash',
|
|
38
|
+
'/1.0/test/:hash',
|
|
37
39
|
(pRequest, pResponse, fNext) =>
|
|
38
40
|
{
|
|
39
41
|
// Send back the request parameters
|
|
@@ -45,7 +47,17 @@ tmpAnticipate.anticipate(
|
|
|
45
47
|
return fStageComplete();
|
|
46
48
|
});
|
|
47
49
|
|
|
50
|
+
// Add the http proxy service
|
|
51
|
+
const libOratorServeStatic = require(`../source/Orator-HTTP-Proxy.js`);
|
|
52
|
+
_Fable.serviceManager.addServiceType('OratorHTTPProxy', libOratorServeStatic);
|
|
53
|
+
_Fable.serviceManager.instantiateServiceProvider('OratorHTTPProxy', {LogLevel: 2});
|
|
48
54
|
// Proxy all /1.0/ requests to the locally-running bookstore service (you need to run this from https://github.com/stevenvelozo/retold-harness ... it's a one-liner to start the service)
|
|
55
|
+
tmpAnticipate.anticipate(
|
|
56
|
+
(fNext)=>
|
|
57
|
+
{
|
|
58
|
+
_Fable.OratorHTTPProxy.connectProxyRoutes();
|
|
59
|
+
return fNext();
|
|
60
|
+
});
|
|
49
61
|
|
|
50
62
|
|
|
51
63
|
// Now start the service server.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<html>
|
|
2
|
+
<head>
|
|
3
|
+
<title>Debug</title>
|
|
4
|
+
</head>
|
|
5
|
+
<body>
|
|
6
|
+
<h1>Debug</h1>
|
|
7
|
+
<p>Debugging is the process of finding and resolving defects or problems within a computer program that prevent correct operation of computer software or a system.</p>
|
|
8
|
+
</body>
|
|
9
|
+
</html>
|
package/package.json
CHANGED
|
@@ -4,9 +4,9 @@ const HttpProxy = require('http-proxy');
|
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Fable service that provies a simple proxy for an orator web server instance that redirects /1.0/* to
|
|
7
|
-
* a given URL (ex. a hosted
|
|
7
|
+
* a given URL (ex. a hosted REST API).
|
|
8
8
|
*/
|
|
9
|
-
class
|
|
9
|
+
class OratorAPIProxy extends FableServiceProviderBase
|
|
10
10
|
{
|
|
11
11
|
/**
|
|
12
12
|
* Construct a service instance.
|
|
@@ -15,33 +15,27 @@ class HeadlightAPIProxyService extends FableServiceProviderBase
|
|
|
15
15
|
* @param {object} pOptions Custom settings for this service instance.
|
|
16
16
|
* @param {string} pServiceHash The hash for this service instance.
|
|
17
17
|
*
|
|
18
|
-
* @return a
|
|
18
|
+
* @return a OratorAPIProxy instance.
|
|
19
19
|
*/
|
|
20
20
|
constructor(pFable, pOptions, pServiceHash)
|
|
21
21
|
{
|
|
22
22
|
super(pFable, pOptions, pServiceHash);
|
|
23
23
|
|
|
24
|
-
if (typeof(this.options.proxyUrl) != 'string' || !this.options.proxyUrl.startsWith('http'))
|
|
25
|
-
{
|
|
26
|
-
this.log.trace('API proxy url falling back to settings...', { badUrl: this.options.proxyUrl });
|
|
27
|
-
this.options.proxyUrl = this.fable.settings.APIProxyUrl;
|
|
28
|
-
}
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
25
|
+
// By jacking up the log level, the static server will become more and more communicative.
|
|
26
|
+
this.LogLevel = (`LogLevel` in this.options) ? this.options.LogLevel
|
|
27
|
+
: `OratorHTTPProxyLogLevel` in this.fable.settings ? this.fable.settings.OratorHTTPProxyLogLevel
|
|
28
|
+
: 0;
|
|
35
29
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
30
|
+
// The proxy url falls back to loopback if not provided.
|
|
31
|
+
this.proxyDestinationURL = (`DestinationURL` in this.options) ? this.options.DestinationURL
|
|
32
|
+
: `OratorHTTPProxyDestinationURL` in this.fable.settings ? this.fable.settings.OratorHTTPProxyDestinationURL
|
|
33
|
+
: 'http://127.0.0.1/';
|
|
40
34
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
35
|
+
// The request prefix falls back to the /1.0/* meadow default if not provided.
|
|
36
|
+
this.requestPrefixList = (`RequestPrefixList` in this.options) ? this.options.RequestPrefix
|
|
37
|
+
: `OratorHTTPProxyRequestPrefixList` in this.fable.settings ? this.fable.settings.OratorHTTPProxyRequestPrefixList
|
|
38
|
+
: ['/1.0/*'];
|
|
45
39
|
|
|
46
40
|
this.httpProxyServer = HttpProxy.createProxyServer({});
|
|
47
41
|
}
|
|
@@ -49,41 +43,62 @@ class HeadlightAPIProxyService extends FableServiceProviderBase
|
|
|
49
43
|
/**
|
|
50
44
|
* Create handlers for each HTTP verb on /1.0/* that proxy requests to the configured proxy URL.
|
|
51
45
|
*/
|
|
52
|
-
connectProxyRoutes(
|
|
46
|
+
connectProxyRoutes(pRequestPrefixList, pProxyURL)
|
|
53
47
|
{
|
|
48
|
+
if (!'Orator' in this.fable)
|
|
49
|
+
{
|
|
50
|
+
this.fable.log.error('Orator must be initialized before adding a static route.');
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// We will proxy each prefix in the array -- this is a convenience function to allow for a single string or function to be passed in.
|
|
55
|
+
let tmpRequestPrefixList = (typeof(pRequestPrefix) === 'string') ? [pRequestPrefixList]
|
|
56
|
+
// Expect the function to return an array
|
|
57
|
+
: (typeof(pRequestPrefixList) === 'function') ? pRequestPrefixList()
|
|
58
|
+
: Array.isArray(pRequestPrefixList) ? pRequestPrefixList
|
|
59
|
+
: this.requestPrefixList;
|
|
60
|
+
|
|
61
|
+
// The complete URL to proxy to (e.g. `http://localhost:3000/`)
|
|
62
|
+
let tmpProxyDestinationURL = (typeof(pProxyURL) === 'string') ? pProxyURL : this.proxyDestinationURL;
|
|
63
|
+
|
|
54
64
|
const proxyRequest = (pRequest, pResponse, fNext) =>
|
|
55
65
|
{
|
|
56
|
-
this.log.info(`Proxying request from URI [${pRequest.url}] to [${
|
|
57
|
-
const
|
|
66
|
+
this.log.info(`Proxying request from URI [${pRequest.url}] to [${tmpProxyDestinationURL}]`);
|
|
67
|
+
const tmpHTTPProxyOptions =
|
|
58
68
|
{
|
|
59
|
-
target:
|
|
69
|
+
target: tmpProxyDestinationURL,
|
|
60
70
|
secure: false, // needed when proxying from HTTP to HTTPS
|
|
61
71
|
};
|
|
62
72
|
if (this.options.httpProxyOptions)
|
|
63
73
|
{
|
|
64
|
-
Object.assign(
|
|
74
|
+
Object.assign(tmpHTTPProxyOptions, this.options.httpProxyOptions);
|
|
65
75
|
}
|
|
66
76
|
try
|
|
67
77
|
{
|
|
68
|
-
this.httpProxyServer.web(pRequest, pResponse,
|
|
78
|
+
this.httpProxyServer.web(pRequest, pResponse, tmpHTTPProxyOptions);
|
|
79
|
+
// return fNext();
|
|
69
80
|
}
|
|
70
81
|
catch (pError)
|
|
71
82
|
{
|
|
72
|
-
this.log.error(`Error proxying ${pRequest.url}: ${pError.message}`, {
|
|
73
|
-
|
|
83
|
+
this.log.error(`Error proxying ${pRequest.url}: ${pError.message}`, { Error: pError });
|
|
84
|
+
// TODO: Make this a configuration
|
|
85
|
+
pResponse.end(JSON.stringify(pError));
|
|
86
|
+
return fNext();
|
|
74
87
|
}
|
|
75
88
|
};
|
|
76
89
|
|
|
77
|
-
this.log.info('Adding API
|
|
90
|
+
this.log.info('Adding API proxies to orator...', { proxyUrl: this.options.proxyUrl, requestPrefixes: this.options.requestPrefixes });
|
|
78
91
|
|
|
79
|
-
for (
|
|
92
|
+
for (let i = 0; i < tmpRequestPrefixList.length; i++)
|
|
80
93
|
{
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
94
|
+
let tmpRequestPrefix = tmpRequestPrefixList[i];
|
|
95
|
+
this.log.info(`Adding http connection from [${tmpRequestPrefix}] proxied to ${tmpProxyDestinationURL}`);
|
|
96
|
+
this.fable.Orator.serviceServer.get(tmpRequestPrefix, proxyRequest);
|
|
97
|
+
this.fable.Orator.serviceServer.put(tmpRequestPrefix, proxyRequest);
|
|
98
|
+
this.fable.Orator.serviceServer.del(tmpRequestPrefix, proxyRequest);
|
|
99
|
+
this.fable.Orator.serviceServer.post(tmpRequestPrefix, proxyRequest);
|
|
85
100
|
}
|
|
86
101
|
}
|
|
87
102
|
}
|
|
88
103
|
|
|
89
|
-
module.exports =
|
|
104
|
+
module.exports = OratorAPIProxy;
|
|
File without changes
|