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 Service Server Proxy
1
+ # Orator Static File Server
2
2
 
3
- Proxy API requests from a stable prefix. This lets you map /1.0/ to one set
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
- Meant to be used for local development and pass-through API for front-side
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "orator-http-proxy",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Orator http proxy pass-through.",
5
5
  "main": "source/Orator-HTTP-Proxy.js",
6
6
  "scripts": {
@@ -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 Headlight API).
7
+ * a given URL (ex. a hosted REST API).
8
8
  */
9
- class HeadlightAPIProxyService extends FableServiceProviderBase
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 HeadlightAPIProxyService instance.
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
- if (typeof(this.options.proxyUrl) != 'string' || !this.options.proxyUrl.startsWith('http'))
31
- {
32
- this.log.trace('API proxy url falling back to default...', { badUrl: this.options.proxyUrl });
33
- this.options.proxyUrl = 'https://127.0.0.1/';
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
- if (typeof(this.options.requestPrefix) != 'string' || !this.options.requestPrefix.startsWith('/'))
37
- {
38
- this.options.requestPrefix = '/1.0/*';
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
- if (!Array.isArray(this.options.requestPrefixes) || this.options.requestPrefixes.length < 1)
42
- {
43
- this.options.requestPrefixes = [this.options.requestPrefix];
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(pOrator)
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 [${this.options.proxyUrl}]`);
57
- const options =
66
+ this.log.info(`Proxying request from URI [${pRequest.url}] to [${tmpProxyDestinationURL}]`);
67
+ const tmpHTTPProxyOptions =
58
68
  {
59
- target: this.options.proxyUrl,
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(options, this.options.httpProxyOptions);
74
+ Object.assign(tmpHTTPProxyOptions, this.options.httpProxyOptions);
65
75
  }
66
76
  try
67
77
  {
68
- this.httpProxyServer.web(pRequest, pResponse, options);
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}`, { stack: pError.stack });
73
- pResponse.end(` - ERROR: ${pError.message}`);
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 proxy to orator...', { proxyUrl: this.options.proxyUrl, requestPrefixes: this.options.requestPrefixes });
90
+ this.log.info('Adding API proxies to orator...', { proxyUrl: this.options.proxyUrl, requestPrefixes: this.options.requestPrefixes });
78
91
 
79
- for (const requestPrefix of this.options.requestPrefixes)
92
+ for (let i = 0; i < tmpRequestPrefixList.length; i++)
80
93
  {
81
- pOrator.webServer.get(requestPrefix, proxyRequest);
82
- pOrator.webServer.put(requestPrefix, proxyRequest);
83
- pOrator.webServer.del(requestPrefix, proxyRequest);
84
- pOrator.webServer.post(requestPrefix, proxyRequest);
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 = HeadlightAPIProxyService;
104
+ module.exports = OratorAPIProxy;