-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
229 lines (176 loc) · 5.1 KB
/
main.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"math/big"
"net/http"
"time"
"github.com/grokify/mogo/fmt/fmtutil"
stackexchange "github.com/grokify/go-stackoverflow/client"
"github.com/grokify/go-stackoverflow/util"
"github.com/ethereum/go-ethereum/ethclient"
)
type Ether struct {
Transaction_Hash string
Transfer_Amount int
Action string
Time time.Time
}
type Total struct {
Total_amount int
}
func Running(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.Error(w, "404 not found. hi", http.StatusNotFound)
return
}
switch r.Method {
case "GET":
http.ServeFile(w, r, "views/index.html")
case "POST":
fmt.Fprintf(w, "Sorry, only GET methods are supported.")
default:
fmt.Fprintf(w, "Oops!")
}
}
func Ethereum_Transactions(w http.ResponseWriter, r *http.Request) {
array := [14]interface{}{}
var obj [13]Ether
var TA [1]Total
switch r.Method {
case "POST":
// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
var iter = 1
var sum = 0
address_ether := string(r.FormValue("Ether_address"))
//client, err := ethclient.Dial("http://127.0.0.1:7545")
client, err := ethclient.Dial(address_ether)
if err != nil {
//log.Fatal(err)
return
}
for i := 15; i > 2; i-- {
j := 15 - i
blockNumber := big.NewInt(int64(i))
block, err := client.BlockByNumber(context.Background(), blockNumber)
if err != nil {
log.Fatal(err)
}
var date = int64(block.Time())
unixTimeUTC := time.Unix(date, 0)
obj[j].Time = unixTimeUTC
fmt.Println(unixTimeUTC)
//fmt.Fprintf(w, "\n Transaction Time : %v " , unixTimeUTC)
if iter <= 4 || iter == 11 {
for _, tx := range block.Transactions() {
obj[j].Transaction_Hash = tx.Hash().Hex()
obj[j].Action = "Withdrawal"
fmt.Println(tx.Hash().Hex()) //
//fmt.Fprintf(w, "\n Transaction Hash : %s " , tx.Hash().Hex())
var data = tx.Data() // []
obj[j].Transfer_Amount = int(data[35])
fmt.Println("withdrawal :", data[35])
sum -= int(data[35])
//fmt.Fprintf(w, "\n withdrawal : %d " , data[35])
array[j] = obj[j]
}
} else if (iter > 4 && iter <= 10) || iter == 12 || iter == 13 {
for _, tx := range block.Transactions() {
obj[j].Transaction_Hash = tx.Hash().Hex()
obj[j].Action = "Deposit"
fmt.Println(tx.Hash().Hex()) //
//fmt.Fprintf(w, "\n Transaction Hash : %s " , tx.Hash().Hex())
var data = tx.Data() // []
obj[j].Transfer_Amount = int(data[35])
fmt.Println("Deposit: ", data[35])
sum += int(data[35])
//fmt.Fprintf(w, "\n Deposit : %d " , data[35])
array[j] = obj[j]
}
}
iter++
}
TA[0].Total_amount = sum
fmt.Println(" \nTotal Customer Balance = ", sum)
//fmt.Fprintf(w," \nTotal Customer Balance = %d", sum)
array[13] = TA[0]
temp1, _ := json.Marshal(array)
temp1, _ = json.MarshalIndent(array, "", " ")
w.Write(temp1)
fmt.Println(string(temp1))
default:
fmt.Fprintf(w, "Sorry, only GET and POST methods are supported.")
}
}
func Stackoverflow(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
apiClient := stackexchange.NewAPIClient(
stackexchange.NewConfiguration())
site := "stackoverflow"
//userId := "1908967"
userId := string(r.FormValue("Stack_id"))
history, err := util.GetReputationHistoryAll(apiClient, site, userId)
if err != nil {
log.Fatal(err)
}
fmtutil.PrintJSON(history)
fmt.Printf("COUNT [%v]\n", len(history.Items))
temp2, _ := json.Marshal(history)
temp2, _ = json.MarshalIndent(history, "", " ")
w.Write(temp2)
fmt.Println("DONE")
}
}
func Hyperledger_Transactions(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
address_hyper := string(r.FormValue("Hyper_address"))
bearer_temp := string(r.FormValue("Bearer"))
bearer := "Bearer" + " " + bearer_temp
client := &http.Client{}
req, err := http.NewRequest("GET", address_hyper, nil)
req.Header.Add("Authorization", bearer)
if err != nil {
log.Fatal(err)
}
resp, err := client.Do(req)
if err != nil {
log.Println("Error on response.\n[ERROR] -", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
w.Write(body)
}
}
func main() {
http.HandleFunc("/", Running)
http.HandleFunc("/api/Ethereum", Ethereum_Transactions)
http.HandleFunc("/api/Hyperledger", Hyperledger_Transactions)
http.HandleFunc("/api/Stackoverflow", Stackoverflow)
fmt.Printf("Starting server for HTTP POST...\n")
if err := http.ListenAndServe(":8081", nil); err != nil {
log.Fatal(err)
}
}