Add logging for incoming HTTP requests and received files.

main
diff 2021-03-25 20:12:05 +00:00
parent 293d95b699
commit 3e0dea55d8
1 changed files with 10 additions and 2 deletions

12
main.go
View File

@ -158,12 +158,17 @@ func main() {
// setupDownload sets up the HTTP server for sending a file to a remote device. // setupDownload sets up the HTTP server for sending a file to a remote device.
func setupDownload(server *http.Server, conf Config) { func setupDownload(server *http.Server, conf Config) {
http.Handle("/", http.RedirectHandler("/"+conf.FileName, http.StatusFound)) // 302 redirect http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Incoming request: %v: %v %v %v\n", r.RemoteAddr, r.Proto, r.Method, r.URL)
// 303 redirect to real file.
http.RedirectHandler("/"+conf.FileName, http.StatusSeeOther).ServeHTTP(w, r)
})
downloads := conf.Downloads downloads := conf.Downloads
http.HandleFunc("/"+conf.FileName, func(w http.ResponseWriter, r *http.Request) { http.HandleFunc("/"+conf.FileName, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(conf.FileName)+"\"") fmt.Printf("Incoming request: %v: %v %v %v\n", r.RemoteAddr, r.Proto, r.Method, r.URL)
w.Header().Set("Content-Disposition", "attachment; filename=\""+url.PathEscape(conf.FileName)+"\"")
// http.ServeFile handles all the nitty gritty details of hauling the file // http.ServeFile handles all the nitty gritty details of hauling the file
// off, but maybe it shouldn't? ServeFile does content ranges and I really // off, but maybe it shouldn't? ServeFile does content ranges and I really
// don't see that working with limited download counts unless we reimplement // don't see that working with limited download counts unless we reimplement
@ -232,6 +237,8 @@ func setupUpload(server *http.Server, conf Config) {
template.Must(tpl.New("UploadMessage").Parse(messageTemplate)) 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) {
fmt.Printf("Incoming request: %v: %v %v %v\n", r.RemoteAddr, r.Proto, r.Method, r.URL)
// Display upload form // Display upload form
if r.Method != http.MethodPost { if r.Method != http.MethodPost {
err := tpl.ExecuteTemplate(w, "UploadForm", conf) err := tpl.ExecuteTemplate(w, "UploadForm", conf)
@ -294,6 +301,7 @@ func saveFile(header *multipart.FileHeader) error {
return err return err
} }
fmt.Printf("Received file: %v\n", header.Filename)
return nil return nil
} }