Browse Source

Temporarily hardcode filesystem storage provider

Fabian Vowie 3 years ago
parent
commit
08af716f9c
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 15
      main.go
  2. 7
      main_test.go
  3. 17
      storage/storage.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()

17
storage/storage.go

@ -34,3 +34,20 @@ 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",
}
}
Loading…
Cancel
Save