diff --git a/main.go b/main.go index 38f2353..6adf454 100644 --- a/main.go +++ b/main.go @@ -109,8 +109,6 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { contentType = contentType[0:idx] } - wg := &sync.WaitGroup{} - switch contentType { case "multipart/form-data": if r.MultipartForm == nil { @@ -128,6 +126,8 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { fileLock := &sync.Mutex{} + wg := &sync.WaitGroup{} + for _, files := range r.MultipartForm.File { // Append files for _, file := range files { @@ -161,19 +161,25 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { log.WithField("count", fileCount).Debug("Waiting for jobs to finish") log.WithField("matches", res).Debug("Matched rules") - json.NewEncoder(w).Encode(res) + err := json.NewEncoder(w).Encode(res) + + if err != nil { + log.WithError(err).Error("Unable to send response") + } default: job := &Job{ Data: r.Body, Callback: func(m yara.MatchRules, err error) { - wg.Add(1) - log.WithField("match", m).Debug("Matched rules") if err != nil { - json.NewEncoder(w).Encode(response{Success: false, Error: err}) + err = json.NewEncoder(w).Encode(response{Success: false, Error: err}) } else { - json.NewEncoder(w).Encode(response{Success: true, MatchedRules: m}) + err = json.NewEncoder(w).Encode(response{Success: true, MatchedRules: m}) + } + + if err != nil { + log.WithError(err).Error("Unable to send response") } }, } @@ -181,8 +187,6 @@ func scanHandler(w http.ResponseWriter, r *http.Request) { log.WithField("contentType", contentType).Debug("Scanning contents of body") jobChan <- job - - wg.Wait() } }