RAW_WS_PROXY_URL=http://localhost:17071, RAW_WS=8080 to use web

master
Bel LaPointe 2023-04-09 11:53:21 -06:00
parent f3cbfa1c48
commit 39f6bc8ed9
3 changed files with 37 additions and 20 deletions

View File

@ -1,27 +1,20 @@
feedback: feedback:
addr: :17071 addr: :17071
ttsurl: http://localhost:15002 ttsurl: http://localhost:15002
broadcast:
message: hi
users: users:
bel: bel:
meta:
lasttsms: 1681062770999
lastlag: 12
state: state:
player: 0 player: 0
message: "hi" message: hi
alias: driver gm:
meta: alias: ""
tsms: 1 lastalias: ""
lastlag: 2 vote: ""
players: players:
- buttons: - transformation: {}
up: "w"
down: "s"
left: "a"
right: "d"
l: "q"
r: "e"
a: "1"
b: "2"
x: "3"
y: "4"
quiet: false quiet: false
broadcast:
message: hi

View File

@ -27,10 +27,10 @@
function noopcallback(responseBody, responseStatus) { function noopcallback(responseBody, responseStatus) {
} }
setInterval(() => { setInterval(() => {
http("GET", `/proxy?user=${document.getElementById("user")}`, (b, s) => { http("GET", `/proxy?user=${document.getElementById("user").value}`, (b, s) => {
if (s != 200) if (s != 200)
return return
document.getElementById("ntfy").innerHTML = b.replace("\n", "<br>") document.getElementById("ntfy").innerHTML = b.replaceAll("\n", "<br>")
}, null) }, null)
}, 1500) }, 1500)
</script> </script>

View File

@ -6,11 +6,18 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/http/httputil"
"net/url"
"os" "os"
"strings"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var (
FlagWSProxy = os.Getenv("RAW_WS_PROXY_URL")
)
type WS struct { type WS struct {
ctx context.Context ctx context.Context
can context.CancelFunc can context.CancelFunc
@ -72,10 +79,27 @@ func (ws WS) serveHTTP(w http.ResponseWriter, r *http.Request) error {
case "/api/ws": case "/api/ws":
return ws.serveWS(w, r) return ws.serveWS(w, r)
} }
if strings.HasPrefix(r.URL.Path, "/proxy") {
return ws.serveProxy(w, r)
}
http.NotFound(w, r) http.NotFound(w, r)
return nil return nil
} }
func (ws WS) serveProxy(w http.ResponseWriter, r *http.Request) error {
u, err := url.Parse(FlagWSProxy)
if err != nil {
return err
}
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/proxy")
if r.URL.Path == "" {
r.URL.Path = "/"
}
proxy := httputil.NewSingleHostReverseProxy(u)
proxy.ServeHTTP(w, r)
return nil
}
//go:embed public/root.html //go:embed public/root.html
var rootHTML string var rootHTML string