Skip to content

Commit

Permalink
updated server config and task to handle configuration of protocol an…
Browse files Browse the repository at this point in the history
…d ports, updated collection to determine which way to call the service based on local config variables
  • Loading branch information
kellyrmilligan committed Jul 9, 2013
1 parent e9ce6a7 commit 878c219
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ module.exports = function( grunt ) {
port: 8080,
vhost: 'localhost',
base: 'src/www',
apiPrefix: '/api*'
apiPrefix: '/api',
apiBaseUrl: 'www.googleapis.com',
proxyPort: '443',
proxyProtocol: 'https'
}
},
prod: {
Expand Down
4 changes: 3 additions & 1 deletion src/www/configs/local.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"example": "value - local"
"example": "value - local",
"apiPrefix" : "/api",
"apiBaseUrl":"https://www.googleapis.com"
}
8 changes: 6 additions & 2 deletions src/www/js/app/collections/BookCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define(function(require) {
var BookModel = require('app/models/BookModel');
var stateModel = require('app/models/StateModel');
var favoriteCollection = require('app/collections/FavoriteCollection');
var Config = require('lavaca/util/Config');

var BookCollection = Collection.extend(function() {
Collection.apply(this, arguments);
Expand All @@ -24,7 +25,7 @@ define(function(require) {
*/
itemsProperty: 'books',

url: 'https://www.googleapis.com/books/v1/volumes',
url: '/books/v1/volumes',

maxResults: 40,

Expand All @@ -49,8 +50,11 @@ define(function(require) {
}
var opts = {};

var url = Config.get('apiPrefix') ? Config.get('apiPrefix') + this.url : Config.get('apiBaseUrl') + this.url;

opts = {
dataType: 'jsonp',
dataType: Config.get('apiPrefix')? 'json' : 'jsonp',
url: url,
data: {
q: searchTerm,
maxResults: this.maxResults,
Expand Down
17 changes: 11 additions & 6 deletions tasks/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ module.exports = function(grunt) {

/* server.js */
var express = require('express'),
util = require('util'),
http = require('http');
util = require('util');


var startServer = function(config) {
config = config || {};

var requester = require(config.proxyProtocol);
var server = express();
var hourMs = config.hourMs || 0*60*60,
vhost = config.vhost || 'localhost',
Expand All @@ -21,7 +23,7 @@ module.exports = function(grunt) {
var postData = request.body;
var options = {
host: host,
port: '80',
port: config.proxyPort,
method: request.method,
path: request.originalUrl.replace(new RegExp(apiPrefix), ''),
headers: {}
Expand All @@ -34,7 +36,7 @@ module.exports = function(grunt) {
if ('POST' === request.method && typeof postData === 'object') {
postData = JSON.stringify(postData);
}
var req = http.request(options, function(res) {
var req = requester.request(options, function(res) {
var output = '';
console.log(options.method + ' @ ' + options.host + options.path + ' Code: '+ res.statusCode);
res.setEncoding('utf8');
Expand Down Expand Up @@ -65,6 +67,7 @@ module.exports = function(grunt) {
req.write(postData);
}


req.end();
}

Expand Down Expand Up @@ -92,12 +95,14 @@ module.exports = function(grunt) {
var options = this.options({
});
var server = startServer({
host: 'localhost', // override this with your third party API host, such as 'search.twitter.com'.
host: options.apiBaseUrl, // override this with your third party API host, such as 'search.twitter.com'.
hourMs: 0*60*60,
vhost: options.vhost,
base: options.base,
port: options.port,
apiPrefix: options.apiPrefix
apiPrefix: options.apiPrefix,
proxyPort: options.proxyPort || '80',
proxyProtocol: options.proxyProtocol || 'http'
}),
args = this.args,
done = args[args.length-1] === 'watch' ? function() {} : this.async();
Expand Down

0 comments on commit 878c219

Please sign in to comment.