Logging using a handmade one.
This commit is contained in:
parent
f5ce98c021
commit
cafb305b76
@ -3,6 +3,7 @@ package handlers
|
|||||||
import (
|
import (
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"dwelling-upload/internal/configuration"
|
"dwelling-upload/internal/configuration"
|
||||||
|
"dwelling-upload/pkg/logging"
|
||||||
"dwelling-upload/pkg/server"
|
"dwelling-upload/pkg/server"
|
||||||
"dwelling-upload/pkg/utils"
|
"dwelling-upload/pkg/utils"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
@ -11,7 +12,6 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -43,14 +43,21 @@ type UploadedData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type UploadHandlers struct {
|
type UploadHandlers struct {
|
||||||
conf *configuration.Configuration
|
conf *configuration.Configuration
|
||||||
|
logErr *logging.Logger
|
||||||
|
logUpload *logging.Logger
|
||||||
|
logDownload *logging.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUploadHandlers(conf *configuration.Configuration) *UploadHandlers {
|
func NewUploadHandlers(conf *configuration.Configuration, lErr, lUp, lDown *logging.Logger) *UploadHandlers {
|
||||||
compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates",
|
compiledTemplates = amber.MustCompileDir(conf.WebDir+"/templates",
|
||||||
amber.DefaultDirOptions, defaultAmberOptions)
|
amber.DefaultDirOptions, defaultAmberOptions)
|
||||||
|
|
||||||
return &UploadHandlers{
|
return &UploadHandlers{
|
||||||
conf: conf}
|
conf: conf,
|
||||||
|
logErr: lErr,
|
||||||
|
logUpload: lUp,
|
||||||
|
logDownload: lDown}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
||||||
@ -84,7 +91,7 @@ func (h *UploadHandlers) Index(w http.ResponseWriter, r *http.Request) {
|
|||||||
StorageUsedStr: usedStr,
|
StorageUsedStr: usedStr,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
log.Fatalln("failed to execute Index template:", err)
|
h.logErr.Fatalln("failed to execute Index template:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,14 +101,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes)
|
r.Body = http.MaxBytesReader(w, r.Body, fMaxSizeBytes)
|
||||||
|
|
||||||
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
|
if err := r.ParseMultipartForm(fMaxSizeBytes); err != nil {
|
||||||
log.Println("failed to parse form:", err)
|
h.logErr.Println("failed to parse form:", err)
|
||||||
http.Error(w, "request too big", http.StatusExpectationFailed)
|
http.Error(w, "request too big", http.StatusExpectationFailed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
f, fHandler, err := r.FormFile("file")
|
f, fHandler, err := r.FormFile("file")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open incoming file:", err)
|
h.logErr.Println("failed to open incoming file:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -112,7 +119,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
s256 := sha256.New()
|
s256 := sha256.New()
|
||||||
if _, err := io.Copy(s256, f); err != nil {
|
if _, err := io.Copy(s256, f); err != nil {
|
||||||
log.Println("failed to compute SHA-256 hash:", err)
|
h.logErr.Println("failed to compute SHA-256 hash:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -129,14 +136,14 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
fDst, err := os.Create(fPath)
|
fDst, err := os.Create(fPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open file for writing", err)
|
h.logErr.Println("failed to open file for writing", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := io.Copy(fDst, f)
|
n, err := io.Copy(fDst, f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to copy uploaded file to destination:", err)
|
h.logErr.Println("failed to copy uploaded file to destination:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -144,7 +151,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
fDst.Sync()
|
fDst.Sync()
|
||||||
fDst.Close()
|
fDst.Close()
|
||||||
|
|
||||||
log.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, utils.NetworkType(r.Host),
|
h.logUpload.Printf("| %s | %s | %s | SHA256 %s | %s | %d", r.RemoteAddr, utils.NetworkType(r.Host),
|
||||||
fHandler.Filename, fSha256, fSaltedHash, fHandler.Size)
|
fHandler.Filename, fSha256, fSaltedHash, fHandler.Size)
|
||||||
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
@ -160,7 +167,7 @@ func (h *UploadHandlers) Upload(w http.ResponseWriter, r *http.Request) {
|
|||||||
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
|
KeepForHours: h.conf.Uploads.Limits.KeepForHours,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
log.Fatalln("failed to execute Index template:", err)
|
h.logErr.Fatalln("failed to execute Index template:", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,11 +188,13 @@ func (h *UploadHandlers) Download(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
fd, err := os.Open(path)
|
fd, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to open file to read:", err)
|
h.logErr.Println("failed to open file to read:", err)
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer fd.Close()
|
defer fd.Close()
|
||||||
|
|
||||||
|
h.logDownload.Printf("| %s | %s | %s | %s", r.RemoteAddr, utils.NetworkType(r.Host), name, saltedHash)
|
||||||
|
|
||||||
http.ServeContent(w, r, path, stat.ModTime(), fd)
|
http.ServeContent(w, r, path, stat.ModTime(), fd)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user