diff --git a/config.toml b/config.toml index 458af21..14c8a47 100644 --- a/config.toml +++ b/config.toml @@ -1,3 +1,4 @@ +debug = true [web] cert = '/etc/ssl/certs/ssl-cert-snakeoil.pem' host = '127.0.0.1' @@ -19,3 +20,4 @@ path = "./storage/files/" [processing] maxparallel = 5 oleurl = "http://localhost:5000" + diff --git a/server/internal/config/config.go b/server/internal/config/config.go index ef70a9d..722984a 100644 --- a/server/internal/config/config.go +++ b/server/internal/config/config.go @@ -45,6 +45,7 @@ func setDefaults() { viper.SetDefault("db.debug", false) viper.SetDefault("processing.oleurl", "http://localhost:5000") viper.SetDefault("store.path", "./storage/files/") + viper.SetDefault("debug", false) } func SaveConfig() error { diff --git a/server/internal/middlewares/logging.go b/server/internal/middlewares/logging.go index 2c694d1..336c223 100644 --- a/server/internal/middlewares/logging.go +++ b/server/internal/middlewares/logging.go @@ -27,6 +27,6 @@ func Logging(next http.Handler) http.Handler { next.ServeHTTP(wrapped, r) - slog.Info("webserver", "status", wrapped.statusCode, "method", r.Method, "path", r.URL.Path, "duration", time.Since(start)) + slog.Info("http", "status", wrapped.statusCode, "method", r.Method, "path", r.URL.Path, "duration", time.Since(start)) }) } diff --git a/server/internal/processing/msoffice/mraptor.go b/server/internal/processing/msoffice/mraptor.go index 847c0a5..bbb51a8 100644 --- a/server/internal/processing/msoffice/mraptor.go +++ b/server/internal/processing/msoffice/mraptor.go @@ -36,6 +36,7 @@ func MraptorScan(fileID pgtype.UUID) error { if json.Valid(body) == false { return fmt.Errorf("JSON not valid") } + slog.Debug("MraptorScan", "file-uuid", fileID.String(), "data", body) database.InsertJsonResult(fileID, body, "msoffice_mraptor") return nil } diff --git a/server/internal/processing/msoffice/olevba.go b/server/internal/processing/msoffice/olevba.go index 21b388c..5c00c05 100644 --- a/server/internal/processing/msoffice/olevba.go +++ b/server/internal/processing/msoffice/olevba.go @@ -35,6 +35,7 @@ func OleVBAScan(fileID pgtype.UUID) error { if json.Valid(body) == false { return fmt.Errorf("JSON not valid") } + slog.Debug("OleVBAScan", "file-uuid", fileID.String(), "data", body) database.InsertJsonResult(fileID, body, "msoffice_olevba") return nil } diff --git a/server/main.go b/server/main.go index b3f2cbf..8bc3bee 100644 --- a/server/main.go +++ b/server/main.go @@ -34,6 +34,10 @@ func main() { log.SetOutput(os.Stderr) slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, nil))) config.ReadConfigFile("") + if viper.GetBool("debug") { + slog.SetDefault(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))) + slog.Debug("Debug logging enabled") + } var wg sync.WaitGroup database.Connect() database.Ping() diff --git a/server/web/index.go b/server/web/index.go index b278975..83c6f93 100644 --- a/server/web/index.go +++ b/server/web/index.go @@ -6,6 +6,7 @@ import ( "net/http" "git.jmbit.de/jmb/scanfile/server/internal/database" + "git.jmbit.de/jmb/scanfile/server/internal/processing" "github.com/spf13/viper" ) @@ -30,12 +31,14 @@ func IndexUploadHandler(w http.ResponseWriter, r *http.Request) { r.Body = http.MaxBytesReader(w, r.Body, maxUploadSize) defer r.Body.Close() fileData, fileHeader, err := r.FormFile("file") + slog.Debug("File form submitted", "file-name", fileHeader.Filename, "file-size", fileHeader.Size) if err != nil { slog.Error("Error parsing form in IndexUploadHandler", "error", err) http.Error(w, err.Error(), http.StatusBadRequest) return } fileBytes, err := io.ReadAll(fileData) + slog.Debug("File from form read", "file-name", fileHeader.Filename, "file-size", len(fileBytes)) if err != nil { slog.Error("Error reading file in IndexUploadHandler", "error", err) http.Error(w, err.Error(), http.StatusBadRequest) @@ -45,7 +48,13 @@ func IndexUploadHandler(w http.ResponseWriter, r *http.Request) { file, err := database.CreateFile(r.Context(), fileHeader.Filename, fileBytes) if err != nil { slog.Error("Error saving file in IndexUploadHandler", "error", err) - http.Error(w, err.Error(), http.StatusBadRequest) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = processing.Submit(r.Context(), file.ID) + if err != nil { + slog.Error("Error submitting file for processing in IndexUploadHandler", "error", err) + http.Error(w, err.Error(), http.StatusInternalServerError) return }