You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

47 lines
961 B

package main
import (
"encoding/gob"
"encoding/json"
"net/http"
)
var blockchain Blockchain
// StartServer starts the HTTP server on port 8080
func StartServer() {
gob.Register(Block{})
gob.Register(Transaction{})
gob.Register(Blockchain{})
blockchain = NewBlockchain()
block := Block{}
transaction := Transaction{Sender: "foo", Receiver: "bar", Amount: 100}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
block = Block{}
transaction = Transaction{Sender: "bar", Receiver: "baz", Amount: 500}
block.AddTransaction(transaction)
go blockchain.AddBlock(block)
http.HandleFunc("/", redirect)
http.HandleFunc("/blockchain", listBlocks)
if err := http.ListenAndServe(":8080", nil); err != nil {
panic(err)
}
}
func redirect(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/blockchain", 301)
}
func listBlocks(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(blockchain.blocks)
}