about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-29 21:07:31 +0200
committerBaitinq <manuelpalenzuelamerino@gmail.com>2024-05-29 21:13:14 +0200
commit1ed866d31ddeec5ab484760bada2355bbf49c698 (patch)
treea278178ed8bc71f7349d1afce16cca2c798b7181
parentrest-api: handle payloads with an api_key (diff)
downloadfs-tracer-backend-1ed866d31ddeec5ab484760bada2355bbf49c698.tar.gz
fs-tracer-backend-1ed866d31ddeec5ab484760bada2355bbf49c698.tar.bz2
fs-tracer-backend-1ed866d31ddeec5ab484760bada2355bbf49c698.zip
rest-api: Take file path in query params
-rwxr-xr-xrequests_examples.sh11
-rw-r--r--src/rest-api/handler/handler.go5
-rw-r--r--src/rest-api/handler/handler_test.go2
3 files changed, 12 insertions, 6 deletions
diff --git a/requests_examples.sh b/requests_examples.sh
index 45ef59d..616e625 100755
--- a/requests_examples.sh
+++ b/requests_examples.sh
@@ -1,5 +1,12 @@
 #!/bin/sh
 
-curl -H "API_KEY: ${API_KEY}" -X POST -d '{"timestamp": "2017-01-02T15:04:05Z"}' http://leunam.dev:9999/file/
+curl -H "API_KEY: ${API_KEY}" -X POST -d '
+{
+	"timestamp": "2020-01-02T15:04:05Z",
+	"absolute_path": "/home/user/file.txt",
+	"contents": "Hello, World!"
+}
+' http://leunam.dev:9999/file/
+
+curl -H "API_KEY: ${API_KEY}" -X GET http://leunam.dev:9999/file/?path=%2Fhome%2Fuser%2Ffile.txt
 
-curl -H "API_KEY: ${API_KEY}" -X GET http://leunam.dev:9999/file/
diff --git a/src/rest-api/handler/handler.go b/src/rest-api/handler/handler.go
index 4b9a426..354378f 100644
--- a/src/rest-api/handler/handler.go
+++ b/src/rest-api/handler/handler.go
@@ -6,7 +6,6 @@ import (
 	"io"
 	"log"
 	"net/http"
-	"strings"
 	"time"
 
 	"github.com/jmoiron/sqlx"
@@ -54,8 +53,8 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 }
 
 func (h Handler) handleGet(w http.ResponseWriter, r *http.Request, user_id string) {
-	_, filePath, ok := strings.Cut(r.URL.Path, "/file/")
-	if !ok {
+	filePath := r.URL.Query().Get("path")
+	if filePath == "" {
 		http.Error(w, "Invalid file path", http.StatusBadRequest)
 		return
 	}
diff --git a/src/rest-api/handler/handler_test.go b/src/rest-api/handler/handler_test.go
index 4709959..183d584 100644
--- a/src/rest-api/handler/handler_test.go
+++ b/src/rest-api/handler/handler_test.go
@@ -24,7 +24,7 @@ func TestHandleGet(t *testing.T) {
 	}
 	db.EXPECT().GetLatestFileByPath(gomock.Any(), "/tmp/file.txt", "USER_ID").Return(file, nil)
 
-	handler.handleGet(recorder, httptest.NewRequest(http.MethodGet, "/file/%2ftmp%2Ffile.txt", nil), "USER_ID")
+	handler.handleGet(recorder, httptest.NewRequest(http.MethodGet, "/file/?path=%2ftmp%2Ffile.txt", nil), "USER_ID")
 
 	require.Equal(t, http.StatusOK, recorder.Code)
 	require.Equal(t, fmt.Sprintln("File: ", file), recorder.Body.String())