From 382720d2423e2a6b5c9903a2816f5944617ec668 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 18:45:04 +0100 Subject: [PATCH 1/4] Add pipeline definition structs --- pipeline.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 pipeline.go diff --git a/pipeline.go b/pipeline.go new file mode 100644 index 0000000..c010406 --- /dev/null +++ b/pipeline.go @@ -0,0 +1,28 @@ +package main + +type PipelineType int +type PipelineStepType int + +const ( + Image PipelineType = iota + Video +) + +const ( + Resize PipelineStepType = iota + Compress + Encode +) + +type Pipeline struct { + Name string + Type PipelineType + RemoveMetadata bool + Steps []Step +} + +type Step struct { + Name string + Type PipelineStepType +} + From 5025ce49ec32a71ad42aa186ce946119e75086d3 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 18:45:20 +0100 Subject: [PATCH 2/4] Add deserialization for pipelines --- pipeline.go | 13 +++++++++++++ pipeline_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 pipeline_test.go diff --git a/pipeline.go b/pipeline.go index c010406..9a12672 100644 --- a/pipeline.go +++ b/pipeline.go @@ -1,5 +1,7 @@ package main +import "encoding/json" + type PipelineType int type PipelineStepType int @@ -26,3 +28,14 @@ type Step struct { Type PipelineStepType } +func DeserializePipelines(pipelines []string) []Pipeline { + values := make([]Pipeline, len(pipelines)) + + for index, pipeline := range pipelines { + var deserializedObject Pipeline + json.Unmarshal([]byte(pipeline), &deserializedObject) + values[index] = deserializedObject + } + + return values +} diff --git a/pipeline_test.go b/pipeline_test.go new file mode 100644 index 0000000..9b7a135 --- /dev/null +++ b/pipeline_test.go @@ -0,0 +1,32 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +const ValidPayload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] +}` + +func TestPipelineDeserialization(t *testing.T) { + t.Run("Pipelines deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([]string{ValidPayload}) + + assert.Equal(t, 1, len(values), "Output should contain one element") + assert.Equal(t, "example pipeline", values[0].Name) + }) +} From 836d699400e767fd4a194cc2d6a32f6554dc9f52 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 19:00:05 +0100 Subject: [PATCH 3/4] Use byte slice instead of string slice for DeserializePipelines --- pipeline.go | 24 +++++++++++++++++++++--- pipeline_test.go | 2 +- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/pipeline.go b/pipeline.go index 9a12672..dfa6681 100644 --- a/pipeline.go +++ b/pipeline.go @@ -1,6 +1,10 @@ package main -import "encoding/json" +import ( + "encoding/json" +) + +var Pipelines []Pipeline type PipelineType int type PipelineStepType int @@ -28,14 +32,28 @@ type Step struct { Type PipelineStepType } -func DeserializePipelines(pipelines []string) []Pipeline { +func DeserializePipelines(pipelines [][]byte) []Pipeline { values := make([]Pipeline, len(pipelines)) for index, pipeline := range pipelines { var deserializedObject Pipeline - json.Unmarshal([]byte(pipeline), &deserializedObject) + json.Unmarshal(pipeline, &deserializedObject) values[index] = deserializedObject } return values } + +// func LoadPipelines() { +// var files []string + +// err := filepath.Walk("./pipelines", func(path string, info fs.FileInfo, err error) error { +// data, err := os.ReadFile(path) + +// return nil +// }) + +// if err != nil { +// panic(err) +// } +// } diff --git a/pipeline_test.go b/pipeline_test.go index 9b7a135..59450d8 100644 --- a/pipeline_test.go +++ b/pipeline_test.go @@ -24,7 +24,7 @@ const ValidPayload string = `{ func TestPipelineDeserialization(t *testing.T) { t.Run("Pipelines deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([]string{ValidPayload}) + values := DeserializePipelines([][]byte{[]byte(ValidPayload)}) assert.Equal(t, 1, len(values), "Output should contain one element") assert.Equal(t, "example pipeline", values[0].Name) From 7717df32e0b46fc7a182729646f155afb1c95a18 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 19:12:54 +0100 Subject: [PATCH 4/4] Load pipelines on program start --- main.go | 2 ++ pipeline.go | 32 ++++++++++++++++++++++---------- pipelines/example.json | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 10 deletions(-) create mode 100644 pipelines/example.json diff --git a/main.go b/main.go index 5057f09..e3d8444 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,8 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { } func main() { + LoadPipelines() + r := mux.NewRouter() r.HandleFunc("/", IndexHandler) diff --git a/pipeline.go b/pipeline.go index dfa6681..431aa8a 100644 --- a/pipeline.go +++ b/pipeline.go @@ -2,6 +2,10 @@ package main import ( "encoding/json" + "fmt" + "io/fs" + "os" + "path/filepath" ) var Pipelines []Pipeline @@ -44,16 +48,24 @@ func DeserializePipelines(pipelines [][]byte) []Pipeline { return values } -// func LoadPipelines() { -// var files []string +func LoadPipelines() { + var files [][]byte -// err := filepath.Walk("./pipelines", func(path string, info fs.FileInfo, err error) error { -// data, err := os.ReadFile(path) + path, _ := os.Getwd() -// return nil -// }) + err := filepath.Walk(path+"/pipelines", func(path string, info fs.FileInfo, err error) error { + if err == nil && info.IsDir() == false { + fmt.Println(path) + data, _ := os.ReadFile(path) + files = append(files, data) + } -// if err != nil { -// panic(err) -// } -// } + return nil + }) + + if err != nil { + panic(err) + } + + Pipelines = DeserializePipelines(files) +} diff --git a/pipelines/example.json b/pipelines/example.json new file mode 100644 index 0000000..bc42609 --- /dev/null +++ b/pipelines/example.json @@ -0,0 +1,15 @@ +{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] +} \ No newline at end of file