-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
126 lines (109 loc) · 3.37 KB
/
main.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/servntire/servntire-demo/blockchain"
"github.com/servntire/servntire-demo/web"
"github.com/servntire/servntire-demo/web/controllers"
)
// Fix empty GOPATH with golang 1.8 (see https://github.com/golang/go/blob/1363eeba6589fca217e155c829b2a7c00bc32a92/src/go/build/build.go#L260-L277)
func defaultGOPATH() string {
env := "HOME"
if runtime.GOOS == "windows" {
env = "USERPROFILE"
} else if runtime.GOOS == "plan9" {
env = "home"
}
if home := os.Getenv(env); home != "" {
def := filepath.Join(home, "go")
if filepath.Clean(def) == filepath.Clean(runtime.GOROOT()) {
// Don't set the default GOPATH to GOROOT,
// as that will trigger warnings from the go tool.
return ""
}
return def
}
return ""
}
func main() {
// Setup correctly the GOPATH in the environment
if goPath := os.Getenv("GOPATH"); goPath == "" {
os.Setenv("GOPATH", defaultGOPATH())
}
// Initialize the Fabric SDK
fabricSdk, err := blockchain.Initialize()
if err != nil {
fmt.Printf("Unable to initialize the Fabric SDK: %v", err)
}
// Install and instantiate the chaincode
err = fabricSdk.InstallAndInstantiateCC()
if err != nil {
fmt.Printf("Unable to install and instantiate the chaincode: %v\n", err)
}
// Query the chaincode
fmt.Println("###### Query All ######")
response, err := fabricSdk.QueryAll()
if err != nil {
fmt.Printf("Unable to query all records from the chaincode: %v\n", err)
} else {
fmt.Printf("Response from the queryAll: %v\n", response)
}
fmt.Println("###### Query One ######")
response, _, err = fabricSdk.QueryOne("CAR4")
if err != nil {
fmt.Printf("Unable to query one from chaincode: %v\n", err)
} else {
fmt.Printf("Response from the queryOne: %v\n", response)
}
// Create New Car
type Car struct {
Make string `json:"make"`
Model string `json:"model"`
Colour string `json:"colour"`
Owner string `json:"owner"`
}
carData := Car{}
carKey := "CAR10"
carData.Make = "Volkswagen"
carData.Model = "Vento"
carData.Colour = "grey"
carData.Owner = "Mohan"
RequestData, _ := json.Marshal(carData)
txId, err := fabricSdk.CreateCar(carKey, string(RequestData))
if err != nil {
fmt.Printf("Unable to create record on the chaincode: %v\n", err)
} else {
fmt.Printf("Successfully created record, transaction ID: %s\n", txId)
}
// Query the chaincode Again to retrieve updated data
fmt.Println("###### Query All ######")
response, err = fabricSdk.QueryAll()
if err != nil {
fmt.Printf("Unable to query all from the chaincode: %v\n", err)
} else {
fmt.Printf("Response from the queryAll: %v\n", response)
}
// Changing Car Owner by Passing Key and Value
fmt.Println("###### Change Ownership ######")
txId, err = fabricSdk.ChangeCarOwner("CAR10", "Keyana")
if err != nil {
fmt.Printf("Unable to invoke - Change Car Owner on the chaincode: %v\n", err)
} else {
fmt.Printf("Successfully invoke - Change Car Owner, transaction ID: %s\n", txId)
}
// Retrieving History of a Record
response, err = fabricSdk.GetHistoryofCar("CAR10")
if err != nil {
fmt.Printf("Unable to query to retrieve history of record from the chaincode: %v\n", err)
} else {
fmt.Printf("Response from the Chain for history of a record: %s\n", response)
}
// Make the web application listening
app := &controllers.Application{
Fabric: fabricSdk,
}
web.Serve(app)
}