forked from ScriptGadget/bitstarter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
181 lines (167 loc) · 5.1 KB
/
web.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
// Define routes for simple SSJS web app.
// Writes Coinbase orders to database.
var async = require('async')
, express = require('express')
, fs = require('fs')
, http = require('http')
, https = require('https')
, db = require('./models');
var app = express();
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('port', process.env.PORT || 8080);
app.use(express.bodyParser());
// Render homepage (note trailing slash): example.com/
app.get('/', function(request, response) {
var data = fs.readFileSync('index.html').toString();
response.send(data);
});
// Render example.com/orders
app.get('/orders', function(request, response) {
global.db.Order.findAll().success(function(orders) {
var orders_json = [];
orders.forEach(function(order) {
orders_json.push({id: order.coinbase_id, amount: order.amount, time: order.time});
});
// Uses views/orders.ejs
response.render("orders", {orders: orders_json});
}).error(function(err) {
console.log(err);
response.send("error retrieving orders");
});
});
// Hit this URL while on example.com/orders to refresh
app.get('/refresh_orders', function(request, response) {
https.get("https://coinbase.com/api/v1/orders?api_key=" + process.env.COINBASE_API_KEY, function(res) {
var body = '';
res.on('data', function(chunk) {body += chunk;});
res.on('end', function() {
try {
var orders_json = JSON.parse(body);
if (orders_json.error) {
response.send(orders_json.error);
return;
}
// add each order asynchronously
async.forEach(orders_json.orders, addOrder, function(err) {
if (err) {
console.log(err);
response.send("error adding orders");
} else {
// orders added successfully
response.redirect("/orders");
}
});
} catch (error) {
console.log(error);
response.send("error parsing json");
}
});
res.on('error', function(e) {
console.log(e);
response.send("error syncing orders");
});
});
});
// sync the database and start the server
db.sequelize.sync().complete(function(err) {
if (err) {
throw err;
} else {
http.createServer(app).listen(app.get('port'), function() {
console.log("Listening on " + app.get('port'));
});
}
});
// add order to the database if it doesn't already exist
var addOrder = function(order_obj, callback) {
var order = order_obj.order; // order json from coinbase
if (order.status != "completed") {
// only add completed orders
callback();
} else {
var Order = global.db.Order;
// find if order has already been added to our database
Order.find({where: {coinbase_id: order.id}}).success(function(order_instance) {
if (order_instance) {
// order already exists, do nothing
callback();
} else {
// build instance and save
var new_order_instance = Order.build({
coinbase_id: order.id,
amount: order.total_btc.cents / 100000000, // convert satoshis to BTC
time: order.created_at
});
new_order_instance.save().success(function() {
callback();
}).error(function(err) {
callback(err);
});
}
});
}
};
// Register a claim
app.post('/claim', function(request, response) {
try {
global.db.Claim.create({
email: request.body.email,
latitude: request.body.latitude,
longitude: request.body.longitude,
size: request.body.size,
name: request.body.name,
description: request.body.description,
}).success(function(claim) {
response.send(JSON.stringify([{
message: "Thank You",
id: claim.id,
size: claim.size,
longitude: claim.longitude,
latitude: claim.latitude,
name: claim.name,
description: claim.description,
}]));
}).error(function(error) {
console.log(error);
response.send(JSON.stringify([{error: "Error adding claim: " + error}]));
});
} catch (error) {
console.log(error);
response.send(JSON.stringify([{error: "Error adding claim: " + error}]));
}
});
app.get('/claims', function(request, response) {
global.db.Claim.findAll().success(function(claims) {
var claims_json = [];
claims.forEach(function(claim) {
claims_json.push({
name: claim.name,
size: claim.size,
description: claim.description,
longitude: claim.longitude,
latitude: claim.latitude,
});
});
response.send(JSON.stringify(claims_json));
}).error(function(error) {
console.log(error);
response.send(JSON.stringify([{error: "error retrieving claims: " + error}]));
});
});
app.get('/score', function(request, response) {
global.db.Order.findAll().success(function(orders) {
var btc = 0.0;
var count = 0;
orders.forEach(function(order) {
if (order.amount > 0.005) {
btc += order.amount;
count += 1;
}
});
response.send(JSON.stringify([{btc_raised: btc, backer_count: count}]));
}).error(function (error) {
console.log(error);
response.send(JSON.stringify([{error: "error retrieving score" + error}]))
});
});