-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.go
More file actions
46 lines (37 loc) · 1.1 KB
/
websocket.go
File metadata and controls
46 lines (37 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// SPDX-License-Identifier: EUPL-1.2
package api
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
// defaultWSPath is the URL path where the WebSocket endpoint is mounted.
const defaultWSPath = "/ws"
// wrapWSHandler adapts a standard http.Handler to a Gin handler for the WebSocket route.
// The underlying handler is responsible for upgrading the connection to WebSocket.
func wrapWSHandler(h http.Handler) gin.HandlerFunc {
return func(c *gin.Context) {
h.ServeHTTP(c.Writer, c.Request)
}
}
// normaliseWSPath coerces custom WebSocket paths into a stable form.
// The path always begins with a single slash and never ends with one.
func normaliseWSPath(path string) string {
path = strings.TrimSpace(path)
if path == "" {
return defaultWSPath
}
path = "/" + strings.Trim(path, "/")
if path == "/" {
return defaultWSPath
}
return path
}
// resolveWSPath returns the configured WebSocket path or the default path
// when no override has been provided.
func resolveWSPath(path string) string {
if strings.TrimSpace(path) == "" {
return defaultWSPath
}
return normaliseWSPath(path)
}