Skip to content

Commit

Permalink
Merge remote-tracking branch 'maximthomas/features/ws-request-path-co…
Browse files Browse the repository at this point in the history
…nfigurable' into frontend-path
  • Loading branch information
jmalloc committed Jun 30, 2024
2 parents 42e135e + 16409cc commit a6ec58c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ SEND_HEADER_ACCESS_CONTROL_ALLOW_METHODS="*"
SEND_HEADER_ACCESS_CONTROL_ALLOW_HEADERS="*"
```

### Web Socket Test Path

Set the `FRONTEND_WS_PATH` environment variable to send web socket request to the particular path
instead of root

## Running the server

The examples below show a few different ways of running the server with the HTTP
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
package main

var websocketHTML = `
<html>
<head>
<title>websocket</title>
Expand Down Expand Up @@ -142,9 +139,9 @@
cancelBtn.className = '';

ws = new WebSocket(
location.protocol === 'https:'
(location.protocol === 'https:'
? 'wss://' + window.location.host
: 'ws://' + window.location.host
: 'ws://' + window.location.host) + '{{.Path}}'
);

ws.onopen = function (ev) {
Expand Down Expand Up @@ -290,4 +287,3 @@
</script>
</body>
</html>
`
30 changes: 27 additions & 3 deletions cmd/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"embed"
"encoding/hex"
"fmt"
"io"
Expand All @@ -10,6 +11,7 @@ import (
"sort"
"strconv"
"strings"
"text/template"
"time"

"github.com/gorilla/websocket"
Expand Down Expand Up @@ -100,9 +102,7 @@ func handler(wr http.ResponseWriter, req *http.Request) {
if websocket.IsWebSocketUpgrade(req) {
serveWebSocket(wr, req, sendServerHostname)
} else if req.URL.Path == "/.ws" {
wr.Header().Add("Content-Type", "text/html")
wr.WriteHeader(200)
io.WriteString(wr, websocketHTML) // nolint:errcheck
serveFrontend(wr)
} else if req.URL.Path == "/.sse" {
serveSSE(wr, req, sendServerHostname)
} else {
Expand Down Expand Up @@ -159,6 +159,30 @@ func serveWebSocket(wr http.ResponseWriter, req *http.Request, sendServerHostnam
}
}

//go:embed "html"
var files embed.FS

func serveFrontend(wr http.ResponseWriter) {
const templateName = "html/frontend.tmpl.html"
tmpl, err := template.ParseFS(files, templateName)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
templateData := struct {
Path string
}{
Path: os.Getenv("FRONTEND_WS_PATH"),
}
err = tmpl.Execute(wr, templateData)
if err != nil {
http.Error(wr, err.Error(), http.StatusInternalServerError)
return
}
wr.Header().Add("Content-Type", "text/html")
wr.WriteHeader(200)
}

func serveHTTP(wr http.ResponseWriter, req *http.Request, sendServerHostname bool) {
wr.Header().Add("Content-Type", "text/plain")
wr.WriteHeader(200)
Expand Down

0 comments on commit a6ec58c

Please sign in to comment.