Skip to content

Commit

Permalink
make proxy configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikespub committed Jun 21, 2018
1 parent 1eb0e60 commit fc25529
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"default": "aws",
"aws": {
"name": "aws-pricing",
"description": "AWS Price List API",
"backend": "pricing.us-east-1.amazonaws.com",
"path": "/offers/v1.0/aws/index.json",
"origin": "*"
},
"eos": {
"name": "fcsm-eos-api",
"description": "FCSM EOS API",
"backend": "emeia.fcsm.io",
"path": "/api/v1/analytics/summary",
"origin": "github.mikespub.net"
}
}
10 changes: 7 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
const https = require('https');
const agent = new https.Agent({keepAlive: true});

const backend = 'pricing.us-east-1.amazonaws.com';
const config = require('./config.json')

//https://github.com/firebase/functions-samples/blob/master/quickstarts/time-server/functions/index.js
// CORS Express middleware to enable CORS Requests.
Expand All @@ -20,6 +20,10 @@ const backend = 'pricing.us-east-1.amazonaws.com';
var cachedAgent = function cachedAgent(req, res) {
method = req.method || 'GET';
path = req.path || '/offers/v1.0/aws/index.json';
// TODO: choose proxy config based on path prefix?
proxy = config[config['default']]
backend = proxy.backend || 'pricing.us-east-1.amazonaws.com';
origin = proxy.origin || '*';
//If-Modified-Since: Sat, 05 May 2018 20:38:27 GMT
modified = req.get('If-Modified-Since');
//If-None-Match: "5e5d61fb71903f6b8f0123335154d11b"
Expand All @@ -31,7 +35,7 @@ var cachedAgent = function cachedAgent(req, res) {
if (match) {
headers['If-None-Match'] = match;
}
console.log(method + ' ' + path + ' ' + req.ips);
console.log(method + ' ' + path + ' ' + proxy.name + ' ' + req.ips);
reqInner = https.request({
hostname: backend,
port: 443,
Expand All @@ -45,7 +49,7 @@ var cachedAgent = function cachedAgent(req, res) {
resInner.on('data', chunk => { rawData += chunk; });
resInner.on('end', () => {
//https://expressjs.com/en/api.html#res
res.set('Access-Control-Allow-Origin', "*");
res.set('Access-Control-Allow-Origin', origin);
res.set('Access-Control-Allow-Methods', "GET, HEAD, OPTIONS");
res.set('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
// CHECKME: what about HEAD or GET 304? We need to expose Content-Length and ETag
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gcf-cors-proxy",
"version": "1.0.0",
"version": "1.0.2",
"description": "Basic CORS Proxy via Google Cloud Functions",
"license": "Apache-2.0",
"author": "mikespub",
Expand Down

0 comments on commit fc25529

Please sign in to comment.