aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: f91fa143d6449282c60fa17f7eb4ee591b4ecfe0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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()
}