blockchainIDsystem/peer/restRoutes.go
peer/restRoutes.goRaw
1package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "net/http"
7
8 "github.com/fatih/color"
9)
10
11type Routes []Route
12
13var routes = Routes{
14 Route{
15 "Index",
16 "GET",
17 "/",
18 Index,
19 },
20 Route{
21 "GetPeers",
22 "GET",
23 "/peers",
24 GetPeers,
25 },
26 Route{
27 "PostUser",
28 "POST",
29 "/register",
30 PostUser,
31 },
32}
33
34type Address struct {
35 Address string `json:"address"` //the pubK of the user, to perform logins
36}
37
38func Index(w http.ResponseWriter, r *http.Request) {
39 fmt.Fprintln(w, runningPeer.ID)
40}
41func GetPeers(w http.ResponseWriter, r *http.Request) {
42 jResp, err := json.Marshal(outcomingPeersList)
43 check(err)
44 fmt.Fprintln(w, string(jResp))
45}
46func PostUser(w http.ResponseWriter, r *http.Request) {
47
48 decoder := json.NewDecoder(r.Body)
49 var address Address
50 err := decoder.Decode(&address)
51 if err != nil {
52 panic(err)
53 }
54 defer r.Body.Close()
55 fmt.Println(address)
56 color.Blue(address.Address)
57
58 //TODO add the verification of the address, to decide if it's accepted to create a new Block
59 block := blockchain.createBlock(address)
60 blockchain.addBlock(block)
61
62 go propagateBlock(block)
63
64 jResp, err := json.Marshal(blockchain)
65 check(err)
66 fmt.Fprintln(w, string(jResp))
67}