mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
refactor(display): use chrome devtools protocol (#38)
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
)
|
||||
|
||||
//go:embed all:static
|
||||
var staticDir embed.FS
|
||||
|
||||
// BuildDirFS contains the embedded dist directory files (without the "build" prefix)
|
||||
var StaticDirFS, _ = fs.Sub(staticDir, "static")
|
||||
+18
-160
@@ -1,53 +1,27 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
shared "plg-mudics/shared"
|
||||
"strings"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
"github.com/skip2/go-qrcode"
|
||||
|
||||
"plg-mudics/display/browser"
|
||||
"plg-mudics/display/pkg"
|
||||
)
|
||||
|
||||
var version string
|
||||
var sseConnection chan string
|
||||
|
||||
func StartWebServer(v string, port string) {
|
||||
version = v
|
||||
|
||||
func StartWebServer(port string) {
|
||||
e := echo.New()
|
||||
|
||||
e.GET("/", indexRoute)
|
||||
e.GET("/sse", sseRoute)
|
||||
e.GET("/splash", func(ctx echo.Context) error {
|
||||
html := shared.SplashScreenTemplate
|
||||
html = strings.ReplaceAll(html, "%%APP-VERSION%%", version)
|
||||
return ctx.HTML(http.StatusOK, html)
|
||||
})
|
||||
e.GET("/qr", qrRoute)
|
||||
|
||||
staticGroup := e.Group("/static")
|
||||
staticGroup.Use(middleware.StaticWithConfig(middleware.StaticConfig{
|
||||
Filesystem: http.FS(StaticDirFS),
|
||||
HTML5: true,
|
||||
}))
|
||||
|
||||
apiGroup := e.Group("/api")
|
||||
apiGroup.Use(middleware.CORS())
|
||||
apiGroup.GET("/ping", pingRoute)
|
||||
@@ -55,6 +29,7 @@ func StartWebServer(v string, port string) {
|
||||
apiGroup.PATCH("/keyboardInput", keyboardInputRoute)
|
||||
apiGroup.PATCH("/showHTML", showHTMLRoute)
|
||||
apiGroup.PATCH("/takeScreenshot", takeScreenshotRoute)
|
||||
apiGroup.PATCH("/openWebsite", openWebsiteRoute)
|
||||
|
||||
fileGroup := apiGroup.Group("/file")
|
||||
fileGroup.Use(extractFilePathMiddleware)
|
||||
@@ -69,85 +44,6 @@ func StartWebServer(v string, port string) {
|
||||
}
|
||||
}
|
||||
|
||||
func indexRoute(ctx echo.Context) error {
|
||||
return indexTemplate().Render(ctx.Request().Context(), ctx.Response().Writer)
|
||||
}
|
||||
|
||||
func sseRoute(ctx echo.Context) error {
|
||||
slog.Info("SSE client connected")
|
||||
|
||||
w := ctx.Response()
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
flusher, _ := w.Writer.(http.Flusher)
|
||||
|
||||
sseConnection = make(chan string)
|
||||
|
||||
// init display
|
||||
ip, err := pkg.GetDeviceIp()
|
||||
if err != nil {
|
||||
slog.Error("Failed to get device IP address", "error", err)
|
||||
}
|
||||
mac, err := pkg.GetDeviceMac()
|
||||
if err != nil {
|
||||
slog.Error("Failed to get device MAC address", "error", err)
|
||||
}
|
||||
showQR := !isPortFree(8080)
|
||||
var status bytes.Buffer
|
||||
deviceInfoTemplate(ip, mac, showQR).Render(context.Background(), &status)
|
||||
connectedEvent := Event{
|
||||
Data: status.Bytes(),
|
||||
}
|
||||
connectedEvent.MarshalTo(w)
|
||||
flusher.Flush()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Request().Context().Done():
|
||||
slog.Info("SSE client disconnected")
|
||||
sseConnection = nil
|
||||
return nil
|
||||
|
||||
case event := <-sseConnection:
|
||||
rawEvent := Event{
|
||||
Event: []byte(""),
|
||||
Data: []byte(event),
|
||||
}
|
||||
|
||||
if err := rawEvent.MarshalTo(w); err != nil {
|
||||
slog.Warn("Error writing to client", "error", err)
|
||||
return err
|
||||
}
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func qrRoute(c echo.Context) error {
|
||||
data := c.QueryParam("data")
|
||||
if data == "" {
|
||||
return c.String(http.StatusBadRequest, "missing data")
|
||||
}
|
||||
|
||||
qr, err := qrcode.New(data, qrcode.Medium)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "could not generate qr")
|
||||
}
|
||||
|
||||
qr.DisableBorder = true
|
||||
qr.ForegroundColor = color.RGBA{R: 0x1c, G: 0x19, B: 0x17, A: 0xff}
|
||||
qr.BackgroundColor = color.RGBA{R: 0xe7, G: 0xe5, B: 0xe4, A: 0xff}
|
||||
|
||||
png, err := qr.PNG(-1)
|
||||
if err != nil {
|
||||
return c.String(http.StatusInternalServerError, "could not encode png")
|
||||
}
|
||||
|
||||
return c.Blob(http.StatusOK, "image/png", png)
|
||||
}
|
||||
|
||||
func extractFilePathMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(ctx echo.Context) error {
|
||||
raw := ctx.Param("path")
|
||||
@@ -308,38 +204,7 @@ func openFileRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusNotFound, shared.ErrorResponse{Description: "File not found"})
|
||||
}
|
||||
|
||||
if sseConnection == nil {
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Cant connect to display browser client"})
|
||||
}
|
||||
|
||||
err = resetView()
|
||||
if err != nil {
|
||||
slog.Error("Failed to reset view", "error", err)
|
||||
}
|
||||
|
||||
mType, err := mimetype.DetectFile(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to detect mime type", "file", pathParam, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to detect file type"})
|
||||
}
|
||||
|
||||
switch mType.String() {
|
||||
case "video/mp4":
|
||||
var templateBuffer bytes.Buffer
|
||||
videoTemplate(pathParam).Render(context.Background(), &templateBuffer)
|
||||
|
||||
sseConnection <- templateBuffer.String()
|
||||
case "image/jpeg", "image/png", "image/gif":
|
||||
var templateBuffer bytes.Buffer
|
||||
imageTemplate(pathParam).Render(context.Background(), &templateBuffer)
|
||||
sseConnection <- templateBuffer.String()
|
||||
case "application/pdf", "application/vnd.openxmlformats-officedocument.presentationml.presentation", "application/vnd.oasis.opendocument.presentation":
|
||||
err = pkg.FileHandler.OpenFile(fullPath)
|
||||
default:
|
||||
slog.Info("Unsupported file type", "type", mType)
|
||||
return ctx.JSON(http.StatusUnsupportedMediaType, shared.ErrorResponse{Description: "Unsupported file type: " + mType.String()})
|
||||
}
|
||||
|
||||
err = pkg.OpenFile(fullPath)
|
||||
if err != nil {
|
||||
slog.Error("Failed to open file", "file", pathParam, "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open file"})
|
||||
@@ -358,13 +223,12 @@ func showHTMLRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription})
|
||||
}
|
||||
|
||||
err := resetView()
|
||||
err := pkg.ShowHTML(request.HTML)
|
||||
if err != nil {
|
||||
slog.Error("Failed to reset view", "error", err)
|
||||
slog.Error("Failed to open html", "error", err)
|
||||
return ctx.JSON(http.StatusInternalServerError, shared.ErrorResponse{Description: "Failed to open html"})
|
||||
}
|
||||
|
||||
sseConnection <- request.HTML
|
||||
|
||||
slog.Info("HTML content sent to client")
|
||||
return ctx.JSON(http.StatusOK, struct{}{})
|
||||
}
|
||||
@@ -372,7 +236,7 @@ func showHTMLRoute(ctx echo.Context) error {
|
||||
func pingRoute(ctx echo.Context) error {
|
||||
return ctx.JSON(http.StatusOK, struct {
|
||||
Version string `json:"version"`
|
||||
}{Version: version})
|
||||
}{Version: shared.Version})
|
||||
}
|
||||
|
||||
func takeScreenshotRoute(ctx echo.Context) error {
|
||||
@@ -415,24 +279,18 @@ func previewRoute(ctx echo.Context) error {
|
||||
return ctx.File(outputFilePath)
|
||||
}
|
||||
|
||||
// Reset previous file views so they dont collide with the new one
|
||||
func resetView() error {
|
||||
err := pkg.FileHandler.CloseRunningProgram()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to close running program: %w", err)
|
||||
func openWebsiteRoute(ctx echo.Context) error {
|
||||
var request struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
if err := ctx.Bind(&request); err != nil {
|
||||
slog.Error("Failed to parse website input", "error", err)
|
||||
return ctx.JSON(http.StatusBadRequest, shared.ErrorResponse{Description: shared.BadRequestDescription})
|
||||
}
|
||||
|
||||
sseConnection <- ""
|
||||
slog.Info("Opening url")
|
||||
|
||||
return nil
|
||||
}
|
||||
browser.Browser.OpenPage(request.URL)
|
||||
|
||||
func isPortFree(port int) bool {
|
||||
addr := fmt.Sprintf("127.0.0.1:%d", port)
|
||||
l, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_ = l.Close()
|
||||
return true
|
||||
return ctx.JSON(http.StatusOK, struct{}{})
|
||||
}
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
package web
|
||||
|
||||
templ indexTemplate() {
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<title>PLG MuDiCS Display</title>
|
||||
<script src="/static/htmx.min.js"></script>
|
||||
<script src="/static/htmx-ext-sse.min.js"></script>
|
||||
<style>
|
||||
:root {
|
||||
--background-color: black;
|
||||
--foreground-color: oklch(92.3% 0.003 48.717);
|
||||
--font-size: 5rem;
|
||||
}
|
||||
|
||||
body {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
/* centers horizontally */
|
||||
align-items: center;
|
||||
/* centers vertically */
|
||||
width: 100vw;
|
||||
/* Viewport width */
|
||||
height: 100vh;
|
||||
/* Viewport height */
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
overflow: hidden;
|
||||
background-color: var(--background-color);
|
||||
color: var(--foreground-color);
|
||||
font-family: ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';
|
||||
cursor: none;
|
||||
}
|
||||
|
||||
video,
|
||||
img,
|
||||
iframe {
|
||||
width: 100vw;
|
||||
/* Viewport width */
|
||||
height: 100vh;
|
||||
/* Viewport height */
|
||||
object-fit: contain;
|
||||
}
|
||||
|
||||
p,
|
||||
li {
|
||||
font-size: var(--font-size);
|
||||
line-height: 1.2;
|
||||
text-wrap: balance;
|
||||
max-width: 90vw;
|
||||
word-break: break-word;
|
||||
margin: 0 0 calc(var(--font-size) * 0.4) 0;
|
||||
}
|
||||
|
||||
ul,
|
||||
ol {
|
||||
padding-left: calc(var(--font-size)*1.5);
|
||||
}
|
||||
</style>
|
||||
<script>
|
||||
document.addEventListener('keydown', function (event) {
|
||||
if (event.code === 'Space') {
|
||||
event.preventDefault();
|
||||
var video = document.querySelector('video');
|
||||
if (video) {
|
||||
if (video.paused) {
|
||||
video.play();
|
||||
} else {
|
||||
video.pause();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</head>
|
||||
<bod>
|
||||
<div hx-get="/splash" hx-trigger="load"></div>
|
||||
<main hx-ext="sse" sse-connect="/sse" sse-swap="message"></main>
|
||||
</bod>
|
||||
</html>
|
||||
}
|
||||
|
||||
templ videoTemplate(path string) {
|
||||
<video autoplay>
|
||||
<source src={ "/api/file/" + path } type="video/mp4"/>
|
||||
</video>
|
||||
}
|
||||
|
||||
templ imageTemplate(path string) {
|
||||
<img src={ "/api/file/" + path }/>
|
||||
}
|
||||
|
||||
templ deviceInfoTemplate(ip string, mac string, showQR bool) {
|
||||
<div style="width: 100vw; height: 100vh; display: flex; flex-direction: row; justify-content: space-between;">
|
||||
<div
|
||||
style="display: flex; flex-direction: column; gap: 1rem; justify-content: end; padding: 2rem; font-size: 4rem;"
|
||||
>
|
||||
{ ip }
|
||||
<span style="text-transform: uppercase;">{ mac }</span>
|
||||
</div>
|
||||
if showQR {
|
||||
<div style="display: flex; justify-content: end; align-items: end; padding: 2rem;">
|
||||
<div style="padding: 1rem; background-color: var(--foreground-color); border-radius: 1rem;">
|
||||
<img
|
||||
style="height: 30vh; width: auto; image-rendering: pixelated; image-rendering: crisp-edges;"
|
||||
src={ "/qr?data=http://" + ip + ":8080" }
|
||||
alt="QR-Code"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
<style>
|
||||
:root {
|
||||
--splash-bg: transparent !important;
|
||||
--splash-fade-out-state: paused !important;
|
||||
--background-color: oklch(21.6% 0.006 56.043);
|
||||
}
|
||||
</style>
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
// Event represents Server-Sent Event.
|
||||
// SSE explanation: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#event_stream_format
|
||||
type Event struct {
|
||||
// ID is used to set the EventSource object's last event ID value.
|
||||
ID []byte
|
||||
// Data field is for the message. When the EventSource receives multiple consecutive lines
|
||||
// that begin with data:, it concatenates them, inserting a newline character between each one.
|
||||
// Trailing newlines are removed.
|
||||
Data []byte
|
||||
// Event is a string identifying the type of event described. If this is specified, an event
|
||||
// will be dispatched on the browser to the listener for the specified event name; the website
|
||||
// source code should use addEventListener() to listen for named events. The onmessage handler
|
||||
// is called if no event name is specified for a message.
|
||||
Event []byte
|
||||
// Retry is the reconnection time. If the connection to the server is lost, the browser will
|
||||
// wait for the specified time before attempting to reconnect. This must be an integer, specifying
|
||||
// the reconnection time in milliseconds. If a non-integer value is specified, the field is ignored.
|
||||
Retry []byte
|
||||
// Comment line can be used to prevent connections from timing out; a server can send a comment
|
||||
// periodically to keep the connection alive.
|
||||
Comment []byte
|
||||
}
|
||||
|
||||
// MarshalTo marshals Event to given Writer
|
||||
func (ev *Event) MarshalTo(w io.Writer) error {
|
||||
// Marshalling part is taken from: https://github.com/r3labs/sse/blob/c6d5381ee3ca63828b321c16baa008fd6c0b4564/http.go#L16
|
||||
if len(ev.Data) == 0 && len(ev.Comment) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(ev.Data) > 0 {
|
||||
if _, err := fmt.Fprintf(w, "id: %s\n", ev.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sd := bytes.Split(ev.Data, []byte("\n"))
|
||||
for i := range sd {
|
||||
if _, err := fmt.Fprintf(w, "data: %s\n", sd[i]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(ev.Event) > 0 {
|
||||
if _, err := fmt.Fprintf(w, "event: %s\n", ev.Event); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(ev.Retry) > 0 {
|
||||
if _, err := fmt.Fprintf(w, "retry: %s\n", ev.Retry); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(ev.Comment) > 0 {
|
||||
if _, err := fmt.Fprintf(w, ": %s\n", ev.Comment); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := fmt.Fprint(w, "\n"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
-1
@@ -1 +0,0 @@
|
||||
(function(){var g;htmx.defineExtension("sse",{init:function(e){g=e;if(htmx.createEventSource==undefined){htmx.createEventSource=t}},getSelectors:function(){return["[sse-connect]","[data-sse-connect]","[sse-swap]","[data-sse-swap]"]},onEvent:function(e,t){var r=t.target||t.detail.elt;switch(e){case"htmx:beforeCleanupElement":var n=g.getInternalData(r);var s=n.sseEventSource;if(s){g.triggerEvent(r,"htmx:sseClose",{source:s,type:"nodeReplaced"});n.sseEventSource.close()}return;case"htmx:afterProcessNode":i(r)}}});function t(e){return new EventSource(e,{withCredentials:true})}function a(n){if(g.getAttributeValue(n,"sse-swap")){var s=g.getClosestMatch(n,v);if(s==null){return null}var e=g.getInternalData(s);var a=e.sseEventSource;var t=g.getAttributeValue(n,"sse-swap");var r=t.split(",");for(var i=0;i<r.length;i++){const u=r[i].trim();const c=function(e){if(l(s)){return}if(!g.bodyContains(n)){a.removeEventListener(u,c);return}if(!g.triggerEvent(n,"htmx:sseBeforeMessage",e)){return}f(n,e.data);g.triggerEvent(n,"htmx:sseMessage",e)};g.getInternalData(n).sseEventListener=c;a.addEventListener(u,c)}}if(g.getAttributeValue(n,"hx-trigger")){var s=g.getClosestMatch(n,v);if(s==null){return null}var e=g.getInternalData(s);var a=e.sseEventSource;var o=g.getTriggerSpecs(n);o.forEach(function(t){if(t.trigger.slice(0,4)!=="sse:"){return}var r=function(e){if(l(s)){return}if(!g.bodyContains(n)){a.removeEventListener(t.trigger.slice(4),r)}htmx.trigger(n,t.trigger,e);htmx.trigger(n,"htmx:sseMessage",e)};g.getInternalData(n).sseEventListener=r;a.addEventListener(t.trigger.slice(4),r)})}}function i(e,t){if(e==null){return null}if(g.getAttributeValue(e,"sse-connect")){var r=g.getAttributeValue(e,"sse-connect");if(r==null){return}n(e,r,t)}a(e)}function n(r,e,n){var s=htmx.createEventSource(e);s.onerror=function(e){g.triggerErrorEvent(r,"htmx:sseError",{error:e,source:s});if(l(r)){return}if(s.readyState===EventSource.CLOSED){n=n||0;n=Math.max(Math.min(n*2,128),1);var t=n*500;window.setTimeout(function(){i(r,n)},t)}};s.onopen=function(e){g.triggerEvent(r,"htmx:sseOpen",{source:s});if(n&&n>0){const t=r.querySelectorAll("[sse-swap], [data-sse-swap], [hx-trigger], [data-hx-trigger]");for(let e=0;e<t.length;e++){a(t[e])}n=0}};g.getInternalData(r).sseEventSource=s;var t=g.getAttributeValue(r,"sse-close");if(t){s.addEventListener(t,function(){g.triggerEvent(r,"htmx:sseClose",{source:s,type:"message"});s.close()})}}function l(e){if(!g.bodyContains(e)){var t=g.getInternalData(e).sseEventSource;if(t!=undefined){g.triggerEvent(e,"htmx:sseClose",{source:t,type:"nodeMissing"});t.close();return true}}return false}function f(t,r){g.withExtensions(t,function(e){r=e.transformResponse(r,null,t)});var e=g.getSwapSpecification(t);var n=g.getTarget(t);g.swap(n,r,e,{contextElement:t})}function v(e){return g.getInternalData(e).sseEventSource!=null}})();
|
||||
Vendored
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user