From 2098da3417695370598d21ae386be9af1fb90ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Mon, 10 Nov 2025 10:18:34 +0100 Subject: [PATCH 1/4] Implemented configurable debugging port, bumped to v2 --- main.go | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/main.go b/main.go index 336aa88..4c91f6b 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "embed" "encoding/json" "flag" + "fmt" "html/template" "io" "log" @@ -16,12 +17,12 @@ import ( "strings" ) -const VERSION = "v1" +const VERSION = "v2" var ( - flagVerbose bool - flagWsURL string - flagWatch string + flagVerbose bool + flagVersion bool + flagDebuggingPort int //go:embed static fs embed.FS @@ -29,10 +30,15 @@ var ( func main() { flag.BoolVar(&flagVerbose, "v", false, "verbose") - flag.StringVar(&flagWsURL, "ws", "ws://127.0.0.1:9222", "devtools url") - flag.StringVar(&flagWatch, "watch", "", "Files to watch") + flag.BoolVar(&flagVersion, "version", false, "Print version and exit") + flag.IntVar(&flagDebuggingPort, "port", 9222, "Chrome debugging port") flag.Parse() + if flagVersion { + fmt.Println(VERSION) + return + } + http.HandleFunc("/", pageIndex) http.HandleFunc("/css/main.css", pageCSS) @@ -46,7 +52,7 @@ func main() { http.ListenAndServe("[::]:5123", nil) } -func replaceTilde(str string) string {// {{{ +func replaceTilde(str string) string { // {{{ homedir, err := os.UserHomeDir() if err != nil { log.Println(err) @@ -58,7 +64,7 @@ func replaceTilde(str string) string {// {{{ } return str -}// }}} +} // }}} func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{ var tmpl *template.Template @@ -73,7 +79,7 @@ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{ } data := map[string]any{ - "Sites": sites, + "Sites": sites, "VERSION": VERSION, } tmpl.Execute(w, data) @@ -91,7 +97,7 @@ func pageCSS(w http.ResponseWriter, r *http.Request) { // {{{ } // }}} func actionSites(w http.ResponseWriter, r *http.Request) { // {{{ j, _ := json.Marshal(struct { - OK bool + OK bool Sites map[string]*Site }{ true, @@ -100,17 +106,17 @@ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{ w.Write(j) } // }}} -func actionBrowserStart(w http.ResponseWriter, r *http.Request) {// {{{ +func actionBrowserStart(w http.ResponseWriter, r *http.Request) { // {{{ body, _ := io.ReadAll(r.Body) userDataDir := replaceTilde(string(body)) - browser := exec.Command("google-chrome", "--user-data-dir="+string(userDataDir), "--remote-debugging-port=9222") + browser := exec.Command("google-chrome", "--user-data-dir="+string(userDataDir), fmt.Sprintf("--remote-debugging-port=%d", flagDebuggingPort)) err := browser.Start() if err != nil { log.Println(err) } else { go browser.Wait() } -}// }}} +} // }}} func actionStart(w http.ResponseWriter, r *http.Request) { // {{{ var req struct { URL string @@ -133,7 +139,8 @@ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{ var site Site expandedWatch := replaceTilde(req.Watch) - site, err = NewSite(flagWsURL, req.URL, expandedWatch) + wsURL := fmt.Sprintf("ws://127.0.0.1:%d", flagDebuggingPort) + site, err = NewSite(wsURL, req.URL, expandedWatch) if err != nil { log.Println(err) } From 9dafb5383dc13833e6153bb3a4a2015719cf2a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Wed, 12 Nov 2025 07:55:03 +0100 Subject: [PATCH 2/4] Use embedded index.html, bumped to v3 --- main.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 4c91f6b..4f3ea87 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( "strings" ) -const VERSION = "v2" +const VERSION = "v3" var ( flagVerbose bool @@ -69,10 +69,7 @@ func replaceTilde(str string) string { // {{{ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{ var tmpl *template.Template var err error - if false { - tmpl, err = template.ParseFS(fs, "static/html/index.html") - } - tmpl, err = template.ParseFiles("static/html/index.html") + tmpl, err = template.ParseFS(fs, "static/html/index.html") if err != nil { w.Write([]byte(err.Error())) return From 8bde331200c9a52dcfe611edb4f73a12f66343e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Wed, 12 Nov 2025 07:56:22 +0100 Subject: [PATCH 3/4] Show HTTP server error, bumped to v4 --- main.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 4f3ea87..9226e7c 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( "strings" ) -const VERSION = "v3" +const VERSION = "v4" var ( flagVerbose bool @@ -49,7 +49,11 @@ func main() { http.HandleFunc("/sites", actionSites) log.Println("Listen on [::]:5123") - http.ListenAndServe("[::]:5123", nil) + err := http.ListenAndServe("[::]:5123", nil) + if err != nil { + log.Println(err) + os.Exit(1) + } } func replaceTilde(str string) string { // {{{ From 40d529704299c8023695939760849421dee5f64d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Wed, 12 Nov 2025 07:57:57 +0100 Subject: [PATCH 4/4] And read CSS from embedded FS. Bumped to v5 --- main.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 9226e7c..54cd430 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ import ( "strings" ) -const VERSION = "v4" +const VERSION = "v5" var ( flagVerbose bool @@ -88,8 +88,7 @@ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{ func pageCSS(w http.ResponseWriter, r *http.Request) { // {{{ w.Header().Add("Content-Type", "text/css") - //data, err := fs.ReadFile("static/css/main.css") - data, err := os.ReadFile("static/css/main.css") + data, err := fs.ReadFile("static/css/main.css") if err != nil { w.Write([]byte(err.Error())) return