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
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// An app to submit patches for the 30 GO GUI repos
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/user"
"time"
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/protobuf/virtpb"
"go.wit.com/log"
)
// refresh the windows & tables the user has open
func (admin *adminT) refresh() {
if argv.Verbose {
log.Info("virtigo scan here")
}
url := argv.Server
msg := []byte(`{"message": "Hello"}`)
// display the uptime
if data, err := postData(url+"/uptime", msg); err != nil {
log.Info("/uptime Error:", err)
} else {
log.Info("Response:", string(data))
admin.uptime.SetText(string(data))
}
// update the droplet list
if data, err := postData(url+"/DropletsPB", msg); err != nil {
log.Info("/DropletsPB Error:", err)
} else {
fmt.Println("DropletsPB Response len:", len(data))
admin.droplets = new(virtpb.Droplets)
if err := admin.droplets.Unmarshal(data); err != nil {
fmt.Println("droplets marshal failed", err)
return
}
fmt.Println("Droplet len=", admin.droplets.Len())
}
// update the hypervisor list
if data, err := postData(url+"/HypervisorsPB", msg); err != nil {
log.Info("Error:", err)
} else {
fmt.Println("HypervisorsPB Response len:", len(data))
admin.hypervisors = new(virtpb.Hypervisors)
if err := admin.hypervisors.Unmarshal(data); err != nil {
fmt.Println("hypervisors marshal failed", err)
return
}
fmt.Println("Hypervisors len=", admin.hypervisors.Len())
}
}
var client *http.Client
func (admin *adminT) doAdminGui() {
me.myGui = gui.New()
me.myGui.InitEmbed(resources)
me.myGui.Default()
// Initialize a persistent client with a custom Transport
client = &http.Client{
Transport: &http.Transport{
DisableKeepAlives: false, // Ensure Keep-Alive is enabled
},
Timeout: 10 * time.Second, // Set a reasonable timeout
}
win := gadgets.NewGenericWindow("Virtigo: (run your cluster)", "virtigo stuff")
win.Custom = func() {
log.Warn("Main window close")
os.Exit(0)
}
admin.uptime = win.Group.NewLabel("uptime")
grid := win.Group.RawGrid()
grid.NewButton("show hypervisors", func() {
if admin.hypervisors == nil {
log.Info("hypervisors not initialized")
return
}
log.Info("Hypervisors len=", admin.hypervisors.Len())
hwin := newHypervisorsWindow()
hwin.doStdHypervisors(admin.hypervisors)
hwin.win.Custom = func() {
log.Info("hiding table window")
}
})
grid.NewButton("show active droplets", func() {
if admin.droplets == nil {
log.Info("droplets not initialized")
return
}
var found *virtpb.Droplets
found = virtpb.NewDroplets()
all := admin.droplets.All()
for all.Scan() {
vm := all.Next()
if vm.Current.State != virtpb.DropletState_ON {
continue
}
found.Append(vm)
}
dropWin := newDropletsWindow()
dropWin.doActiveDroplets(found)
dropWin.win.Custom = func() {
log.Info("hiding droplet table window")
}
})
grid.NewButton("inactive droplets", func() {
if admin.droplets == nil {
log.Info("droplets not initialized")
return
}
var found *virtpb.Droplets
found = virtpb.NewDroplets()
all := admin.droplets.All()
for all.Scan() {
vm := all.Next()
if vm.Current.State == virtpb.DropletState_ON {
continue
}
found.Append(vm)
}
dropWin := newDropletsWindow()
dropWin.doInactiveDroplets(found)
dropWin.win.Custom = func() {
log.Info("hiding droplet table window")
}
})
grid.NextRow()
grid.NewButton("refresh", func() {
admin.refresh()
})
grid.NewButton("test gui close", func() {
me.myGui.Close()
// okExit("admin close")
})
// sit here forever refreshing the GUI
for {
admin.refresh()
time.Sleep(90 * time.Second)
}
}
func postData(url string, data []byte) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
usr, _ := user.Current()
req.Header.Set("author", usr.Username)
req.Header.Set("Connection", "keep-alive") // Ensure keep-alive is used
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response: %w", err)
}
return body, nil
}
/*
func main() {
url := "http://example.com/endpoint"
data := []byte(`{"message": "Hello"}`)
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
response, err := postData(url, data)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Response:", string(response))
}
}
}
*/
|