-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
183 lines (140 loc) · 4.4 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"team-proflujo/rubixHCPMiddleware/globalVars"
"log"
"os"
)
func initApp() {
// Initialize Logging
globalVars.AppLogger = globalVars.AppLoggerStruct{
Info: log.New(os.Stdout, "INFO ", log.Ldate|log.Ltime),
Debug: log.New(os.Stdout, "DEBUG ", log.Ldate|log.Ltime),
Warning: log.New(os.Stdout, "WARNING ", log.Ldate|log.Ltime),
Error: log.New(os.Stdout, "ERROR ", log.Ldate|log.Ltime),
}
globalVars.AppLogger.Info.Println("Fetching Config Data...")
// Read Config Data
tempAppConfig, configError := getConfigData()
if configError != nil {
globalVars.AppLogger.Error.Println("Error while trying to get Config Data: " + configError.Error())
os.Exit(1)
}
if len(tempAppConfig.HcpAPIURL) == 0 {
globalVars.AppLogger.Error.Println("Unable to get the Config Data!")
os.Exit(1)
}
// Set Global config variable
globalVars.AppConfig = globalVars.ConfigDataStruct(tempAppConfig)
}
func readAppReqData(r *http.Request) (data []byte, err error) {
data, reqDataError := ioutil.ReadAll(r.Body)
if reqDataError != nil {
err = reqDataError
return
}
return
}
func respondJson(w http.ResponseWriter, statusCode int, response any) {
w.Header().Set("Content-Type", "application/json")
bytesJson, jsonEncodeError := json.Marshal(response)
if jsonEncodeError != nil {
// Fail when Unable to convert the Response to JSON
globalVars.AppLogger.Error.Println("Error when encoding Response Data: " + jsonEncodeError.Error())
w.WriteHeader(500)
w.Write([]byte("{\"success\": false, \"message\": \"Error occurred while trying to Respond.\"}"))
return
}
w.WriteHeader(statusCode)
w.Write(bytesJson)
}
func respondError(w http.ResponseWriter, statusCode int, message string) {
respondJson(w, statusCode, globalVars.APPHTTPResponse{
Success: false,
Message: message,
})
}
func handleRequests() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
respondJson(w, 200, globalVars.APPHTTPResponse{
Success: true,
Message: "Welcome to Rubix-HCP Vault middleware",
})
})
http.HandleFunc("/register", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondError(w, 405, "405 Method Not Allowed")
return
}
if len(globalVars.AppConfig.HcpAccessToken) == 0 {
respondError(w, 406, "Wallet has already been registered to HCP Vault!")
return
}
// Get Request Data
byteReqData, reqDataError := readAppReqData(r)
if reqDataError != nil {
respondError(w, 500, "Error while trying to read Request Data")
return
}
type RegisterWalletReqData struct {
Password string
}
var reqData RegisterWalletReqData
// Convert Request Data to Pre-defined format
reqJsonError := json.Unmarshal(byteReqData, &reqData)
reqData.Password = strings.TrimSpace(reqData.Password)
if reqJsonError != nil {
respondError(w, 400, "Invalid Request! Request data must be a valid JSON.")
return
} else if len(reqData.Password) == 0 {
respondError(w, 400, "Password must not be empty!")
return
}
// Register Wallet to HCP Vault
response := hcpRegisterWallet(reqData.Password)
if response.Success {
// Remove RegisterToken when Successfully Registered to HCP Vault
globalVars.AppConfig.HcpAccessToken = ""
// Update config.json with new Data
updateConfigData(globalVars.AppConfig)
}
respondJson(w, 200, response)
})
http.HandleFunc("/wallet-data", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
respondError(w, 405, "405 Method Not Allowed")
return
}
// Get Request Data
byteReqData, reqDataError := readAppReqData(r)
if reqDataError != nil {
respondError(w, 500, "Error while trying to read Request Data")
return
}
type WalletReqData struct {
Password string
}
var reqData WalletReqData
// Convert Request Data to Pre-defined format
reqJsonError := json.Unmarshal(byteReqData, &reqData)
reqData.Password = strings.TrimSpace(reqData.Password)
if reqJsonError != nil {
respondError(w, 400, "Invalid Request! Request data must be a valid JSON.")
return
} else if len(reqData.Password) == 0 {
respondError(w, 400, "Password must not be empty!")
return
}
// Get Wallet Data stored in HCP Vault
response := hcpGetWalletData(reqData.Password)
respondJson(w, 200, response)
})
http.ListenAndServe(":3333", nil)
}
func main() {
initApp()
handleRequests()
}