Skip to content

Commit

Permalink
feat(ui): add real-time updates #164 and refactor #126
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Enne authored and christophenne committed Feb 7, 2024
1 parent 56b8a84 commit af6019b
Show file tree
Hide file tree
Showing 13 changed files with 908 additions and 195 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fluxcd/helm-controller/api v0.37.4
github.com/fluxcd/source-controller/api v1.2.4
github.com/go-logr/logr v1.4.1
github.com/gorilla/websocket v1.5.0
github.com/invopop/jsonschema v0.12.0
github.com/onsi/ginkgo/v2 v2.15.0
github.com/onsi/gomega v1.31.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/google/pprof v0.0.0-20231229205709-960ae82b1e42 h1:dHLYa5D8/Ta0aLR2Xc
github.com/google/pprof v0.0.0-20231229205709-960ae82b1e42/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down
42 changes: 42 additions & 0 deletions internal/web/components/installbutton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package components

import (
"fmt"
"github.com/glasskube/glasskube/pkg/client"
"github.com/glasskube/glasskube/pkg/list"
"html/template"
"io"
"os"
)

func GetButtonId(pkgName string) string {
return fmt.Sprintf("install-%v", pkgName)
}

func GetSwap(buttonId string) string {
return fmt.Sprintf("outerHTML:#%s", buttonId)
}

func RenderInstallButton(w io.Writer, tmpl *template.Template, pkgName string, status *client.PackageStatus) {
buttonId := GetButtonId(pkgName)
err := tmpl.ExecuteTemplate(w, "installbutton", &map[string]any{
"ButtonId": buttonId,
"Swap": GetSwap(buttonId),
"PackageName": pkgName,
"Status": status,
})
if err != nil {
fmt.Fprintf(os.Stderr, "An error occurred rendering the install button for %v: \n%v\n"+
"This is most likely a BUG!", pkgName, err)
}
}

func ToInstallButtonInput(pkgTeaser list.PackageTeaserWithStatus) map[string]any {
buttonId := GetButtonId(pkgTeaser.PackageName)
return map[string]any{
"ButtonId": buttonId,
"Swap": GetSwap(buttonId),
"PackageName": pkgTeaser.PackageName,
"Status": pkgTeaser.Status,
}
}
45 changes: 45 additions & 0 deletions internal/web/hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package web

// Hub maintains the set of active clients and broadcasts messages to the clients.
type Hub struct {
// Inbound messages from the clients.
Broadcast chan []byte

// Register requests from the clients.
Register chan *WsClient

// Unregister requests from clients.
Unregister chan *WsClient

// Registered clients.
clients map[*WsClient]bool
}

// NewHub creates new Hub
func NewHub() *Hub {
return &Hub{
Broadcast: make(chan []byte),
Register: make(chan *WsClient),
Unregister: make(chan *WsClient),
clients: make(map[*WsClient]bool),
}
}

// Run handles communication operations with Hub
func (h *Hub) Run() {
for {
select {
case client := <-h.Register:
h.clients[client] = true
case client := <-h.Unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.Broadcast:
for client := range h.clients {
client.send <- message
}
}
}
}
1 change: 1 addition & 0 deletions internal/web/root/static/js/htmx-1.9.10.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit af6019b

Please sign in to comment.