1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-12-20 10:31:16 +00:00
share/http.go
Knut Ahlers 632c48022f
Update deps, fix linter errors, improve code
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-03-18 13:42:19 +01:00

51 lines
1.1 KiB
Go

package main
import (
"fmt"
"net/http"
"time"
"github.com/gofrs/uuid"
"github.com/sirupsen/logrus"
)
func doListen() (err error) {
http.HandleFunc("/post", simpleFilePost)
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
}
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)
)
f, fh, err := r.FormFile("file")
if err != nil {
logger.WithError(err).Error("retrieving file from request")
http.Error(w, errStr, http.StatusBadRequest)
return
}
url, err := executeUpload(fh.Filename, f, true, "", false)
if err != nil {
logger.WithError(err).Error("uploading file from HTTP request")
http.Error(w, errStr, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/plain")
http.Error(w, url, http.StatusOK)
}