Compare commits
2 Commits
5ea944695d
...
c1edf992f9
Author | SHA1 | Date |
---|---|---|
diff | c1edf992f9 | |
diff | 0b02918dfb |
27
main.go
27
main.go
|
@ -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{}{}
|
||||||
|
}
|
Loading…
Reference in New Issue