quality of life, updated config structure
This commit is contained in:
36
ws/util.go
36
ws/util.go
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -99,16 +98,26 @@ func (wsg *WebsocketGateway) validateApiKey(r *http.Request) bool {
|
||||
return !(apiKey == "" || apiKey != wsg.apiKey)
|
||||
}
|
||||
|
||||
func loggingMiddleware(logger *slog.Logger, next http.Handler) http.Handler {
|
||||
func (wsg *WebsocketGateway) loggingMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
next.ServeHTTP(w, r)
|
||||
logger.Info("Incoming HTTP request.", "remote", r.RemoteAddr, "path", r.URL.Path, "duration", time.Since(start))
|
||||
wsg.logger.Info("Incoming HTTP request.", "remote", r.RemoteAddr, "path", r.URL.Path, "duration", time.Since(start))
|
||||
})
|
||||
}
|
||||
|
||||
// connections
|
||||
|
||||
func closeConn(conn *websocket.Conn) {
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(time.Second))
|
||||
_ = conn.WriteControl(
|
||||
websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "Disconnecting."),
|
||||
time.Now().Add(time.Second),
|
||||
)
|
||||
_ = conn.Close()
|
||||
}
|
||||
|
||||
func (wsg *WebsocketGateway) registerConn(conn *websocket.Conn, typ, channelId, serverId string) bool {
|
||||
if typ == "bot" {
|
||||
wsg.registry.botMu.Lock()
|
||||
@@ -140,20 +149,23 @@ func (wsg *WebsocketGateway) closeAll() {
|
||||
|
||||
wsg.registry.UnregisterBot()
|
||||
|
||||
wsg.registry.mu.RLock()
|
||||
channelIDs := make([]string, 0, len(wsg.registry.entries))
|
||||
for channelID := range wsg.registry.entries {
|
||||
channelIDs = append(channelIDs, channelID)
|
||||
}
|
||||
wsg.registry.mu.RUnlock()
|
||||
|
||||
for _, channelID := range channelIDs {
|
||||
wsg.registry.ForEach(func(channelID string) {
|
||||
wsg.registry.UnregisterMod(channelID)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
func NewUpgrader() websocket.Upgrader {
|
||||
return websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true // local by default; change for production
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewRegistry(queueCap int) *Registry {
|
||||
return &Registry{
|
||||
entries: make(map[string]*ChannelEntry),
|
||||
|
||||
Reference in New Issue
Block a user