Update responses to use slice instead of pointer
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Tyler 2021-03-07 21:32:42 -05:00
parent a84d3dc9fc
commit 73ceab9418
1 changed files with 7 additions and 9 deletions

16
main.go
View File

@ -18,7 +18,7 @@ import (
"time" "time"
) )
type callbackFunc func(*yara.MatchRules, error) type callbackFunc func(yara.MatchRules, error)
type Job struct { type Job struct {
Data io.ReadCloser Data io.ReadCloser
@ -143,9 +143,9 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
job := &Job{ job := &Job{
Data: f, Data: f,
Callback: func(m *yara.MatchRules, err error) { Callback: func(m yara.MatchRules, err error) {
fileLock.Lock() fileLock.Lock()
res.Files[file.Filename] = *m res.Files[file.Filename] = m
fileLock.Unlock() fileLock.Unlock()
wg.Done() wg.Done()
@ -158,20 +158,18 @@ func scanHandler(w http.ResponseWriter, r *http.Request) {
log.WithField("count", fileCount).Debug("Waiting for jobs to finish") log.WithField("count", fileCount).Debug("Waiting for jobs to finish")
wg.Wait()
log.WithField("matches", res).Debug("Matched rules") log.WithField("matches", res).Debug("Matched rules")
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
default: default:
job := &Job{ job := &Job{
Data: r.Body, Data: r.Body,
Callback: func(m *yara.MatchRules, err error) { Callback: func(m yara.MatchRules, err error) {
log.WithField("match", m).Debug("Matched rules") log.WithField("match", m).Debug("Matched rules")
if err != nil { if err != nil {
json.NewEncoder(w).Encode(response{Success: false, Error: err}) json.NewEncoder(w).Encode(response{Success: false, Error: err})
} else { } else {
json.NewEncoder(w).Encode(response{Success: true, MatchedRules: *m}) json.NewEncoder(w).Encode(response{Success: true, MatchedRules: m})
} }
}, },
} }
@ -252,7 +250,7 @@ func worker(rules *yara.Rules) {
} }
func processJob(s *yara.Scanner, job *Job) { func processJob(s *yara.Scanner, job *Job) {
var m yara.MatchRules m := make(yara.MatchRules, 0)
defer job.Data.Close() defer job.Data.Close()
@ -270,5 +268,5 @@ func processJob(s *yara.Scanner, job *Job) {
return return
} }
job.Callback(&m, nil) job.Callback(m, nil)
} }