From 7b2370a8fc9d1f8dbea71b35b449abde707a2d1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Sat, 2 Dec 2023 13:58:02 +0100 Subject: [PATCH] Update frontend --- .dockerignore | 2 ++ .gitmodules | 3 ++ Dockerfile | 13 ++++++- ministream-frontend | 1 + server/server.go | 9 +++++ server/static/index.html | 21 +++++------- server/static/script.js | 73 ---------------------------------------- server/static/style.css | 3 -- 8 files changed, 36 insertions(+), 89 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitmodules create mode 160000 ministream-frontend delete mode 100644 server/static/script.js delete mode 100644 server/static/style.css diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..82e6df7 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +**/node_modules/ +**/dist/ \ No newline at end of file diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3ecfdd1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "ministream-frontend"] + path = ministream-frontend + url = ssh://git@git.t-juice.club:222/torjus/ministream-frontend.git diff --git a/Dockerfile b/Dockerfile index 78098a3..9a53566 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,20 @@ +FROM node:latest as frontend-builder +WORKDIR /app +COPY ministream-frontend/package-lock.json /app/package-lock.json +COPY ministream-frontend/package.json /app/package.json +COPY ministream-frontend/tsconfig.json /app/tsconfig.json +RUN npm install +COPY ministream-frontend/src /app/src +RUN npm run-script build + FROM golang:alpine as builder WORKDIR /app COPY go.mod /app/go.mod COPY go.sum /app/go.sum RUN go mod download -COPY . /app +COPY --from=frontend-builder /app/dist /app/server/static +COPY main.go /app +COPY server /app/server RUN go build -o ministream main.go FROM alpine:latest diff --git a/ministream-frontend b/ministream-frontend new file mode 160000 index 0000000..e26f555 --- /dev/null +++ b/ministream-frontend @@ -0,0 +1 @@ +Subproject commit e26f5553007c43d87dd641149e1368e0360827c9 diff --git a/server/server.go b/server/server.go index 29b174e..7ce2f9f 100644 --- a/server/server.go +++ b/server/server.go @@ -31,6 +31,7 @@ func NewServer(store *UserStore) *Server { } r := chi.NewRouter() + r.Use(corsMiddleware) r.Get("/", srv.StaticHandler) r.Get("/{name}", srv.StaticHandler) r.Post("/whip", http.HandlerFunc(srv.WhipHandler)) @@ -45,6 +46,14 @@ func NewServer(store *UserStore) *Server { return srv } +func corsMiddleware(next http.Handler) http.Handler { + fn := func(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Access-Control-Allow-Origin", "*") + next.ServeHTTP(w, r) + } + return http.HandlerFunc(fn) +} + func (s *Server) OptionsHandler(w http.ResponseWriter, r *http.Request) { slog.Info("Got OPTIONS") } diff --git a/server/static/index.html b/server/static/index.html index 0ce8593..fd28b43 100644 --- a/server/static/index.html +++ b/server/static/index.html @@ -1,15 +1,12 @@ - + - + + - title - - - - -

HELLO!

-
    -
- - + ministream + + + +

Frontend not bundled correctly!

+ \ No newline at end of file diff --git a/server/static/script.js b/server/static/script.js deleted file mode 100644 index 84087d0..0000000 --- a/server/static/script.js +++ /dev/null @@ -1,73 +0,0 @@ -const startStream = function (streamKey) { - const pc = new RTCPeerConnection({ - iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] - }) - - pc.oniceconnectionstatechange = e => console.log(e) - pc.onicecandidate = e => { - console.log("Adding ice candidate: " + e.candidate); - if (!e.candidate) { - console.log("Done adding candidates. Creating offer."); - fetch("/whip/tjuice", { - method: "POST", - body: pc.localDescription.sdp - }).then(resp => { - resp.text().then(text => { - var answer = { - type: "answer", - sdp: text - } - try { - console.log("Setting remote description."); - pc.setRemoteDescription(answer) - } catch (e) { - console.log("Error setting remote description: " + e) - } - }) - }); - } - } - pc.createOffer().then(offer => { - console.log("Setting local description."); - pc.setLocalDescription(offer) - }) - pc.addTransceiver('video') - pc.addTransceiver('audio') - - pc.ontrack = function (event) { - console.log(event) - const el = document.getElementById('video1') - var ms = new MediaStream() - event.streams.forEach(s => { - const tracks = s.getTracks() - tracks.forEach(t => { - ms.addTrack(t) - }) - }) - - el.srcObject = ms - el.autoplay = true - el.controls = true - } -} - -displayStreams = function () { - fetch("/whip").then(resp => { - resp.json().then(streamList => { - for (const element of streamList) { - const ol = document.getElementById("streamList") - var entry = document.createElement("li") - var a = document.createElement("a") - a.onclick = _ => startStream(element) - a.append(document.createTextNode(element)) - entry.appendChild(a) - ol.appendChild(entry) - } - }) - }) -} - -window.onload = function (ev) { - console.log(ev) - displayStreams() -} \ No newline at end of file diff --git a/server/static/style.css b/server/static/style.css deleted file mode 100644 index 4f3cde2..0000000 --- a/server/static/style.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - color: #ff5722; -} \ No newline at end of file