// 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()) } // update the events list if data, err := postData(url+"/EventsPB", msg); err != nil { log.Info("Error:", err) } else { fmt.Println("EventsPB Response len:", len(data)) admin.events = new(virtpb.Events) if err := admin.events.Unmarshal(data); err != nil { fmt.Println("events marshal failed", err) return } fmt.Println("Events len=", admin.events.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.NewButton("events", func() { if admin.events == nil { log.Info("events are not initialized") return } log.Info("Events len=", admin.events.Len()) hwin := newEventsWindow() hwin.doStdEvents(admin.events) hwin.win.Custom = func() { log.Info("hiding 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)) } } } */