From 08ca3c5b2c25af6eb9e278991e4ea9e7bffb839d Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 15:19:38 +0100 Subject: [PATCH] Temporarily hardcode filesystem storage provider --- main.go | 15 ++++++++++----- main_test.go | 7 +++++-- storage/storage.go | 15 +++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index 0b60c73..aef8790 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,10 @@ package main import ( "encoding/json" - "github.com/geplauder/lithium/pipelines" "net/http" + "github.com/geplauder/lithium/pipelines" + "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" ) @@ -16,7 +17,7 @@ type Metadata struct { Version string } -func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { +func PipelineHandler(pipeline pipelines.IPipeline, storageProvider storage.IStorageProvider, w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(pipeline) if err != nil { @@ -32,21 +33,25 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { } } -func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline) { +func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline, storageProvider storage.IStorageProvider) { for _, pipeline := range pipelines { r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { - PipelineHandler(pipeline, w, r) + PipelineHandler(pipeline, storageProvider, w, r) }) } } func main() { + storageProvider := storage.GetFileSystemStorageProvider("test") + + storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10}) + pipes := pipelines.LoadPipelines() r := mux.NewRouter() r.HandleFunc("/", IndexHandler) - RegisterPipelineRoutes(r, pipes) + RegisterPipelineRoutes(r, pipes, storageProvider) err := http.ListenAndServe(":8000", r) if err != nil { diff --git a/main_test.go b/main_test.go index e183155..6f33974 100644 --- a/main_test.go +++ b/main_test.go @@ -3,12 +3,13 @@ package main import ( "encoding/json" "fmt" - "github.com/geplauder/lithium/pipelines" "net/http" "net/http/httptest" "testing" "github.com/bxcodec/faker/v3" + "github.com/geplauder/lithium/pipelines" + "github.com/geplauder/lithium/storage" "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -34,7 +35,9 @@ func TestEndpointRoute(t *testing.T) { t.Run("Registered pipelines are valid routes", func(t *testing.T) { router := mux.NewRouter() - RegisterPipelineRoutes(router, []pipelines.IPipeline{data}) + fs := storage.GetMemoryStorageProvider() + + RegisterPipelineRoutes(router, []pipelines.IPipeline{data}, fs) request, _ := http.NewRequest("GET", "/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() diff --git a/storage/storage.go b/storage/storage.go index 102bd27..9e898a5 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -35,4 +35,19 @@ func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName return sp.StoreRaw(bucketName, objectName, bytesRead) } +func GetFileSystemStorageProvider(basePath string) FileSystemStorageProvider { + wd, _ := os.Getwd() + + return FileSystemStorageProvider{ + fileSystem: afero.NewBasePathFs(afero.NewOsFs(), filepath.Join(wd, "files")), + basePath: basePath, + } +} + +// TODO: Move this out of this file +func GetMemoryStorageProvider() FileSystemStorageProvider { + return FileSystemStorageProvider{ + fileSystem: afero.NewBasePathFs(afero.NewMemMapFs(), "/"), + basePath: "/tmp/foo/bar", + } }