-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.go
96 lines (82 loc) · 2.36 KB
/
app.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
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/grrrben/glog"
)
type Server struct {
Server string
Port int
}
type Config struct {
Server Server
}
type App struct {
Router *mux.Router
DB *sql.DB
}
func (a *App) Initialize() {
fmt.Println("Initialising the blockchain")
name, err := os.Hostname()
if err != nil {
glog.Errorf("Could not get os.Hostname; %s", err)
return
}
// add the Node to the stack
nodes = initNodes()
cl := Node{
Protocol: "http://",
Hostname: name,
Port: nodePort,
Name: *nodeName,
}
cl.createWallet()
me = cl
// register me as the first node
nodes.addNode(&cl)
// fetch a list of existing Nodes
nodes.syncNodes()
// register me at all other Nodes
nodes.greetNodes()
bc = initBlockchain()
bc.getCurrentTransactions()
glog.Info("Starting with a base blockchain:")
glog.Infof("Blockchain:\n %v\n", bc)
glog.Flush()
a.Router = mux.NewRouter()
a.initializeRoutes()
}
func (a *App) Run() {
p := fmt.Sprintf("%d", nodePort)
fmt.Println("Starting server")
fmt.Printf("Running on Port %s\n", p)
log.Fatal(http.ListenAndServe(":"+p, a.Router))
}
func (a *App) initializeRoutes() {
a.Router.HandleFunc("/", a.index).Methods("GET")
// transactions
a.Router.HandleFunc("/transaction", a.newTransaction).Methods("POST")
a.Router.HandleFunc("/transaction/distributed", a.distributedTransaction).Methods("POST")
a.Router.HandleFunc("/transactions/{hash}", a.transactions).Methods("GET")
a.Router.HandleFunc("/transactions", a.currentTransactions).Methods("GET")
// wallet
a.Router.HandleFunc("/wallet/{hash}", a.wallet).Methods("GET")
// blocks
a.Router.HandleFunc("/block", a.lastblock).Methods("GET")
a.Router.HandleFunc("/block/{hash}", a.block).Methods("GET")
a.Router.HandleFunc("/block/index/{index}", a.blockByIndex).Methods("GET")
a.Router.HandleFunc("/block/distributed", a.distributedBlock).Methods("POST")
// mining and chaining
a.Router.HandleFunc("/mine", a.mine).Methods("GET")
a.Router.HandleFunc("/chain", a.chain).Methods("GET")
a.Router.HandleFunc("/validate", a.validate).Methods("GET")
a.Router.HandleFunc("/resolve", a.resolve).Methods("GET")
a.Router.HandleFunc("/status", a.chainStatus).Methods("GET")
// Nodes
a.Router.HandleFunc("/node", a.connectNode).Methods("POST")
a.Router.HandleFunc("/node", a.getNodes).Methods("GET")
}