2017-12-29 22:35:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-18 12:42:19 +00:00
|
|
|
"fmt"
|
2017-12-29 22:35:57 +00:00
|
|
|
"net/http"
|
2024-03-18 12:42:19 +00:00
|
|
|
"time"
|
2017-12-29 22:35:57 +00:00
|
|
|
|
2024-03-18 12:42:19 +00:00
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
"github.com/sirupsen/logrus"
|
2017-12-29 22:35:57 +00:00
|
|
|
)
|
|
|
|
|
2024-03-18 12:42:19 +00:00
|
|
|
func doListen() (err error) {
|
2017-12-29 22:35:57 +00:00
|
|
|
http.HandleFunc("/post", simpleFilePost)
|
2024-03-18 12:42:19 +00:00
|
|
|
|
|
|
|
server := &http.Server{
|
|
|
|
Addr: cfg.Listen,
|
|
|
|
Handler: http.DefaultServeMux,
|
|
|
|
ReadHeaderTimeout: time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = server.ListenAndServe(); err != nil {
|
|
|
|
return fmt.Errorf("listening for HTTP traffic: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-12-29 22:35:57 +00:00
|
|
|
}
|
|
|
|
|
2024-03-18 12:42:19 +00:00
|
|
|
func simpleFilePost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
|
|
|
reqUUID = uuid.Must(uuid.NewV4()).String()
|
|
|
|
logger = logrus.WithField("req-id", reqUUID)
|
|
|
|
errStr = fmt.Sprintf("something went wrong: %s", reqUUID)
|
|
|
|
)
|
|
|
|
|
2017-12-29 22:35:57 +00:00
|
|
|
f, fh, err := r.FormFile("file")
|
|
|
|
if err != nil {
|
2024-03-18 12:42:19 +00:00
|
|
|
logger.WithError(err).Error("retrieving file from request")
|
|
|
|
http.Error(w, errStr, http.StatusBadRequest)
|
2017-12-29 22:35:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-12 22:05:06 +00:00
|
|
|
url, err := executeUpload(fh.Filename, f, true, "", false)
|
2017-12-29 22:35:57 +00:00
|
|
|
if err != nil {
|
2024-03-18 12:42:19 +00:00
|
|
|
logger.WithError(err).Error("uploading file from HTTP request")
|
|
|
|
http.Error(w, errStr, http.StatusInternalServerError)
|
2017-12-29 22:35:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-18 12:42:19 +00:00
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
http.Error(w, url, http.StatusOK)
|
2017-12-29 22:35:57 +00:00
|
|
|
}
|