-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
220 lines (196 loc) · 6.91 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*jshint node:true*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
if (process.env.VCAP_SERVICES) {
var env = JSON.parse(process.env.VCAP_SERVICES);
var mongo = env['mongolab'][0].credentials;
} else {
var mongo = {
"username" : "user1",
"password" : "secret",
"uri" : 'mongodb://127.0.0.1:27017/test'
};
}
// This application uses express as it's web server
// for more info, see: http://expressjs.com
var express = require('express');
var satelize = require('satelize');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
var requestIp = require('request-ip');
var http = require('http');
var hardcode_data = require('./hardcode_data');
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
// Connection URL
var MongoUrl = mongo.uri;
var WaterDataCollection = "WaterDataCollection";
// Use connect method to connect to the Server
//MongoClient.connect(MongoUrl, function(err, db) {
// assert.equal(null, err);
// console.log("Connected correctly to server");
// db.close();
//});
var WaterSupplyDemandDataLib = require('./WaterSupplyDemandData');
var WaterSupplyDemandData = WaterSupplyDemandDataLib.WaterSupplyDemandData;
// create a new express server
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: false }));
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
//--------------------------------------------------
// Functions definitions
//--------------------------------------------------
function GetCityInformation(CityName, StateName, callback) {
return http.get({
host: 'api.sba.gov',
path: '/geodata/all_links_for_city_of/' + CityName + '/' + StateName + '.json'
}, function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
callback(parsed, StateName);
});
});
};
function getWaterDataByStateCounty(StateAbbr, County){
for (var i = 0; i < WaterSupplyDemandData.length; i++){
if (WaterSupplyDemandData[i].StateAbbr == StateAbbr && WaterSupplyDemandData[i].County == County){
return WaterSupplyDemandData[i];
}
}
return null;
}
function getCountyList(StateAbbr, callback){
return http.get({
host: 'api.sba.gov',
path: '/geodata/county_links_for_state_of/' + StateAbbr + '.json'
}, function(response) {
// Continuously update stream with data
var body = '';
response.on('data', function(d) {
body += d;
});
response.on('end', function() {
// Data reception is done, do whatever with it!
var parsed = JSON.parse(body);
var processed = parsed.map(function(array_elem){
return array_elem.name;
});
callback(processed);
});
});
}
//--------------------------------------------------
// MongoDB
//--------------------------------------------------
MongoClient.connect(MongoUrl, function(err, db) {
db.createCollection('WaterDataCollection', function(err, collection){
if (err) { console.log("Got error creating collection"); throw err;}
collection.remove({});
for (var i = 0; i < WaterSupplyDemandData.length; i++){
collection.insert(WaterSupplyDemandData[i]);
}
});
});
function getWaterDataByStateCountyMongoDB(StateAbbr, County, callback){
console.log("querying " + StateAbbr + " and county " + County);
MongoClient.connect(MongoUrl, function(err, db) {
var collection = db.collection('WaterDataCollection');
collection.findOne({"StateAbbr": StateAbbr, "County": County}, function(err, find_result){
if (err) throw err;
console.log(find_result);
callback(find_result);
});
});
}
//--------------------------------------------------
// Routes allowed in this app
//--------------------------------------------------
//app.post('/login',function(req,res){
// var data = req.body.data;
// var ip = requestIp.getClientIp(req);
// if (ip == "127.0.0.1"){
// ip = "184.177.20.169";
// }
// satelize.satelize({ip:ip}, function(err, geoData){
// var obj = JSON.parse(geoData);
// GetCityInformation( obj.city.replace(' ', '%20'), obj.region_code, function (cityinfo){
// res.end("You're in " + cityinfo[0].full_county_name + " in " + cityinfo[0].state_name);
// //res.end(JSON.stringify(cityinfo));
// });
// });
//});
app.get('/ajaxget', function(req, res){
console.log('ajaxget: ' + JSON.stringify(req.query));
var data = req.query;
if (data.functionName == 'getStateList'){
res.end(JSON.stringify({
"StateList": hardcode_data.StateList
}));
} else if (data.functionName == 'getCountyList'){
var StateAbbr = data.abbreviation;
getCountyList(StateAbbr, function(data){
res.end(JSON.stringify({
"CountyList": data
}));
});
} else if (data.functionName == 'getCurrentLocation'){
var data = req.body.data;
var ip = requestIp.getClientIp(req);
if (ip == "127.0.0.1"){
ip = "184.177.20.169";
}
satelize.satelize({ip:ip}, function(err, geoData){
try {
var obj = JSON.parse(geoData);
} catch (err){
var obj = {
city: "Irvine",
region_code: "CA"
};
}
GetCityInformation( obj.city.replace(' ', '%20'), obj.region_code, function (cityinfo,StateAbbr){
res.end(JSON.stringify({
"County": cityinfo[0].full_county_name,
"State": cityinfo[0].state_name,
"StateAbbr": StateAbbr
}));
//res.end(JSON.stringify(cityinfo));
});
});
} else if (data.functionName == 'getWaterData'){
var lookup_result = getWaterDataByStateCounty(data.StateAbbr, data.County);
if (lookup_result == null){
res.status(500).send({'error': "Can't find info for this state and county"});
} else {
res.end(JSON.stringify(lookup_result));
}
} else if (data.functionName == 'getWaterDataMongoDB'){
getWaterDataByStateCountyMongoDB(data.StateAbbr, data.County, function(lookup_result){
if (lookup_result == null){
res.status(500).send({'error': "Can't find info for this state and county"});
} else {
res.end(JSON.stringify(lookup_result));
}
});
}
});
//--------------------------------------------------
// Main function
//--------------------------------------------------
// start server on the specified port and binding host
app.listen(appEnv.port, appEnv.bind, function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});