From f40159390084a91ad81b8b6bff0e992477cdc106 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 31 Dec 2022 16:58:44 +0530 Subject: validate port input and default to 8080 if not specified --- main.go | 53 ++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 41d69c8..1a67787 100644 --- a/main.go +++ b/main.go @@ -1,14 +1,20 @@ package main import ( - "os" - "net/http" + "context" + "fmt" "log" + "net/http" + "os" + "strconv" + "sync" + "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" - "context" - "sync" - "fmt" +) + +var ( + DEFAULT_PORT, DEFAULT_DIR string = "8080", "." ) func serve(port, dir string, wg *sync.WaitGroup) *http.Server { @@ -16,12 +22,15 @@ func serve(port, dir string, wg *sync.WaitGroup) *http.Server { mux.Handle("/", http.FileServer(http.Dir(dir))) server := &http.Server{Addr: ":" + port, Handler: mux} + if port == "" { port = DEFAULT_PORT } + if dir == "" { dir = DEFAULT_DIR } + go func() { defer wg.Done() if err := server.ListenAndServe(); err != http.ErrServerClosed { // TODO: show error in GTK error window - log.Fatalf("ListenAndServe(): %v", err) + log.Printf("Error while running HTTP server: %v\n", err) } }() @@ -30,7 +39,7 @@ func serve(port, dir string, wg *sync.WaitGroup) *http.Server { func main() { - app, _ := gtk.ApplicationNew("net.mikunonaka.fileserver", glib.APPLICATION_FLAGS_NONE) + app, _ := gtk.ApplicationNew("net.mikunonaka.gofileserver", glib.APPLICATION_FLAGS_NONE) app.Connect("activate", func() { onActivate(app) }) app.Run(os.Args) } @@ -46,6 +55,7 @@ func onActivate(app *gtk.Application) { dirLabel, _ := gtk.LabelNew("Directory to serve: ") dirBox.PackStart(dirLabel, false, false, 5) dirInput, _ := gtk.EntryNew() + dirInput.SetText(DEFAULT_DIR) dirBox.PackEnd(dirInput, false, false, 5) box.PackStart(dirBox, false, false, 5) @@ -53,11 +63,12 @@ func onActivate(app *gtk.Application) { portLabel, _ := gtk.LabelNew("Port:") portBox.PackStart(portLabel, false, false, 5) portInput, _ := gtk.EntryNew() + portInput.SetText(DEFAULT_PORT) portBox.PackEnd(portInput, false, false, 5) box.PackStart(portBox, false, false, 5) buttonBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) - buttonSwitch, _ := gtk.ButtonNew() + buttonSwitch, _ := gtk.ToggleButtonNew() buttonSwitch.SetLabel("Start") buttonBox.PackStart(buttonSwitch, true, true, 0) box.PackStart(buttonBox, false, false, 5) @@ -67,7 +78,7 @@ func onActivate(app *gtk.Application) { var on = false var server *http.Server - buttonSwitch.Connect("clicked", func() { + buttonSwitch.Connect("toggled", func() { // TODO: validate dir and port port, _ := portInput.GetText() dir, _ := dirInput.GetText() @@ -78,21 +89,45 @@ func onActivate(app *gtk.Application) { go func() { killServerDone := &sync.WaitGroup{} server = serve(port, dir, killServerDone) + killServerDone.Add(1) // do this after server starts on = true buttonSwitch.SetLabel("Stop") statusLabel.SetText(fmt.Sprintf("Serving directory '%s' on PORT %s", dir, port)) + dirInput.SetEditable(false) + dirInput.SetCanFocus(false) + portInput.SetEditable(false) + portInput.SetCanFocus(false) killServerDone.Wait() // do this after server shuts down on = false buttonSwitch.SetLabel("Start") statusLabel.SetText("") + dirInput.SetEditable(true) + dirInput.SetCanFocus(true) + portInput.SetEditable(true) + portInput.SetCanFocus(true) }() } }) + portInput.Connect("insert-text", func(_ *gtk.Entry, input string) { + if _, err := strconv.Atoi(input); err != nil { + // if input is not numeric, don't insert + portInput.StopEmission("insert-text") + } else { + // stop if port number is invalid / out of range + currentInput, _ := portInput.GetText() + nextInput := currentInput + input + n, _ := strconv.Atoi(nextInput) + if n < 0 || n > 65535 { + portInput.StopEmission("insert-text") + } + } + }) + win.Add(box) win.ShowAll() } -- cgit v1.2.3