-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
112 lines (104 loc) · 4.24 KB
/
api.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
// TODO 這邊還要補齊 /v1/status,與測試程式
var express = require('express');
var transactionData = require('../db/models/transactionData.js');
var searchTransaction = require('../db/models/searchTransaction.js');
var producer = require('../kafka/producer/producer.js');
var Web3 = require('web3');
var web3 = new Web3();
var router = express.Router();
var kafkaTopic = 'InsertQueue';
web3.setProvider(new web3.providers.HttpProvider(process.env.ENODE_BASE || 'http://localhost:8545'));
var kafka = require('kafka-node'),
HighLevelProducer = kafka.HighLevelProducer,
client = new kafka.Client("127.0.0.1:2181"),
producer = new HighLevelProducer(client),
message = {};
/**
* 把基本服務的 API 都放在這邊
*/
/**
* PUT data
* 把資料存入,回傳 txHash
* Example:
* curl --request PUT "http://localhost:3000/api/v1/data" --data '{"data":"your data here"}' -H "Content-Type: application/json"
* @param data
*/
router.put('/v1/data', function(req, res, next) {
// Step 1: Insert TransactionData
if (req.body.data != null && req.body.data != '') {
transactionData.create({"data": req.body.data.toString()}).then(function (result) {
// 寫入 kafka
message[result] = req.body.data.toString();
var payloads = [
{topic: kafkaTopic, messages: JSON.stringify(message)}
];
// Step 2: Put to Kafka queue
// FIXME Kafka producer 要做 error handling,有錯要重送,這邊我測試如果沒有打開 Kafka 一樣會過
producer.send(payloads);
res.json({'data': {'txHash': result}});
}).catch(function (err) {
// error handle
console.log(err.message, err.stack);
res.json({'error': {'message': err.message}});
});
} else {
res.json({'error': {'message': 'invalid data'}});
}
});
/**
* Get All Data
* 拿table中全部的資料,初始化實用
*/
router.get('/v1/all', function(req, res, next) {
transactionData.readAll().then(function(result){
// console.log('typeof: ' + typeof result);
// console.log('object: ' + JSON.parse(JSON.stringify(result)));
res.json(result.rows);
})
.catch(function (err) {
console.log(err.message, err.stack);
res.json({'error': {'message': err.message}});
});
});
/**
* Get txHash information
* Example:
* curl --request GET "http://localhost:3000/api/v1/status?txHash=13600b294191fc92924bb3ce4b969c1e7e2bab8f4c93c3fc6d0a51733df3c060"
* @param txHash
*/
router.get('/v1/status', function(req, res, next) {
if (req.query.txHash != null && req.query.txHash != '') {
transactionData.read(req.query.txHash).then(function(result){
console.log(result);
// FIXME 這邊要看狀態把資料丟出去
var transaction = {txHash: req.query.txHash, fromAddress: web3.eth.coinbase};
if (result.rowCount > 0) {
//transaction[status] = result.rows[0].status;
//searchTransaction.create(transaction);
res.json({'data': {
'txHash': result.rows[0].txhash,
'status': result.rows[0].status,
'transactionTimestamp': result.rows[0].txtimestamp,
'updateTimestamp': result.rows[0].updatetimestamp,
'transactionHash': result.rows[0].transactionhash,
'dataHash': result.rows[0].datahash,
'blockNumber': result.rows[0].blocknumber,
'blockHash': result.rows[0].blockhash,
'gasUsed': result.rows[0].gas,
'network': result.rows[0].network
}
});
} else {
//transaction[status] = 'ERROR';
//searchTransaction.create(transaction);
res.json({'error': {'message': 'invalid txHash'}});
}
}).catch(function (err) {
console.log(err.message, err.stack);
res.json({'error': {'message': err.message}});
});
} else {
res.json({'error': {'message': 'invalid txHash'}});
}
});
module.exports = router;