-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (152 loc) · 5.31 KB
/
app.js
File metadata and controls
184 lines (152 loc) · 5.31 KB
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
var fs = require('fs');
var express = require('express');
var app = express();
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
app.use(express.bodyParser());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
var dg = {};
var users = [];
eval(fs.readFileSync('./graph.js')+'');
eval(fs.readFileSync('./secrets.js')+'');
if (process.argv.length <= 2) tcpport = 8888;
else tcpport = process.argv[2]; //port of the HTTP server
function findById(id, fn) {
var idx = id - 1;
if (users[idx]) {
fn(null, users[idx]);
} else {
fn(new Error('User ' + id + ' does not exist'));
}
}
function findByUsername(username, fn) {
for (var i = 0, len = users.length; i < len; i++) {
var user = users[i];
if (user.username === username) {
return fn(null, user);
}
}
return fn(null, null);
}
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing.
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
findById(id, function (err, user) {
done(err, user);
});
});
// Facebook connect stuff with passport
passport.use(
new FacebookStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET,
callbackURL: "http://localhost:8888/auth/facebook/callback",
},
function(accessToken, refreshToken, profile, done) {
console.log(profile);
// Find the user by username. If there is no user with the given
// username, or the password is not correct, set the user to `false` to
// indicate failure and set a flash message. Otherwise, return the
// authenticated `user`.
findByUsername(profile.username, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
users[users.length] = profile;
}
return done(null, user);
})
}
));
// End facebook connect section
// Redirect the user to Facebook for authentication. When complete,
// Facebook will redirect the user back to the application at
// /auth/facebook/callback
app.get('/auth/facebook', passport.authenticate('facebook'));
// Facebook will redirect the user to this URL after approval. Finish the
// authentication process by attempting to obtain an access token. If
// access was granted, the user will be logged in. Otherwise,
// authentication has failed.
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { successRedirect: '/',
failureRedirect: '/' }));
app.get('/', function(req, res){
indexer = fs.readFileSync('index.html').toString();
console.log(users);
res.send(indexer);
});
app.use("/assets", express.static(__dirname + '/assets'));
//Accept post request
app.post('/addTransPost', addTransaction, function(req, res) {
});
//Handle a user's request for their transaction information
app.post('/requestTransDict', serveTrans, function(req, res) {
});
try {
app.listen(tcpport);
} catch(err) {
console.log(err);
console.log("Could not start express server!");
process.exit();
}
console.log('Express server started on port ' + tcpport.toString() +
'\nGo to http://localhost:'+ tcpport.toString() +
'/ with your web browser.');
function addTransaction(req, res) {
console.log(req.body);
var myError, myStatus;
var lender = req.body.lender;
var borrower = req.body.borrower;
var amount = req.body.amount;
var description = req.body.description;
console.log("Attempt to Add Transaction from: " + lender);
if(isNumber(amount) && amount >= 0) {
myError = "";
myStatus = 1;
updateGraph(lender, borrower, amount, description);
checkCycle(lender, borrower);
} else {
myError = "Amount is not a positive number.";
myStatus = 0;
}
res.send({status:myStatus, error:myError});
}
function updateGraph(lender, borrower, amount, description) {
addTransactionHalf(dg, borrower, lender,
parseFloat(amount), description);
addTransactionHalf(dg, lender, borrower,
-parseFloat(amount), description);
}
function checkCycle(lender, borrower) {
var cyclePath = bestCycle(dg, lender, borrower);
if (cyclePath != "none") {
var amount = cyclePath.amount
// fix to be more user friendly ???
var desc = "Cycle resolution"
updateGraph(lender, cyclePath.path[0], amount, desc);
for (var i = 1; i < cyclePath.path.length; i++) {
updateGraph(cyclePath.path[i - 1], cyclePath.path[i], amount, desc);
}
updateGraph(borrower, lender, amount, desc);
}
console.log(cyclePath);
}
// Thank you "CMS" on Stack Overflow
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
function serveTrans(req, res) {
var user = req.body.user;
var netValue = getNet(dg, user);
var myNeighbors = getNeighbors(dg, user);
res.send({value:netValue, neighbors:myNeighbors});
}