/ pkg / web / websocket.go
websocket.go
 1  package web
 2  
 3  import (
 4  	"net/http"
 5  
 6  	dogeboxd "github.com/dogeorg/dogeboxd/pkg"
 7  	"golang.org/x/net/websocket"
 8  )
 9  
10  // Represents a websocket connection from a client
11  type WSCONN struct {
12  	WS   *websocket.Conn
13  	Stop chan bool
14  }
15  
16  func (t *WSCONN) IsClosed() bool {
17  	return t.Stop == nil
18  }
19  
20  func (t *WSCONN) Close() {
21  	if t.Stop != nil {
22  		close(t.Stop)
23  		t.Stop = nil
24  	}
25  }
26  
27  // Handle incomming websocket connections for general updates
28  func (t api) getUpdateSocket(w http.ResponseWriter, r *http.Request) {
29  	initialPayload := func() any {
30  		return dogeboxd.Change{ID: "internal", Error: "", Type: "bootstrap", Update: t.getRawBS()}
31  	}
32  	t.ws.GetWSHandler(initialPayload).ServeHTTP(w, r)
33  }
34  
35  // Handle incomming websocket connections for log output
36  func (t api) getLogSocket(w http.ResponseWriter, r *http.Request) {
37  	PupID := r.PathValue("PupID")
38  	wh, err := GetLogHandler(PupID, t.dbx)
39  	if err != nil {
40  		sendErrorResponse(w, http.StatusBadRequest, "Error establishing log channel")
41  		return
42  	}
43  	wh.ServeHTTP(w, r)
44  }