-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
220 lines (183 loc) · 4.95 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"bytes"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
)
var Count int
var Token string
//struct for our vm, you can check here https://mholt.github.io/json-to-go/ for convert json to struct of golang
type VMs []struct {
ID int `json:"id"`
Name string `json:"name"`
RAM int `json:"ram"`
CPU float64 `json:"cpu"`
Ssd int `json:"ssd"`
Sata string `json:"sata"`
}
func preparation(){
client := &http.Client{}
URL := "https://t3.linxdatacenter.com/api/v1/auth/" //http of auth
resp, _ := http.NewRequest("GET", URL, nil)
resp.SetBasicAuth("test3", "test88") //basic auth
res, _ := client.Do(resp)
//take token
body, err := ioutil.ReadAll(res.Body)
Token = string(body)
if err != nil {
log.Fatal("Error reading body. ", err)
}
fmt.Printf("%s\n", body)
//--------------------------------------
//take information(json) about virtuals mashines
URLvm := "https://t3.linxdatacenter.com/api/v1/vm/"
client2 := &http.Client{}
site, _ := http.NewRequest("GET", URLvm, nil)
site.Header.Add("x-auth", string(body)) //add token for API
siteIN, _ := client2.Do(site)
body2, err := ioutil.ReadAll(siteIN.Body)
if err != nil {
log.Fatal("Error reading body. ", err)
}
fmt.Printf("string that have after Request: %s\n", body2)
json_parse(string(body2), &ParseVM)
fmt.Println("ParseVM без указателя:", ParseVM)
fmt.Println("ParseVM c указателя:", *ParseVM)
fmt.Println("ParseVM если пройтись range:")
for k, v := range *ParseVM {
fmt.Print(k)
fmt.Print(" : ")
fmt.Println(v)
}
}
type SimpleVM struct {
ID int `json:"id"`
Name string `json:"name"`
RAM int `json:"ram"`
CPU float64 `json:"cpu"`
Ssd int `json:"ssd"`
Sata string `json:"sata"`
}
//Просто ф-я для более удобного парсинга
func json_parse(Data string, obj interface{}) {
var b_Data = []byte(Data)
err := json.Unmarshal(b_Data, obj)
if err != nil {
log.Println("error:", err)
}
}
/*func CreateVMs(Data string) *VMs{
var var_VMs = &VMs{}
json_parse(Data, var_VMs)
fmt.Println(var_VMs)
return var_VMs
}*/
var ParseVM = &VMs{}
func indexHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/index.html", "templates/header.html", "templates/footer.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "index", *ParseVM)
}
func writeHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/write.html", "templates/header.html", "templates/footer.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
t.ExecuteTemplate(w, "write", nil)
}
func StrToInt(str string) (int, error) { //
nonFractionalPart := strings.Split(str, ".")
return strconv.Atoi(nonFractionalPart[0])
}
func SaveInAPI() {
URLvm := "https://t3.linxdatacenter.com/api/v1/vm/"
client2 := &http.Client{}
j, _ := json.Marshal(*ParseVM)
site, _ := http.NewRequest("POST", URLvm, bytes.NewBuffer(j))
site.Header.Add("x-auth", string(Token)) //add token for API
_, _ = client2.Do(site)
}
func savePostHandler(w http.ResponseWriter, r *http.Request) {
id := r.FormValue("id")
//fmt.Println(id," is id that save")
name := r.FormValue("name")
ram, _ := StrToInt(r.FormValue("ram"))
cpu, _ := strconv.ParseFloat(r.FormValue("cpu"), 64)
ssd, _ := StrToInt(r.FormValue("ssd"))
sata := r.FormValue("sata")
//fmt.Println(name," ", ram," ", cpu," ", ssd," ", sata)
if id != "" {
fmt.Println("here")
idi, _ := StrToInt(id)
NewVM := &VMs{{
ID: idi,
Name: name,
RAM: ram,
CPU: cpu,
Ssd: ssd,
Sata: sata,
}}
URLvm := "https://t3.linxdatacenter.com/api/v1/vm/"
client2 := &http.Client{}
j, _ := json.Marshal(*NewVM)
fmt.Println("bytes.NewBuffer(j", bytes.NewBuffer(j))
site, _ := http.NewRequest("POST", URLvm, bytes.NewBuffer(j))
site.Header.Add("x-auth", string(Token)) //add token for API
_, _ = client2.Do(site)
preparation()
} else {
Count++
NewVM := SimpleVM{
ID: Count,
Name: name,
RAM: ram,
CPU: cpu,
Ssd: ssd,
Sata: sata,
}
fmt.Println(NewVM)
m := &VMs{}
*m = append(*ParseVM, NewVM)
ParseVM = m
SaveInAPI()
}
http.Redirect(w, r, "/", 302)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("templates/write.html", "templates/header.html", "templates/footer.html")
if err != nil {
fmt.Fprintf(w, err.Error())
return
}
id, _ := StrToInt(r.FormValue("id"))
M := SimpleVM{}
for _, v := range *ParseVM {
if v.ID == id {
M = v
}
}
fmt.Println(M)
t.ExecuteTemplate(w, "write", M)
}
func main() {
preparation()
Count = len(*ParseVM)
fmt.Println(Count)
//client for take the token for API
fmt.Println("listening on port :3000")
http.HandleFunc("/", indexHandler)
http.HandleFunc("/write", writeHandler)
http.HandleFunc("/edit", editHandler)
http.HandleFunc("/SavePost", savePostHandler)
http.ListenAndServe(":3000", nil)
}