-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
124 lines (115 loc) · 4 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
// 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);
// Render homepage (note trailing slash): example.com/
app.get('/', function(request, response) {
var data = fs.readFileSync('index.html').toString();
response.send(data);
});
// Sotiris attempts to hack db insertion
app.get('/pay', function(request, response) {
var attempt_order_instance = global.db.Order.build({
coinbase_id: String(Date.now()),
amount: 0,
time: Date()
});
attempt_order_instance.save();
console.log("Something must have happened...");
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);
});
}
});
}
};