diff --git a/go.mod b/go.mod index b32ad86..71cce32 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.24.1 require ( 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/gommon v0.4.2 // indirect github.com/matoous/go-nanoid v1.5.1 // indirect diff --git a/go.sum b/go.sum index a8fa44b..cc77a08 100644 --- a/go.sum +++ b/go.sum @@ -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/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/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/go.mod h1:o90YNEeQWjDozo584l7AwhJMHN0bOC4tAfg+Xox9q5g= github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= diff --git a/main/main.go b/main/main.go index ad3bbf9..1da68d7 100644 --- a/main/main.go +++ b/main/main.go @@ -70,6 +70,10 @@ func main() { deviceApiHandler.SetRouter(webServer.Router()) deviceApiHandler.Initialize(&authenticator) + webSocketServer := server.WebsocketServer{} + webSocketServer.SetRouter(webServer.Router()) + webSocketServer.Initialize(&authenticator) + var wg sync.WaitGroup wg.Add(1) go func() { diff --git a/server/websocket_server.go b/server/websocket_server.go new file mode 100644 index 0000000..869bc01 --- /dev/null +++ b/server/websocket_server.go @@ -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 +}