Compare commits
No commits in common. "c1edf992f994d531d84a25b7b59e09205419a045" and "5ea944695d1696e30653728fe7adc9e6616a4ee4" have entirely different histories.
c1edf992f9
...
5ea944695d
27
main.go
27
main.go
|
@ -86,8 +86,6 @@ 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 {
|
||||||
|
@ -124,12 +122,6 @@ 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) {
|
||||||
|
@ -141,7 +133,7 @@ func setupDownload(server *http.Server, conf Config) {
|
||||||
|
|
||||||
downloads--
|
downloads--
|
||||||
if downloads == 0 {
|
if downloads == 0 {
|
||||||
go shutdown(server)
|
server.Shutdown(context.Background())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -163,7 +155,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">
|
||||||
|
@ -171,21 +163,16 @@ var uploadTemplate = `{{template "BaseHeader" "RUFF - Upload Form"}}
|
||||||
</form>
|
</form>
|
||||||
{{template "BaseFooter"}}`
|
{{template "BaseFooter"}}`
|
||||||
|
|
||||||
var errorTemplate = `{{template "BaseHeader" "RUFF - Upload Error"}}
|
var errorTemplate = `{{template "BaseHeader" "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
|
||||||
|
@ -219,13 +206,5 @@ 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