temp push

This commit is contained in:
2025-12-01 13:40:58 +01:00
parent aac6db39c2
commit 4a668493c4
11 changed files with 559 additions and 116 deletions

View File

@@ -2,7 +2,6 @@ package ws
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
@@ -13,30 +12,11 @@ import (
"github.com/gorilla/websocket"
)
func (g *WebsocketGateway) StartGatewayWithForwarder() {
go func() {
for out := range g.Outgoing() {
b, _ := json.Marshal(out)
// use Info because these are normal forward events
g.logger.Info("forwarder -> bot", "msg", string(b))
}
}()
func (g *WebsocketGateway) Start() error {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
if err := g.Serve(ctx, fmt.Sprintf(":%d", g.port)); err != nil {
g.logger.Error("gateway serve error", err)
}
close(g.outgoingCh)
}
func (g *WebsocketGateway) Outgoing() <-chan GatewayMessageOut {
return g.outgoingCh
}
func (g *WebsocketGateway) OutgoingLen() int {
return len(g.outgoingCh)
return g.Serve(ctx, fmt.Sprintf(":%d", g.port))
}
// util
@@ -69,23 +49,23 @@ func loggingMiddleware(logger *slog.Logger, next http.Handler) http.Handler {
// connections
func (g *WebsocketGateway) registerConn(c *websocket.Conn) {
g.modConnsMu.Lock()
g.modConns[c] = struct{}{}
g.modConnsMu.Unlock()
func (g *WebsocketGateway) registerConn(c *websocket.Conn, meta connMetadata) {
g.connsMu.Lock()
g.conns[c] = meta
g.connsMu.Unlock()
}
func (g *WebsocketGateway) unregisterConn(c *websocket.Conn) {
g.modConnsMu.Lock()
delete(g.modConns, c)
g.modConnsMu.Unlock()
g.connsMu.Lock()
delete(g.conns, c)
g.connsMu.Unlock()
}
func (g *WebsocketGateway) closeAll() {
g.modConnsMu.Lock()
defer g.modConnsMu.Unlock()
g.connsMu.Lock()
defer g.connsMu.Unlock()
g.logger.Info("closing websocket connections")
for c := range g.modConns {
for c := range g.conns {
_ = c.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "shutting down"), time.Now().Add(time.Second))
_ = c.Close()
}