Implemented configurable debugging port, bumped to v2

This commit is contained in:
Magnus Åhall 2025-11-10 10:18:34 +01:00
parent 5a15824ed5
commit 2098da3417

35
main.go
View file

@ -7,6 +7,7 @@ import (
"embed" "embed"
"encoding/json" "encoding/json"
"flag" "flag"
"fmt"
"html/template" "html/template"
"io" "io"
"log" "log"
@ -16,12 +17,12 @@ import (
"strings" "strings"
) )
const VERSION = "v1" const VERSION = "v2"
var ( var (
flagVerbose bool flagVerbose bool
flagWsURL string flagVersion bool
flagWatch string flagDebuggingPort int
//go:embed static //go:embed static
fs embed.FS fs embed.FS
@ -29,10 +30,15 @@ var (
func main() { func main() {
flag.BoolVar(&flagVerbose, "v", false, "verbose") flag.BoolVar(&flagVerbose, "v", false, "verbose")
flag.StringVar(&flagWsURL, "ws", "ws://127.0.0.1:9222", "devtools url") flag.BoolVar(&flagVersion, "version", false, "Print version and exit")
flag.StringVar(&flagWatch, "watch", "", "Files to watch") flag.IntVar(&flagDebuggingPort, "port", 9222, "Chrome debugging port")
flag.Parse() flag.Parse()
if flagVersion {
fmt.Println(VERSION)
return
}
http.HandleFunc("/", pageIndex) http.HandleFunc("/", pageIndex)
http.HandleFunc("/css/main.css", pageCSS) http.HandleFunc("/css/main.css", pageCSS)
@ -46,7 +52,7 @@ func main() {
http.ListenAndServe("[::]:5123", nil) http.ListenAndServe("[::]:5123", nil)
} }
func replaceTilde(str string) string {// {{{ func replaceTilde(str string) string { // {{{
homedir, err := os.UserHomeDir() homedir, err := os.UserHomeDir()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -58,7 +64,7 @@ func replaceTilde(str string) string {// {{{
} }
return str return str
}// }}} } // }}}
func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
var tmpl *template.Template var tmpl *template.Template
@ -73,7 +79,7 @@ func pageIndex(w http.ResponseWriter, r *http.Request) { // {{{
} }
data := map[string]any{ data := map[string]any{
"Sites": sites, "Sites": sites,
"VERSION": VERSION, "VERSION": VERSION,
} }
tmpl.Execute(w, data) tmpl.Execute(w, data)
@ -91,7 +97,7 @@ func pageCSS(w http.ResponseWriter, r *http.Request) { // {{{
} // }}} } // }}}
func actionSites(w http.ResponseWriter, r *http.Request) { // {{{ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{
j, _ := json.Marshal(struct { j, _ := json.Marshal(struct {
OK bool OK bool
Sites map[string]*Site Sites map[string]*Site
}{ }{
true, true,
@ -100,17 +106,17 @@ func actionSites(w http.ResponseWriter, r *http.Request) { // {{{
w.Write(j) w.Write(j)
} // }}} } // }}}
func actionBrowserStart(w http.ResponseWriter, r *http.Request) {// {{{ func actionBrowserStart(w http.ResponseWriter, r *http.Request) { // {{{
body, _ := io.ReadAll(r.Body) body, _ := io.ReadAll(r.Body)
userDataDir := replaceTilde(string(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() err := browser.Start()
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} else { } else {
go browser.Wait() go browser.Wait()
} }
}// }}} } // }}}
func actionStart(w http.ResponseWriter, r *http.Request) { // {{{ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
var req struct { var req struct {
URL string URL string
@ -133,7 +139,8 @@ func actionStart(w http.ResponseWriter, r *http.Request) { // {{{
var site Site var site Site
expandedWatch := replaceTilde(req.Watch) 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 { if err != nil {
log.Println(err) log.Println(err)
} }