feat: add websocket server

This commit is contained in:
Fritz Heiden 2025-03-30 16:25:56 +02:00
parent 7a4d9c73f5
commit e4384bdbfb
4 changed files with 52 additions and 0 deletions

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.24.1
require ( require (
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/labstack/echo/v4 v4.13.3 // indirect github.com/labstack/echo/v4 v4.13.3 // indirect
github.com/labstack/gommon v0.4.2 // indirect github.com/labstack/gommon v0.4.2 // indirect
github.com/matoous/go-nanoid v1.5.1 // indirect github.com/matoous/go-nanoid v1.5.1 // indirect

2
go.sum
View File

@ -2,6 +2,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY= github.com/labstack/echo/v4 v4.13.3 h1:pwhpCPrTl5qry5HRdM5FwdXnhXSLSY+WE+YQSeCaafY=
github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g= github.com/labstack/echo/v4 v4.13.3/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=

View File

@ -70,6 +70,10 @@ func main() {
deviceApiHandler.SetRouter(webServer.Router()) deviceApiHandler.SetRouter(webServer.Router())
deviceApiHandler.Initialize(&authenticator) deviceApiHandler.Initialize(&authenticator)
webSocketServer := server.WebsocketServer{}
webSocketServer.SetRouter(webServer.Router())
webSocketServer.Initialize(&authenticator)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(1) wg.Add(1)
go func() { go func() {

View File

@ -0,0 +1,45 @@
package server
import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v4"
)
var upgrader = websocket.Upgrader{}
type WebsocketServer struct {
router *echo.Echo
}
func (s *WebsocketServer) Initialize(authenticator *Authenticator) {
//s.router.Use(authenticator.Authenticate("/ws/terminal", []string{}))
s.router.GET("/ws", s.handle)
}
func (s *WebsocketServer) handle(context echo.Context) error {
//authContext := context.(AuthContext)
ws, err := upgrader.Upgrade(context.Response(), context.Request(), nil)
if err != nil {
return err
}
defer ws.Close()
for {
messageType, messageBytes, err := ws.ReadMessage()
if err != nil {
return err
}
if messageType == websocket.TextMessage {
var messageObject map[string]any
json.Unmarshal(messageBytes, &messageObject)
fmt.Println(messageObject)
}
}
}
func (s *WebsocketServer) SetRouter(router *echo.Echo) {
s.router = router
}