cmd/wol-proxy: show a loading page for / (#381)

When requesting / wol-proxy will show a loading page that polls /status
every second. When the upstream server is ready the loading page will
refresh causing the actual root page to be displayed
This commit is contained in:
Benson Wong
2025-11-03 22:37:06 -05:00
committed by GitHub
parent b24467ab89
commit 6aedbe121a
3 changed files with 78 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"bufio"
"context"
_ "embed"
"errors"
"flag"
"fmt"
@@ -19,6 +20,9 @@ import (
"time"
)
//go:embed index.html
var loadingPageHTML string
var (
flagMac = flag.String("mac", "", "mac address to send WoL packet to")
flagUpstream = flag.String("upstream", "", "upstream proxy address to send requests to")
@@ -230,6 +234,15 @@ func (p *proxyServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if err := sendMagicPacket(*flagMac); err != nil {
slog.Warn("failed to send magic WoL packet", "error", err)
}
// For root path requests, return loading page with status polling
if path == "/" {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, loadingPageHTML)
return
}
ticker := time.NewTicker(250 * time.Millisecond)
timeout, cancel := context.WithTimeout(context.Background(), time.Duration(*flagTimeout)*time.Second)
defer cancel()