Merge branch 'main' of ssh://git.gibonuddevalla.se:2222/go/webservice

This commit is contained in:
Magnus Åhall 2024-02-20 08:05:42 +01:00
commit ad1cb56a06
2 changed files with 8 additions and 0 deletions

View File

@ -274,6 +274,8 @@ func (db *T) CreateUser(username, password, name string) (userID int64, err erro
),
$3
)
ON CONFLICT (username) DO UPDATE
SET username = EXCLUDED.username
RETURNING id
`,
username,

View File

@ -10,6 +10,7 @@ import (
"net/http"
"slices"
"strings"
"sync"
)
type ReadHandler func(*ConnectionManager, *WsConnection, []byte)
@ -28,6 +29,7 @@ type ConnectionManager struct {
logger *slog.Logger
domains []string
readHandlers []ReadHandler
connSync sync.Mutex
}
type SendRequest struct {
WsConn *WsConnection
@ -67,7 +69,9 @@ func (cm *ConnectionManager) NewConnection(w http.ResponseWriter, r *http.Reques
}
// Keep track of all connections.
cm.connSync.Lock()
cm.connections[wsConn.UUID] = &wsConn
cm.connSync.Unlock()
// Successfully upgraded to a websocket connection.
cm.logger.Info("websocket", "uuid", wsConn.UUID, "remote_addr", r.RemoteAddr)
@ -97,7 +101,9 @@ func (cm *ConnectionManager) Prune(wsConn *WsConnection, err error) { // {{{
cm.logger.Info("websocket", "op", "prune", "uuid", wsConn.UUID)
wsConn.Conn.Close()
wsConn.Pruned = true
cm.connSync.Lock()
delete(cm.connections, wsConn.UUID)
cm.connSync.Unlock()
} // }}}
func (cm *ConnectionManager) ReadLoop(wsConn *WsConnection) { // {{{
var data []byte