garritfra
6 years ago
8 changed files with 67 additions and 63 deletions
@ -1,47 +0,0 @@ |
|||||||
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) |
|
||||||
} |
|
@ -0,0 +1,7 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import "github.com/garritfra/blockchain-project/server" |
||||||
|
|
||||||
|
func main() { |
||||||
|
server.Start() |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
package server |
||||||
|
|
||||||
|
import ( |
||||||
|
"encoding/gob" |
||||||
|
"encoding/json" |
||||||
|
"net/http" |
||||||
|
|
||||||
|
core "github.com/garritfra/blockchain-project/core" |
||||||
|
) |
||||||
|
|
||||||
|
var blockchain core.Blockchain |
||||||
|
|
||||||
|
// Start starts the HTTP server on port 8080
|
||||||
|
func Start() { |
||||||
|
gob.Register(core.Block{}) |
||||||
|
gob.Register(core.Transaction{}) |
||||||
|
gob.Register(core.Blockchain{}) |
||||||
|
|
||||||
|
blockchain = core.NewBlockchain() |
||||||
|
block := core.Block{} |
||||||
|
|
||||||
|
transaction := core.Transaction{Sender: "foo", Receiver: "bar", Amount: 100} |
||||||
|
block.AddTransaction(transaction) |
||||||
|
|
||||||
|
go blockchain.AddBlock(block) |
||||||
|
|
||||||
|
block = core.Block{} |
||||||
|
transaction = core.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) |
||||||
|
} |
Loading…
Reference in new issue