From f3a7433e1586da48bfa43f2abd1bd4d532862f3c Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Sat, 15 Jan 2022 12:29:18 +0100 Subject: [PATCH] Register routes for pipelines --- main.go | 17 +++++++++++++++- main_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++ pipeline.go | 5 +++++ pipelines/example.json | 34 +++++++++++++++----------------- 4 files changed, 81 insertions(+), 19 deletions(-) diff --git a/main.go b/main.go index e3d8444..2574244 100644 --- a/main.go +++ b/main.go @@ -15,16 +15,31 @@ type Metadata struct { Version string } +func PipelineHandle(pipeline IPipeline, w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(true) +} + func IndexHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(Metadata{Name, Version}) } +func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) { + for _, pipeline := range pipelines { + r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { + PipelineHandle(pipeline, w, r) + }) + } +} + func main() { - LoadPipelines() + pipelines := LoadPipelines() r := mux.NewRouter() r.HandleFunc("/", IndexHandler) + RegisterPipelineRoutes(r, pipelines) + http.ListenAndServe(":8000", r) } diff --git a/main_test.go b/main_test.go index 513828f..64710c3 100644 --- a/main_test.go +++ b/main_test.go @@ -5,6 +5,7 @@ import ( "net/http/httptest" "testing" + "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -19,3 +20,46 @@ func TestIndexRoute(t *testing.T) { assert.NotNil(t, responseRecorder.Body, "Response should contain body") }) } + +func TestEndpointRoute(t *testing.T) { + // TODO: Use mock/fake for dummy pipeline data + givenEndpoint := Pipeline{ + Name: "example pipeline", + Slug: "example", + Type: 0, + RemoveMetadata: false, + Steps: []Step{ + { + Name: "resize image", + Type: 0, + }, + { + Name: "compress image", + Type: 1, + }, + }, + } + + t.Run("Registered pipelines are valid routes", func(t *testing.T) { + router := mux.NewRouter() + RegisterPipelineRoutes(router, []IPipeline{givenEndpoint}) + + request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 200) + }) + + t.Run("Unregistered pipelines return 404", func(t *testing.T) { + router := mux.NewRouter() + + request, _ := http.NewRequest("GET", "/"+givenEndpoint.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 404) + }) +} diff --git a/pipeline.go b/pipeline.go index 4ea3e9a..c493b23 100644 --- a/pipeline.go +++ b/pipeline.go @@ -21,6 +21,7 @@ type PipelineType int type IPipeline interface { GetName() string + GetSlug() string GetType() PipelineType GetSteps() []Step } @@ -37,6 +38,10 @@ func (p Pipeline) GetName() string { return p.Name } +func (p Pipeline) GetSlug() string { + return p.Slug +} + func (p Pipeline) GetType() PipelineType { return p.Type } diff --git a/pipelines/example.json b/pipelines/example.json index 3cb6290..a8720a7 100644 --- a/pipelines/example.json +++ b/pipelines/example.json @@ -1,18 +1,16 @@ -[ - { - "name": "example pipeline", - "slug": "example", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] - } -] \ No newline at end of file +{ + "name": "example pipeline", + "slug": "example", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] +} \ No newline at end of file