From 2bb57e067e196b3623eb86528517addd52b67943 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 3 Nov 2015 18:58:28 -0500 Subject: app proxy running smoothly --- proxy/index.js | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 116 insertions(+), 11 deletions(-) (limited to 'proxy/index.js') diff --git a/proxy/index.js b/proxy/index.js index c2e12c60..85a4981c 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -1,15 +1,52 @@ var http = require('http') -var PORT = 4567 +var https = require('https') +var path = require('path') +var fs = require('fs') +var url = require('url') -var server = http.createServer(function (req, res){ - res.end('It Works!! Path Hit: ' + req.url) - console.log(req.headers) +var PORT = 9090 + +var server = http.createServer().listen(PORT, function(){ + console.log("Proxy listening on: http://lvh.me:%s", PORT) }) -server.listen(PORT, function(){ - console.log("Proxy listening on: http://localhost:%s", PORT) +server.on('request', function (req, res){ + + if ( req.method == 'GET' && ! req.url.match(/API/) ) { + // public... + return stream(req, res) + } + + console.log(req.method, req.url) + + // console.log(req.headers) + + var options = {} + options.headers = get_headers(req.headers) + options.method = req.method + options.port = 443 + options.hostname = "secure.api.yoox.biz" + options.path = req.url + + req.pause() + + var connector = https.request(options, function(server_res) { + console.log(">> GOT", server_res.statusCode) + + server_res.pause() + res.writeHeader(server_res.statusCode, server_res.headers) + // server_res.on("data", function(s){ console.log("<<", s.toString()) }) + server_res.pipe(res) + server_res.resume() + }) + // req.on("data", function(s){ console.log(">>", s.toString()) }) + req.on("error", function(s){ console.log("/!\\ ERROR /!\\"); console.log(s) }) + req.pipe(connector) + req.resume() + }) + /* headers: { "x-yoox-appname": auth.appname, @@ -18,8 +55,76 @@ server.listen(PORT, function(){ "x-yoox-api-key": auth.apikey, "x-yoox-cart-token": cart.token, }, - - proxy content body - proxy method - proxy query string -*/ \ No newline at end of file +*/ + +function get_headers (h){ + var hh = {} + "appname account-token device api-key cart-token".split(" ").forEach(function(s){ + var key = "x-yoox-" + s + if (key in h) hh[key] = h[key] + }) + hh['Content-Type'] = "application/json" // h['content-type'] + if ('content-length' in h) hh['Content-Length'] = h['content-length'] + if ('connection' in h) hh['Connection'] = h['connection'] + return hh +} + +var mimes = { + "gif": "image/gif", + "png": "image/png", + "jpg": "image/jpeg", + "jpeg": "image/jpeg", + "html": "text/html", + "js": "application/javascript", + "css": "text/css", + "woff": "application/font-woff", + "ttf": "application/font-woff", +} + +function stream (req, res) { + var url = req.url.toLowerCase().split("?")[0] + var ext_partz = url.split("."), ext = ext_partz[ext_partz.length-1] + var mime = mimes[ext] || "application/octet-stream" + + if (! url || url == "/") { + url = "index.html" + mime = "text/html" + } + + var file = path.resolve("../StoneIsland/www/" + url) + + fs.stat(file, function(err, stats) { + if (! stats) { + // console.log(404) + res.writeHead(404) + res.end("404") + return + } + + var headers = { + "Content-Length": stats.size, + "Content-Type": mime, + } + + res.writeHead(200, "OK", headers) + + var stream = fs.createReadStream(file, { start: 0, end: stats.size }) + stream.on("open", function() { + stream.pipe(res) + }) + stream.on("error", function(err) { + console.log(err) + stream.destroy() + res.end() + }) + stream.on("close", function(){ + stream.destroy() + res.end() + }) + req.connection.setMaxListeners(50) + req.connection.on('close', function(){ + stream.destroy() + res.end() + }) + }) +} \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 6a1456f06024b646647db8512b6b1288aaba783f Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 3 Nov 2015 20:00:44 -0500 Subject: cart working --- StoneIsland/www/js/sdk/cart.js | 1 + proxy/index.js | 6 +++--- test/test/04-cart.js | 8 +------- 3 files changed, 5 insertions(+), 10 deletions(-) (limited to 'proxy/index.js') diff --git a/StoneIsland/www/js/sdk/cart.js b/StoneIsland/www/js/sdk/cart.js index 9f9d81f1..9fb9b11f 100644 --- a/StoneIsland/www/js/sdk/cart.js +++ b/StoneIsland/www/js/sdk/cart.js @@ -13,6 +13,7 @@ sdk.cart = (function(){ "x-yoox-appname": auth.appname, "x-yoox-device": auth.device, }, + data: "{}", // data: opt.data, success: function(data){ console.log(data) diff --git a/proxy/index.js b/proxy/index.js index 85a4981c..45d38f1d 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -29,17 +29,17 @@ server.on('request', function (req, res){ options.path = req.url req.pause() - + console.log(options) var connector = https.request(options, function(server_res) { console.log(">> GOT", server_res.statusCode) server_res.pause() res.writeHeader(server_res.statusCode, server_res.headers) - // server_res.on("data", function(s){ console.log("<<", s.toString()) }) + server_res.on("data", function(s){ console.log("<<", s.toString()) }) server_res.pipe(res) server_res.resume() }) - // req.on("data", function(s){ console.log(">>", s.toString()) }) + req.on("data", function(s){ console.log(">>", s.toString()) }) req.on("error", function(s){ console.log("/!\\ ERROR /!\\"); console.log(s) }) req.pipe(connector) req.resume() diff --git a/test/test/04-cart.js b/test/test/04-cart.js index 2272f7bb..31764f5e 100644 --- a/test/test/04-cart.js +++ b/test/test/04-cart.js @@ -25,13 +25,7 @@ describe('cart', function(){ describe('#initialize()', function(){ it('initializes the cart', function(done){ - if (! sdk.auth.access_token) { - sdk.auth.access_token = "45871479f5001afc06e628c7bb8e95ffb1f71df8" - sdk.auth.user_id = 374663521 - } - - promise(sdk.address.add, { data: {} }).then(function(data){ - console.log(data) + promise(sdk.cart.initialize, { data: {} }).then(function(data){ assert(data.Header.StatusCode == 200) assert(sdk.cart.id !== "") assert(sdk.cart.token !== "") -- cgit v1.2.3-70-g09d2 From 80f1271c496e8e9af6c3b746d1481e23363add61 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Tue, 3 Nov 2015 20:07:32 -0500 Subject: dont log so much --- proxy/index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'proxy/index.js') diff --git a/proxy/index.js b/proxy/index.js index 45d38f1d..ae9b0aa9 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -29,17 +29,16 @@ server.on('request', function (req, res){ options.path = req.url req.pause() - console.log(options) var connector = https.request(options, function(server_res) { console.log(">> GOT", server_res.statusCode) server_res.pause() res.writeHeader(server_res.statusCode, server_res.headers) - server_res.on("data", function(s){ console.log("<<", s.toString()) }) + // server_res.on("data", function(s){ console.log("<<", s.toString()) }) server_res.pipe(res) server_res.resume() }) - req.on("data", function(s){ console.log(">>", s.toString()) }) + // req.on("data", function(s){ console.log(">>", s.toString()) }) req.on("error", function(s){ console.log("/!\\ ERROR /!\\"); console.log(s) }) req.pipe(connector) req.resume() -- cgit v1.2.3-70-g09d2