diff --git a/go.mod b/go.mod index 3641441..434e79d 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.17 require github.com/gorilla/mux v1.8.0 require ( + github.com/bxcodec/faker/v3 v3.7.0 // indirect github.com/davecgh/go-spew v1.1.0 // indirect github.com/google/go-cmp v0.5.6 // indirect github.com/hexops/valast v1.4.1 // indirect diff --git a/go.sum b/go.sum index 849fa9a..59db30e 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,6 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/bxcodec/faker/v3 v3.7.0 h1:qWAFFwcyVS0ukF0UoJju1wBLO0cuPQ7JdVBPggM8kNo= +github.com/bxcodec/faker/v3 v3.7.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= diff --git a/main.go b/main.go index e3d8444..0f43e55 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(pipeline) +} + 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..f887bc5 100644 --- a/main_test.go +++ b/main_test.go @@ -1,10 +1,14 @@ package main import ( + "encoding/json" + "fmt" "net/http" "net/http/httptest" "testing" + "github.com/bxcodec/faker/v3" + "github.com/gorilla/mux" "github.com/stretchr/testify/assert" ) @@ -19,3 +23,36 @@ func TestIndexRoute(t *testing.T) { assert.NotNil(t, responseRecorder.Body, "Response should contain body") }) } + +func TestEndpointRoute(t *testing.T) { + data := Pipeline{} + err := faker.FakeData(&data) + if err != nil { + fmt.Println(err) + } + + t.Run("Registered pipelines are valid routes", func(t *testing.T) { + router := mux.NewRouter() + RegisterPipelineRoutes(router, []IPipeline{data}) + + request, _ := http.NewRequest("GET", "/"+data.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 200) + body, _ := json.Marshal(data) + assert.JSONEq(t, string(body), responseRecorder.Body.String()) + }) + + t.Run("Unregistered pipelines return 404", func(t *testing.T) { + router := mux.NewRouter() + + request, _ := http.NewRequest("GET", "/"+data.Slug, nil) + responseRecorder := httptest.NewRecorder() + + router.ServeHTTP(responseRecorder, request) + + assert.Equal(t, responseRecorder.Code, 404) + }) +} diff --git a/pipeline.go b/pipeline.go index 0fd1ca3..56ecc4d 100644 --- a/pipeline.go +++ b/pipeline.go @@ -21,14 +21,16 @@ type PipelineType int type IPipeline interface { GetName() string + GetSlug() string GetType() PipelineType GetSteps() []Step } type Pipeline struct { - Name string `json:"name"` - Type PipelineType `json:"type"` - RemoveMetadata bool `json:"remove_metadata"` + Name string `json:"name" faker:"name"` + Slug string `json:"slug" faker:"word"` + Type PipelineType `json:"type" faker:"-"` + RemoveMetadata bool `json:"remove_metadata" faker:"-"` Steps []Step `json:"steps"` } @@ -36,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 } @@ -62,8 +68,8 @@ const ( ) type Step struct { - Name string - Type StepType + Name string `faker:"name"` + Type StepType `faker:"-"` } func (s Step) Translate() (IExecutableStep, error) { diff --git a/pipeline_test.go b/pipeline_test.go index 38c16e6..0c91123 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -1,8 +1,9 @@ package main import ( - "github.com/stretchr/testify/assert" "testing" + + "github.com/stretchr/testify/assert" ) func TestImagePipelineDeserialization(t *testing.T) { diff --git a/pipelines/example.json b/pipelines/example.json index bc42609..a8720a7 100644 --- a/pipelines/example.json +++ b/pipelines/example.json @@ -1,5 +1,6 @@ { "name": "example pipeline", + "slug": "example", "type": 0, "removeMetadata": false, "steps": [