-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (88 loc) · 3.96 KB
/
index.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
const client = require('./connection')
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.listen(3300, () => {
console.log("Server is now listening on Port 3300");
});
// begin client module
client.connect();
// getFromDBAndSend function
function getFromDBAndSend(query, res) {
client.query(query, (err, result) => {
if (!err) {
res.json(result.rows); // Send JSON response
} else {
res.status(500).json({ error: err.message });
}
});
}
// Helper function to build query with pagination and sorting
function buildQuery(baseQuery, page, limit, sort, order) {
const offset = (page - 1) * limit;
const sorting = sort ? `ORDER BY ${sort} ${order}` : '';
return `${baseQuery} ${sorting} LIMIT ${limit} OFFSET ${offset}`;
}
// Routes
app.get('/accounts', (req, res) => {
const { page = 1, limit = 10, sort = 'timestamp', order = 'DESC' } = req.query;
const LATEST_ACCOUNTS_QUERY = buildQuery('SELECT * FROM accounts', page, limit, sort, order);
getFromDBAndSend(LATEST_ACCOUNTS_QUERY, res);
});
app.get('/blocks', (req, res) => {
const { page = 1, limit = 10, sort = 'timestamp', order = 'DESC' } = req.query;
const LATEST_BLOCKS_QUERY = buildQuery('SELECT * FROM blocks', page, limit, sort, order);
getFromDBAndSend(LATEST_BLOCKS_QUERY, res);
});
app.get('/transactions', (req, res) => {
const { page = 1, limit = 10, sort = 'block_number', order = 'DESC' } = req.query;
const LATEST_TRANSACTIONS_QUERY = buildQuery('SELECT * FROM transactions', page, limit, sort, order);
getFromDBAndSend(LATEST_TRANSACTIONS_QUERY, res);
});
// Routes
app.get('/accounts', (req, res) => {
const LATEST_ACCOUNTS_QUERY = 'SELECT * FROM accounts ORDER BY timestamp DESC LIMIT 100';
getFromDBAndSend(LATEST_ACCOUNTS_QUERY, req, res);
});
app.get('/accounts/:address', (req, res) => {
const ACCOUNTS_ADDRESS_QUERY = `SELECT * FROM accounts WHERE address = '${req.params.address}'`;
getFromDBAndSend(ACCOUNTS_ADDRESS_QUERY, req, res);
});
app.get('/accounts/:address/balance', (req, res) => {
const ACCOUNTS_BALANCE_BY_ADDRESS_QUERY = `SELECT * FROM accounts WHERE address = '${req.params.address}'`;
getFromDBAndSend(ACCOUNTS_BALANCE_BY_ADDRESS_QUERY, req, res);
});
app.get('/blocks', (req, res) => {
const LATEST_BLOCKS_QUERY = 'SELECT * FROM blocks ORDER BY timestamp DESC LIMIT 100';
getFromDBAndSend(LATEST_BLOCKS_QUERY, req, res);
});
app.get('/blocks/:block_number', (req, res) => {
const BLOCK_BY_NUMBER_QUERY = `SELECT * FROM blocks WHERE block_number = '${req.params.block_number}';`
getFromDBAndSend(BLOCK_BY_NUMBER_QUERY, req, res);
});
app.get('/transactions', (req, res) => {
const LATEST_TRANSACTIONS_QUERY = 'SELECT * FROM transactions ORDER BY block_number DESC;';
getFromDBAndSend(LATEST_TRANSACTIONS_QUERY, req, res);
});
app.get('/transaction/:tx_hash', (req, res) => {
const TRANSACTION_BY_HASH_QUERY = `SELECT * FROM transactions WHERE tx_hash = '${req.params.tx_hash}'`;
getFromDBAndSend(TRANSACTION_BY_HASH_QUERY, req, res);
});
app.get('/transaction/:address', (req, res) => {
const TRANSACTION_BY_HASH_QUERY = `SELECT * FROM transactions WHERE tx_hash = '${req.params.tx_hash}'`;
getFromDBAndSend(TRANSACTION_BY_HASH_QUERY, req, res);
});
app.get('/transaction/:to_address', (req, res) => {
const INCOMING_TRANSACTION_BY_HASH_QUERY = `SELECT * FROM transactions WHERE to_address = '${req.params.to_address}'`;
getFromDBAndSend(INCOMING_TRANSACTION_BY_HASH_QUERY, req, res);
});
app.get('/transaction/:from_address', (req, res) => {
const OUTGOING_TRANSACTION_BY_HASH_QUERY = `SELECT * FROM transactions WHERE from_address = '${req.params.from_address}'`;
getFromDBAndSend(OUTGOING_TRANSACTION_BY_HASH_QUERY, req, res);
});
const path = require('path');
// Serve the HTML file
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});