From 487ce4dc647a8aa17123acf193765bf08e2f1e2d Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 31 Dec 2022 00:19:33 +0530 Subject: starting/stopping webserver works now --- main.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 58 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index f91fa14..ce6ae8e 100644 --- a/main.go +++ b/main.go @@ -2,56 +2,90 @@ package main import ( "os" - "fmt" "net/http" + "log" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" + "context" + "sync" ) -func serve(port, dir string) { - http.Handle("/", http.FileServer(http.Dir(dir))) - http.ListenAndServe(":" + port, nil) +func serve(port, dir string, wg *sync.WaitGroup) *http.Server { + mux := http.NewServeMux() + mux.Handle("/", http.FileServer(http.Dir(dir))) + server := &http.Server{Addr: ":" + port, Handler: mux} + + go func() { + defer wg.Done() + + if err := server.ListenAndServe(); err != http.ErrServerClosed { + // TODO: show error in GTK error window + log.Fatalf("ListenAndServe(): %v", err) + } + }() + + return server } + func main() { - app, _ := gtk.ApplicationNew("net.mikunonaka.fileserver", glib.APPLICATION_FLAGS_NONE) - app.Connect("activate", func() { onActivate(app) }) - app.Run(os.Args) + app, _ := gtk.ApplicationNew("net.mikunonaka.fileserver", glib.APPLICATION_FLAGS_NONE) + app.Connect("activate", func() { onActivate(app) }) + app.Run(os.Args) } func onActivate(app *gtk.Application) { win, _ := gtk.ApplicationWindowNew(app) - win.SetTitle("HTTP File Server") - win.SetDefaultSize(400, 400) + win.SetTitle("HTTP File Server") + win.SetDefaultSize(400, 400) - grid, _ := gtk.GridNew() - grid.SetOrientation(gtk.ORIENTATION_VERTICAL) + box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0) + dirBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) dirLabel, _ := gtk.LabelNew("Directory to serve: ") - grid.Attach(dirLabel, 0, 0, 1, 1) - dirInput, _ := gtk.EntryNew() - grid.Attach(dirInput, 1, 0, 2, 1) + dirBox.PackStart(dirLabel, false, false, 5) + dirInput, _ := gtk.EntryNew() + dirBox.PackEnd(dirInput, false, false, 5) + box.PackStart(dirBox, false, false, 5) + portBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) portLabel, _ := gtk.LabelNew("Port:") - grid.Attach(portLabel, 0, 1, 1, 1) - portInput, _ := gtk.EntryNew() - grid.Attach(portInput, 1, 1, 2, 1) + portBox.PackStart(portLabel, false, false, 5) + portInput, _ := gtk.EntryNew() + portBox.PackEnd(portInput, false, false, 5) + box.PackStart(portBox, false, false, 5) + buttonBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0) buttonSwitch, _ := gtk.ButtonNew() buttonSwitch.SetLabel("Start") - grid.Attach(buttonSwitch, 1, 2, 2, 1) + buttonBox.PackStart(buttonSwitch, true, true, 0) + box.PackStart(buttonBox, false, false, 5) statusLabel, _ := gtk.LabelNew("") - grid.Attach(statusLabel, 0, 3, 1, 4) + box.PackStart(statusLabel, false, true, 10) + var on = false + var server *http.Server buttonSwitch.Connect("clicked", func() { port, _ := portInput.GetText() dir, _ := dirInput.GetText() - go serve(port, dir) - statusText := fmt.Sprintf("Serving directory '%s' on port '%s'", dir, port) - statusLabel.SetText(statusText) + + if on { + server.Shutdown(context.TODO()) + } else { + go func() { + killServerDone := &sync.WaitGroup{} + server = serve(port, dir, killServerDone) + killServerDone.Add(1) + on = true + buttonSwitch.SetLabel("Stop") + killServerDone.Wait() + on = false + buttonSwitch.SetLabel("Start") + }() + } }) - win.Add(grid) - win.ShowAll() + win.Add(box) + win.ShowAll() } -- cgit v1.2.3