Browse Source
Merge commit '7717df32e0b46fc7a182729646f155afb1c95a18' into HEAD
feature/improve-pipeline-abstraction
Jenkins
3 years ago
committed by
Fabian Vowie
No known key found for this signature in database
GPG Key ID: C27317C33B27C410
4 changed files with
120 additions and
0 deletions
-
main.go
-
pipeline.go
-
pipeline_test.go
-
pipelines/example.json
|
@ -21,6 +21,8 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func main() { |
|
|
func main() { |
|
|
|
|
|
LoadPipelines() |
|
|
|
|
|
|
|
|
r := mux.NewRouter() |
|
|
r := mux.NewRouter() |
|
|
r.HandleFunc("/", IndexHandler) |
|
|
r.HandleFunc("/", IndexHandler) |
|
|
|
|
|
|
|
|
|
@ -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) |
|
|
|
|
|
} |
|
@ -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) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
@ -0,0 +1,15 @@ |
|
|
|
|
|
{ |
|
|
|
|
|
"name": "example pipeline", |
|
|
|
|
|
"type": 0, |
|
|
|
|
|
"removeMetadata": false, |
|
|
|
|
|
"steps": [ |
|
|
|
|
|
{ |
|
|
|
|
|
"name": "resize image", |
|
|
|
|
|
"type": 0 |
|
|
|
|
|
}, |
|
|
|
|
|
{ |
|
|
|
|
|
"name": "compress image", |
|
|
|
|
|
"type": 1 |
|
|
|
|
|
} |
|
|
|
|
|
] |
|
|
|
|
|
} |