Browse Source

Register routes for pipelines

feature/add-pipeline-endpoints
Fabian Vowie 2 years ago
parent
commit
f3a7433e15
No known key found for this signature in database GPG Key ID: C27317C33B27C410
  1. 17
      main.go
  2. 44
      main_test.go
  3. 5
      pipeline.go
  4. 34
      pipelines/example.json

17
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)
}

44
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)
})
}

5
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
}

34
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
}
]
}
]
{
"name": "example pipeline",
"slug": "example",
"type": 0,
"removeMetadata": false,
"steps": [
{
"name": "resize image",
"type": 0
},
{
"name": "compress image",
"type": 1
}
]
}
Loading…
Cancel
Save