diff options
author | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-30 23:13:37 +0200 |
---|---|---|
committer | Baitinq <manuelpalenzuelamerino@gmail.com> | 2024-05-30 23:13:37 +0200 |
commit | 61c5a7b22965d8769e50b8112d018cc0c8572236 (patch) | |
tree | 17e121d66ef3f2fe3f058f391224104c4f6d46b2 | |
parent | rest-api: return 404 for all other routes except / (diff) | |
download | fs-tracer-backend-61c5a7b22965d8769e50b8112d018cc0c8572236.tar.gz fs-tracer-backend-61c5a7b22965d8769e50b8112d018cc0c8572236.tar.bz2 fs-tracer-backend-61c5a7b22965d8769e50b8112d018cc0c8572236.zip |
payload-processor: take a list of files
-rwxr-xr-x | requests_examples.sh | 4 | ||||
-rw-r--r-- | src/payload-processor/processor/db.go | 13 | ||||
-rw-r--r-- | src/payload-processor/processor/mock_db.go | 12 | ||||
-rw-r--r-- | src/payload-processor/processor/processor.go | 10 | ||||
-rw-r--r-- | src/payload-processor/processor/processor_test.go | 8 |
5 files changed, 25 insertions, 22 deletions
diff --git a/requests_examples.sh b/requests_examples.sh index 3aa5179..ada2497 100755 --- a/requests_examples.sh +++ b/requests_examples.sh @@ -1,11 +1,11 @@ #!/bin/sh curl -H "API_KEY: ${FS_TRACER_API_KEY}" -X POST -d ' -{ +[{ "timestamp": "2020-01-02T15:04:05Z", "absolute_path": "/home/user/file.txt", "contents": "Hello, World!" -} +}] ' http://leunam.dev:9999/api/v1/file/ curl -H "API_KEY: ${FS_TRACER_API_KEY}" -X GET http://leunam.dev:9999/api/v1/file/?path=%2Fhome%2Fuser%2Ffile.txt diff --git a/src/payload-processor/processor/db.go b/src/payload-processor/processor/db.go index 2200937..f8c4619 100644 --- a/src/payload-processor/processor/db.go +++ b/src/payload-processor/processor/db.go @@ -9,7 +9,7 @@ import ( //go:generate mockgen -source=$GOFILE -package=$GOPACKAGE -destination=mock_$GOFILE type DB interface { - InsertFile(ctx context.Context, file lib.File, user_id string) error + InsertFiles(ctx context.Context, files []lib.File, user_id string) error } type DBImpl struct { @@ -22,8 +22,11 @@ func NewDB(db *sqlx.DB) DB { return &DBImpl{db: db} } -func (db DBImpl) InsertFile(ctx context.Context, file lib.File, user_id string) error { - file.User_id = user_id - _, err := db.db.NamedExecContext(ctx, "INSERT INTO private.file (user_id, absolute_path, contents, timestamp) VALUES (:user_id, :absolute_path, :contents, :timestamp)", file) - return err +func (db DBImpl) InsertFiles(ctx context.Context, files []lib.File, user_id string) error { + for _, file := range files { + file.User_id = user_id + _, err := db.db.NamedExecContext(ctx, "INSERT INTO private.file (user_id, absolute_path, contents, timestamp) VALUES (:user_id, :absolute_path, :contents, :timestamp)", file) + return err + } + return nil } diff --git a/src/payload-processor/processor/mock_db.go b/src/payload-processor/processor/mock_db.go index f6347d7..ba60804 100644 --- a/src/payload-processor/processor/mock_db.go +++ b/src/payload-processor/processor/mock_db.go @@ -40,16 +40,16 @@ func (m *MockDB) EXPECT() *MockDBMockRecorder { return m.recorder } -// InsertFile mocks base method. -func (m *MockDB) InsertFile(ctx context.Context, file lib.File, user_id string) error { +// InsertFiles mocks base method. +func (m *MockDB) InsertFiles(ctx context.Context, files []lib.File, user_id string) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "InsertFile", ctx, file, user_id) + ret := m.ctrl.Call(m, "InsertFiles", ctx, files, user_id) ret0, _ := ret[0].(error) return ret0 } -// InsertFile indicates an expected call of InsertFile. -func (mr *MockDBMockRecorder) InsertFile(ctx, file, user_id any) *gomock.Call { +// InsertFiles indicates an expected call of InsertFiles. +func (mr *MockDBMockRecorder) InsertFiles(ctx, files, user_id any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertFile", reflect.TypeOf((*MockDB)(nil).InsertFile), ctx, file, user_id) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "InsertFiles", reflect.TypeOf((*MockDB)(nil).InsertFiles), ctx, files, user_id) } diff --git a/src/payload-processor/processor/processor.go b/src/payload-processor/processor/processor.go index 1203fea..37632ea 100644 --- a/src/payload-processor/processor/processor.go +++ b/src/payload-processor/processor/processor.go @@ -76,11 +76,11 @@ func (p Processor) process(ctx context.Context, cancel context.CancelFunc) { // TODO: Remove after testing if string(m.Value) == "" { - m.Value = []byte(fmt.Sprintf(`{ + m.Value = []byte(fmt.Sprintf(`[{ "absolute_path": "/home/user/file.txt", "contents": "Hello, World!", "timestamp": "%s" - }`, time.Now().Format(time.RFC3339))) + }]`, time.Now().Format(time.RFC3339))) } err = p.handleMessage(ctx, m, string(user_id)) @@ -96,13 +96,13 @@ func (p Processor) process(ctx context.Context, cancel context.CancelFunc) { func (p Processor) handleMessage(ctx context.Context, m kafka.Message, user_id string) error { fmt.Printf("(%s): message at paritition %d: offset %d: %s = %s, user_id = %s\n", time.Now().String(), m.Partition, m.Offset, string(m.Key), string(m.Value), user_id) - var file lib.File - err := json.Unmarshal(m.Value, &file) + var files []lib.File + err := json.Unmarshal(m.Value, &files) if err != nil { return err } - err = p.db.InsertFile(ctx, file, user_id) + err = p.db.InsertFiles(ctx, files, user_id) if err != nil { return err } diff --git a/src/payload-processor/processor/processor_test.go b/src/payload-processor/processor/processor_test.go index 4a55e48..d7849d9 100644 --- a/src/payload-processor/processor/processor_test.go +++ b/src/payload-processor/processor/processor_test.go @@ -19,20 +19,20 @@ func TestProcessMessage(t *testing.T) { } message := []byte(` - { + [{ "absolute_path": "/tmp/file.txt", "contents": "hello world", "timestamp": "2021-01-01T00:00:00Z" - } + }] `) ctx := context.Background() - mockdb.EXPECT().InsertFile(ctx, lib.File{ + mockdb.EXPECT().InsertFiles(ctx, []lib.File{{ Absolute_path: "/tmp/file.txt", Contents: "hello world", Timestamp: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), - }, "USER_ID").Return(nil) + }}, "USER_ID").Return(nil) err := processor.handleMessage(ctx, kafka.Message{Value: message}, "USER_ID") |