优化结构
This commit is contained in:
@@ -1,15 +1,18 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
gorillawebsocket "github.com/gorilla/websocket"
|
||||
melody "github.com/olahol/melody"
|
||||
"kra/internal/integration/runtimeconfig"
|
||||
platformws "kra/pkg/websocket"
|
||||
@@ -57,6 +60,71 @@ func storeConfig(store *runtimeconfig.Store) runtimeconfig.Config {
|
||||
return config
|
||||
}
|
||||
|
||||
// TestConfig performs a local WebSocket handshake using a temporary server
|
||||
// built from the candidate settings. It does not touch the live endpoint.
|
||||
func TestConfig(ctx context.Context, raw json.RawMessage) error {
|
||||
values := map[string]any{}
|
||||
if err := json.Unmarshal(raw, &values); err != nil {
|
||||
return fmt.Errorf("decode websocket configuration: %w", err)
|
||||
}
|
||||
path := text(values, "path")
|
||||
if path == "" {
|
||||
path = "/ws"
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") {
|
||||
path = "/" + path
|
||||
}
|
||||
temporary := platformws.New(platformws.Config{
|
||||
WriteWait: durationValue(values, "write_wait", 10*time.Second),
|
||||
PongWait: durationValue(values, "pong_wait", 60*time.Second),
|
||||
PingPeriod: durationValue(values, "ping_period", 54*time.Second),
|
||||
MaxMessageSize: int64Value(values, "max_message_size"),
|
||||
MessageBufferSize: int(intValue(values, "message_buffer_size")),
|
||||
ConcurrentMessageHandling: boolValue(values, "concurrent_message_handling"),
|
||||
AllowOrigins: stringList(values, "allow_origins"),
|
||||
})
|
||||
defer temporary.Close()
|
||||
httpServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != path {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err := temporary.HandleRequest(w, r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
}))
|
||||
defer httpServer.Close()
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
headers := http.Header{}
|
||||
origins := stringList(values, "allow_origins")
|
||||
if len(origins) > 0 {
|
||||
origin := origins[0]
|
||||
if origin == "*" {
|
||||
origin = "http://localhost"
|
||||
}
|
||||
headers.Set("Origin", origin)
|
||||
}
|
||||
wsURL := "ws" + strings.TrimPrefix(httpServer.URL, "http") + path
|
||||
connection, response, err := gorillawebsocket.DefaultDialer.DialContext(ctx, wsURL, headers)
|
||||
if response != nil && response.Body != nil {
|
||||
_ = response.Body.Close()
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("websocket handshake failed: %w", err)
|
||||
}
|
||||
if connection == nil {
|
||||
return errors.New("websocket handshake returned an empty connection")
|
||||
}
|
||||
return connection.Close()
|
||||
}
|
||||
|
||||
func (s *Server) apply(config runtimeconfig.Config) {
|
||||
if s == nil {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user