aboutsummaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-12-30 22:46:09 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-12-30 22:46:09 +0530
commitb72b1278f23af720a16f9d231533864fa48034e8 (patch)
tree4bcf0ee8830b3bd33cfb825873942369ac5f0c20 /main.go
initial commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..f91fa14
--- /dev/null
+++ b/main.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+ "os"
+ "fmt"
+ "net/http"
+ "github.com/gotk3/gotk3/glib"
+ "github.com/gotk3/gotk3/gtk"
+)
+
+func serve(port, dir string) {
+ http.Handle("/", http.FileServer(http.Dir(dir)))
+ http.ListenAndServe(":" + port, nil)
+}
+
+func main() {
+ 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)
+
+ grid, _ := gtk.GridNew()
+ grid.SetOrientation(gtk.ORIENTATION_VERTICAL)
+
+ dirLabel, _ := gtk.LabelNew("Directory to serve: ")
+ grid.Attach(dirLabel, 0, 0, 1, 1)
+ dirInput, _ := gtk.EntryNew()
+ grid.Attach(dirInput, 1, 0, 2, 1)
+
+ portLabel, _ := gtk.LabelNew("Port:")
+ grid.Attach(portLabel, 0, 1, 1, 1)
+ portInput, _ := gtk.EntryNew()
+ grid.Attach(portInput, 1, 1, 2, 1)
+
+ buttonSwitch, _ := gtk.ButtonNew()
+ buttonSwitch.SetLabel("Start")
+ grid.Attach(buttonSwitch, 1, 2, 2, 1)
+
+ statusLabel, _ := gtk.LabelNew("")
+ grid.Attach(statusLabel, 0, 3, 1, 4)
+
+ 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)
+ })
+
+ win.Add(grid)
+ win.ShowAll()
+}