Compare commits

...

2 Commits

Author SHA1 Message Date
diff c1edf992f9 Make sure all requests are complete before shutting down.
Missed a spot in the net/http docs. Calling Server.Shutdown() causes
ListenAndServe to return *immediately*, and when it does main() exits and
the "Upload Success" message gets cut off before it can be fully sent, which
spooks HTTP clients a bit.

To avoid that, you have to wait for the call to Server.Shutdown() to return,
and then signal back to main() that it's now safe to exit.
2021-03-24 06:01:31 +00:00
diff 0b02918dfb Add success message for uploads. 2021-03-24 05:44:09 +00:00
1 changed files with 24 additions and 3 deletions

27
main.go
View File

@ -86,6 +86,8 @@ func getIP() (string, error) {
return localAddr.IP.String(), nil return localAddr.IP.String(), nil
} }
var done = make(chan struct{})
func main() { func main() {
conf, err := getConfig() conf, err := getConfig()
if err != nil { if err != nil {
@ -122,6 +124,12 @@ func main() {
fmt.Println(err) fmt.Println(err)
os.Exit(1) os.Exit(1)
} }
// Wait for the server to finish any transfers, up to 3 seconds
select {
case <-done:
case <-time.After(3*time.Second):
}
} }
func setupDownload(server *http.Server, conf Config) { func setupDownload(server *http.Server, conf Config) {
@ -133,7 +141,7 @@ func setupDownload(server *http.Server, conf Config) {
downloads-- downloads--
if downloads == 0 { if downloads == 0 {
server.Shutdown(context.Background()) go shutdown(server)
} }
}) })
} }
@ -155,7 +163,7 @@ var baseHeader = `<!DOCTYPE html>
var baseFooter = `</body> var baseFooter = `</body>
</html>` </html>`
var uploadTemplate = `{{template "BaseHeader" "RUFF Upload Form"}} var uploadTemplate = `{{template "BaseHeader" "RUFF - Upload Form"}}
<form enctype="multipart/form-data" action="/" method="post"> <form enctype="multipart/form-data" action="/" method="post">
<label for="file">Select a file for upload:</label> <label for="file">Select a file for upload:</label>
<input type="file" name="file"> <input type="file" name="file">
@ -163,16 +171,21 @@ var uploadTemplate = `{{template "BaseHeader" "RUFF Upload Form"}}
</form> </form>
{{template "BaseFooter"}}` {{template "BaseFooter"}}`
var errorTemplate = `{{template "BaseHeader" "Upload Error"}} var errorTemplate = `{{template "BaseHeader" "RUFF - Upload Error"}}
<p>{{.}}</p> <p>{{.}}</p>
<p><a href="/">Go back</a></p> <p><a href="/">Go back</a></p>
{{template "BaseFooter"}}` {{template "BaseFooter"}}`
var messageTemplate = `{{template "BaseHeader" (print "RUFF - " .)}}
<p>{{.}}</p>
{{template "BaseFooter"}}`
func setupUpload(server *http.Server, conf Config) { func setupUpload(server *http.Server, conf Config) {
tpl := template.Must(template.New("BaseHeader").Parse(baseHeader)) tpl := template.Must(template.New("BaseHeader").Parse(baseHeader))
template.Must(tpl.New("BaseFooter").Parse(baseFooter)) template.Must(tpl.New("BaseFooter").Parse(baseFooter))
template.Must(tpl.New("UploadForm").Parse(uploadTemplate)) template.Must(tpl.New("UploadForm").Parse(uploadTemplate))
template.Must(tpl.New("UploadError").Parse(errorTemplate)) template.Must(tpl.New("UploadError").Parse(errorTemplate))
template.Must(tpl.New("UploadMessage").Parse(messageTemplate))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// Upload form // Upload form
@ -206,5 +219,13 @@ func setupUpload(server *http.Server, conf Config) {
tpl.ExecuteTemplate(w, "UploadError", err) tpl.ExecuteTemplate(w, "UploadError", err)
return return
} }
tpl.ExecuteTemplate(w, "UploadMessage", "Upload successful!")
go shutdown(server)
}) })
} }
func shutdown(server *http.Server) {
server.Shutdown(context.Background())
done <- struct{}{}
}