Compare commits

...

2 Commits

Author SHA1 Message Date
Fabian Vowie 08ca3c5b2c
Temporarily hardcode filesystem storage provider 2 years ago
Fabian Vowie 44038e1626
Fix store methods not being public 2 years ago
  1. 15
      main.go
  2. 7
      main_test.go
  3. 27
      storage/storage.go
  4. 4
      storage/storage_test.go

15
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 {

7
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()

27
storage/storage.go

@ -8,8 +8,8 @@ import (
)
type IStorageProvider interface {
storeRaw(bucketName string, objectName string, data []byte) string
storeExisting(bucketName string, objectName string, existingFilePath string) string
StoreRaw(bucketName string, objectName string, data []byte) string
StoreExisting(bucketName string, objectName string, existingFilePath string) string
}
type FileSystemStorageProvider struct {
@ -17,7 +17,7 @@ type FileSystemStorageProvider struct {
basePath string
}
func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName string, data []byte) string {
func (sp FileSystemStorageProvider) StoreRaw(bucketName string, objectName string, data []byte) string {
directoryPath := filepath.Join(sp.basePath, bucketName)
sp.fileSystem.MkdirAll(directoryPath, os.ModePerm)
@ -29,8 +29,25 @@ func (sp FileSystemStorageProvider) storeRaw(bucketName string, objectName strin
return filePath
}
func (sp FileSystemStorageProvider) storeExisting(bucketName string, objectName string, existingFilePath string) string {
func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName string, existingFilePath string) string {
bytesRead, _ := afero.ReadFile(sp.fileSystem, existingFilePath)
return sp.storeRaw(bucketName, objectName, bytesRead)
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",
}
}

4
storage/storage_test.go

@ -19,7 +19,7 @@ func TestFileSystemStorageProvider(t *testing.T) {
basePath: "/tmp/foo/bar",
}
finalPath := provider.storeRaw("test", "test.bin", dummyData)
finalPath := provider.StoreRaw("test", "test.bin", dummyData)
assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath)
exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")
@ -39,7 +39,7 @@ func TestFileSystemStorageProvider(t *testing.T) {
basePath: "/tmp/foo/bar",
}
finalPath := provider.storeExisting("test", "test.bin", "/tmp/existing.bin")
finalPath := provider.StoreExisting("test", "test.bin", "/tmp/existing.bin")
assert.Equal(t, "/tmp/foo/bar/test/test.bin", finalPath)
exists, _ := afero.Exists(fileSystem, "/tmp/foo/bar/test/test.bin")

Loading…
Cancel
Save