Browse Source

Temporarily hardcode filesystem storage provider

feature/update-route-registration
Fabian Vowie 3 years ago
parent
commit
81d6046d01
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 ( import (
"encoding/json" "encoding/json"
"github.com/geplauder/lithium/pipelines"
"net/http" "net/http"
"github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/storage"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -16,7 +17,7 @@ type Metadata struct {
Version string 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") w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(pipeline) err := json.NewEncoder(w).Encode(pipeline)
if err != nil { 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 { for _, pipeline := range pipelines {
r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
PipelineHandler(pipeline, w, r)
PipelineHandler(pipeline, storageProvider, w, r)
}) })
} }
} }
func main() { func main() {
storageProvider := storage.GetFileSystemStorageProvider("test")
storageProvider.StoreRaw("abc", "def.test", []byte{0x12, 0x10})
pipes := pipelines.LoadPipelines() pipes := pipelines.LoadPipelines()
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", IndexHandler) r.HandleFunc("/", IndexHandler)
RegisterPipelineRoutes(r, pipes)
RegisterPipelineRoutes(r, pipes, storageProvider)
err := http.ListenAndServe(":8000", r) err := http.ListenAndServe(":8000", r)
if err != nil { if err != nil {

7
main_test.go

@ -3,12 +3,13 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/geplauder/lithium/pipelines"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"github.com/bxcodec/faker/v3" "github.com/bxcodec/faker/v3"
"github.com/geplauder/lithium/pipelines"
"github.com/geplauder/lithium/storage"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/stretchr/testify/assert" "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) { t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
RegisterPipelineRoutes(router, []pipelines.IPipeline{data})
fs := storage.GetMemoryStorageProvider()
RegisterPipelineRoutes(router, []pipelines.IPipeline{data}, fs)
request, _ := http.NewRequest("GET", "/"+data.Slug, nil) request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder() responseRecorder := httptest.NewRecorder()

17
storage/storage.go

@ -34,3 +34,20 @@ func (sp FileSystemStorageProvider) StoreExisting(bucketName string, objectName
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",
}
}
Loading…
Cancel
Save