aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 705139eddb00b074c2acba2cf4cbe5e5b2bcece6 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main

import (
	"context"
	"fmt"
	"net/http"
	"os"
	"log"
	"strconv"
	"sync"
	"github.com/gotk3/gotk3/glib"
	"github.com/gotk3/gotk3/gtk"
	"github.com/gotk3/gotk3/pango"
)

var (
	DEFAULT_PORT, DEFAULT_DIR, APP_VERSION string = "8080", ".", "v0.1.1"
)

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}

	if port == "" { port = DEFAULT_PORT }

    go func() {
		wg.Add(1)
        defer wg.Done()

        if err := server.ListenAndServe(); err != http.ErrServerClosed {
			log.Fatalf("Error while running the server: %v\n", err)
        }
    }()

    return server
}


func main() {
	app, _ := gtk.ApplicationNew("net.mikunonaka.gofileserver", 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(330, 230)
	win.SetResizable(false)
	win.SetPosition(gtk.WIN_POS_CENTER)

	box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)

	dirBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
	dirLabel, _ := gtk.LabelNew("Directory to serve: ")
	dirBox.PackStart(dirLabel, false, false, 5)
	browseButton, _ := gtk.FileChooserButtonNew("Browse Directory To Serve", gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER)
	dirBox.PackEnd(browseButton, false, false, 5)
	box.PackStart(dirBox, false, false, 5)

	portBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
	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)
	aboutButton, _ := gtk.ButtonNewWithLabel("About")
	buttonBox.PackStart(aboutButton, false, true, 5)
	buttonSwitch, _ := gtk.ButtonNewWithLabel("Start")
	buttonBox.PackEnd(buttonSwitch, true, true, 5)
	box.PackStart(buttonBox, false, false, 5)

	statusLabel, _ := gtk.LabelNew("")
	statusLabel.SetLineWrap(true)
	statusLabel.SetLineWrapMode(pango.WrapMode(gtk.ALIGN_START))
	statusLabel.SetJustify(gtk.JUSTIFY_CENTER)
	box.PackStart(statusLabel, false, true, 10)

	var on = false
	var server *http.Server
	buttonSwitch.Connect("clicked", func() {
		// TODO: validate dir and port
		port, _ := portInput.GetText()
		dir := browseButton.GetFilename()
		if dir == "" { dir = DEFAULT_DIR }

		if on {
			server.Shutdown(context.TODO())
			statusLabel.SetMarkup("<span foreground='#ffcc00'>Server was terminated by user.</span>")
		} else {
			go func() {
				killServerDone := &sync.WaitGroup{}
				server = serve(port, dir, killServerDone)

				//killServerDone.Add(1)
				// do this after server starts
				on = true
				buttonSwitch.SetLabel("Stop")
				statusLabel.SetMarkup(fmt.Sprintf("Serving\n%s\nOn <a href=\"http://localhost:%s\">http://localhost:%s</a>", dir, port, port))
				browseButton.SetCanFocus(false)
				portInput.SetEditable(false)
				portInput.SetCanFocus(false)

				killServerDone.Wait()
				// do this after server shuts down
				on = false
				buttonSwitch.SetLabel("Start")
				browseButton.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")
			}
		}
	})

	browseButton.Connect("selection-changed", func() {
		/* when different directory is selected,
		 * shut down the server and show warning message */
		if on {
			server.Shutdown(context.TODO())
			statusLabel.SetMarkup("<span foreground='#ffcc00'>Server was terminated because root directory changed.</span>")
		}
	})

	aboutButton.Connect("clicked", func() {
		aboutWindow, _ := gtk.ApplicationWindowNew(app)
		aboutWindow.SetTitle("About - GoFileServer")
		aboutWindow.SetDefaultSize(420, 180)
		aboutWindow.SetResizable(false)
		aboutWindow.SetPosition(gtk.WIN_POS_MOUSE)

		box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 10)

		titleLabel, _ := gtk.LabelNew("")
		titleLabel.SetMarkup(fmt.Sprintf("<b>GoFileServer</b> <span color='lightgray'>%s</span>", APP_VERSION))
		box.PackStart(titleLabel, true, true, 5)

		copyrightLabel, _ := gtk.LabelNew("Copyright (c) 2022 Vidhu Kant Sharma")
		box.PackStart(copyrightLabel, true, true, 5)

		urlLabel, _ := gtk.LabelNew("")
		urlLabel.SetMarkup("<a href='https://github.com/MikunoNaka/GoFileServer'>https://github.com/MikunoNaka/GoFileServer</a>")
		box.PackStart(urlLabel, true, true, 5)

		gplLabel, _ := gtk.LabelNew("")
		gplLabel.SetMarkup("<span>This program comes with absolutely no warranty.\nSee the <a href='https://www.gnu.org/licenses/gpl-3.0.en.html'>GNU General Public License Version 3 or later</a> for details.</span>")
		gplLabel.SetLineWrap(true)
		gplLabel.SetLineWrapMode(pango.WrapMode(gtk.ALIGN_START))
		gplLabel.SetJustify(gtk.JUSTIFY_CENTER)
		box.PackStart(gplLabel, true, true, 5)

		buttonBox, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
		closeButton, _ := gtk.ButtonNewWithLabel("Close")
		buttonBox.PackEnd(closeButton, false, true, 5)
		box.PackEnd(buttonBox, true, true, 5)

		closeButton.Connect("clicked", func() { aboutWindow.Close() })

		aboutWindow.Add(box)
		aboutWindow.ShowAll()
	})

	win.Add(box)
	win.ShowAll()
}