arnaucube
arnaucube/bcPublic Self-hosted repository
bc/peer/restRoutes.go
peer/restRoutes.goRaw
  1package main
  2
  3import (
  4	"encoding/json"
  5	"fmt"
  6	"net/http"
  7
  8	blockchainlib "./blockchainlib"
  9	p2plib "./p2plib"
 10	"github.com/fatih/color"
 11	"github.com/gorilla/mux"
 12)
 13
 14type Routes []Route
 15
 16var routes = Routes{
 17	Route{
 18		"Index",
 19		"GET",
 20		"/",
 21		Index,
 22	},
 23	Route{
 24		"GetPeers",
 25		"GET",
 26		"/peers",
 27		GetPeers,
 28	},
 29	Route{
 30		"PostUser",
 31		"POST",
 32		"/register",
 33		PostUser,
 34	},
 35	Route{
 36		"GenesisBlock",
 37		"GET",
 38		"/blocks/genesis",
 39		GenesisBlock,
 40	},
 41	Route{
 42		"NextBlock",
 43		"GET",
 44		"/blocks/next/{blockhash}",
 45		NextBlock,
 46	},
 47	Route{
 48		"LastBlock",
 49		"GET",
 50		"/blocks/last",
 51		LastBlock,
 52	},
 53}
 54
 55type Address struct {
 56	Address string `json:"address"` //the pubK of the user, to perform logins
 57}
 58
 59func Index(w http.ResponseWriter, r *http.Request) {
 60	fmt.Fprintln(w, tp.ID)
 61}
 62func GetPeers(w http.ResponseWriter, r *http.Request) {
 63	jResp, err := json.Marshal(tp.PeersConnections.Outcoming)
 64	check(err)
 65	fmt.Fprintln(w, string(jResp))
 66}
 67func PostUser(w http.ResponseWriter, r *http.Request) {
 68
 69	decoder := json.NewDecoder(r.Body)
 70	var address string
 71	err := decoder.Decode(&address)
 72	if err != nil {
 73		panic(err)
 74	}
 75	defer r.Body.Close()
 76	fmt.Println(address)
 77	color.Blue(address)
 78
 79	//TODO add the verification of the address, to decide if it's accepted to create a new Block
 80	block := blockchain.CreateBlock(address)
 81	blockchain.AddBlock(block)
 82
 83	go PropagateBlock(block)
 84
 85	jResp, err := json.Marshal(blockchain)
 86	check(err)
 87	fmt.Fprintln(w, string(jResp))
 88}
 89
 90func PropagateBlock(b blockchainlib.Block) {
 91	//prepare the msg to send to all connected peers
 92	var msg p2plib.Msg
 93	msg.Construct("Block", "new block")
 94	bJson, err := json.Marshal(b)
 95	check(err)
 96
 97	msg.Data = []byte(bJson)
 98	msgB := msg.ToBytes()
 99	for _, peer := range tp.PeersConnections.Outcoming.Peers {
100		if peer.Conn != nil {
101			_, err := peer.Conn.Write(msgB)
102			check(err)
103		}
104	}
105}
106
107func GenesisBlock(w http.ResponseWriter, r *http.Request) {
108	var genesis blockchainlib.Block
109	if len(blockchain.Blocks) > 0 {
110		genesis = blockchain.Blocks[0]
111	}
112
113	jResp, err := json.Marshal(genesis)
114	check(err)
115	fmt.Fprintln(w, string(jResp))
116}
117
118func NextBlock(w http.ResponseWriter, r *http.Request) {
119	vars := mux.Vars(r)
120	blockhash := vars["blockhash"]
121
122	currBlock, err := blockchain.GetBlockByHash(blockhash)
123	check(err)
124	nextBlock, err := blockchain.GetBlockByHash(currBlock.NextHash)
125	check(err)
126
127	jResp, err := json.Marshal(nextBlock)
128	check(err)
129	fmt.Fprintln(w, string(jResp))
130}
131
132func LastBlock(w http.ResponseWriter, r *http.Request) {
133	var genesis blockchainlib.Block
134	if len(blockchain.Blocks) > 0 {
135		genesis = blockchain.Blocks[len(blockchain.Blocks)-1]
136	}
137
138	jResp, err := json.Marshal(genesis)
139	check(err)
140	fmt.Fprintln(w, string(jResp))
141}