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 new file mode 100644 index 0000000..431aa8a --- /dev/null +++ b/pipeline.go @@ -0,0 +1,71 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/fs" + "os" + "path/filepath" +) + +var Pipelines []Pipeline + +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 +} + +func DeserializePipelines(pipelines [][]byte) []Pipeline { + values := make([]Pipeline, len(pipelines)) + + for index, pipeline := range pipelines { + var deserializedObject Pipeline + json.Unmarshal(pipeline, &deserializedObject) + values[index] = deserializedObject + } + + return values +} + +func LoadPipelines() { + var files [][]byte + + path, _ := os.Getwd() + + 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) + } + + return nil + }) + + if err != nil { + panic(err) + } + + Pipelines = DeserializePipelines(files) +} diff --git a/pipeline_test.go b/pipeline_test.go new file mode 100644 index 0000000..59450d8 --- /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([][]byte{[]byte(ValidPayload)}) + + assert.Equal(t, 1, len(values), "Output should contain one element") + assert.Equal(t, "example pipeline", values[0].Name) + }) +} 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