-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·65 lines (56 loc) · 2.1 KB
/
app.js
File metadata and controls
executable file
·65 lines (56 loc) · 2.1 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
var bodyParser = require('body-parser');
var express = require('express');
var redis = require('redis');
var axios = require('axios');
var app = express()
//var client = redis.createClient(process.env.REDISCLOUD_URL, {no_ready_check: true});
// assign app settings from envvironment || defaults
const port = process.env.PORT || 8080;
const name = process.env.HEROKU_APP_NAME || 'Unknown Name';
const version = process.env.HEROKU_RELEASE_VERSION || 'Unknown Version';
const BTC_ADDR = process.env.BTC_ADDRESS_LIST || '3C8667tWtc9tLU3Fhp8J1u2NQ9fXijS8AM';
// parse application/json
app.use(bodyParser.json())
// make express look in the public directory for assets (css/js/img)
app.use(express.static(__dirname + '/public'));
// set the home page route
app.get('/', function(req, res) {
res.json({"name": name,"version": version});
});
//
// Retrieve last transaction sent to pre-sale/sale BTC address
//
app.post('/transaction/update', function(req, res) {
const url = "https://blockchain.info/address/" + BTC_ADDR + "?format=json";
axios.get(url)
.then(function(response) {
const body = response.data;
const txn = body.result[0];
const ts = +new Date()
const sender = body.result[0].from;
res.json({"sender": sender, "txn": txn, "timestamp": ts, "count": body.result.length});
})
.catch(function (err) {
res.status(500).send('Error fetching transaction data');
});
});
//
// Retrieve total transactions sent to BTC address
//
app.get('/transaction/total', function(req, res) {
const url = "https://blockchain.info/balance/" + BTC_ADDR + "?format=json";
axios.get(url)
.then(function(response) {
const body = response.data;
const total = body.result;
const ts = +new Date()
res.json({"currency": "BTC","total": total, "timestamp": ts});
})
.catch(function (err) {
res.status(500).send('Error fetching balance data');
});
});
// Start the app listening to default port
app.listen(port, function() {
console.log(name + ' app is running on port ' + port);
});