//Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//Licensed under the Apache License, Version 2.0 (the "License"). 
//You may not use this file except in compliance with the License. 
//A copy of the License is located at
//
//    http://aws.amazon.com/apache2.0/
//
//or in the "license" file accompanying this file. This file is distributed 
//on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
//either express or implied. See the License for the specific language 
//governing permissions and limitations under the License.

//Get modules.
var express = require('express');
var http = require('http');
var routes = require('./routes');
var path = require('path');
var fs = require('fs');
var AWS = require('aws-sdk');
var jade = require('jade')
var app = express();

app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(app.router);


//Read config values from a JSON file.
var config = fs.readFileSync('./app_config.json', 'utf8');
config = JSON.parse(config);

//Create DynamoDB client and pass in region.
var db = new AWS.DynamoDB({
	region: config.AWS_REGION
});

//GET home page.
//app.get('/', routes.index);

//GET resolve
app.get('/resolve', function(req, res) {
	var appIdField = req.query.appId;
	console.log('resolving', appIdField);
	resolve(appIdField, function(data) {
		console.log('got data ', data);
		res.render('template', {data: data})
		//res.send(data)
	});
});

var resolve = function(appId, cb) {
	var params = {
		TableName: 'apps',
		Key: {
			"app_id": {
				"S": appId
			},
		}
	}
	db.getItem(params, function(err, data) {
		console.log('sent', params)
		if (err) {
			console.log('Error getting data from database: ', err);
			cb(err)
		} else {
			console.log('Got data:', data);
			cb(data)
		}
	});
};

//app.get('/add')

http.createServer(app).listen(app.get('port'), function() {
	console.log('Express server listening on port ' + app.get('port'));
});

@@ -1,580 +1,4 @@
-//Copyright 2013-2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.%0A//Licensed under the Apache License, Version 2.0 (the %22License%22). %0A//You may not use this file except in compliance with the License. %0A//A copy of the License is located at%0A//%0A//    http://aws.amazon.com/apache2.0/%0A//%0A//or in the %22license%22 file accompanying this file. This file is distributed %0A//on an %22AS IS%22 BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, %0A//either express or implied. See the License for the specific language %0A//governing permissions and limitations under the License.%0A%0A
 //Ge
@@ -354,36 +354,8 @@
 ');%0A
-app.use(express.favicon());%0A
 app.
@@ -492,24 +492,26 @@
 /public'));%0A
+//
 app.use(app.
@@ -1387,16 +1387,17 @@
 ot data:
+ 
 ', data)
@@ -1444,139 +1444,4 @@
 dd')
-%0A%0Ahttp.createServer(app).listen(app.get('port'), function() %7B%0A%09console.log('Express server listening on port ' + app.get('port'));%0A%7D);%0A

remove favicon ref?