From 7717df32e0b46fc7a182729646f155afb1c95a18 Mon Sep 17 00:00:00 2001 From: Fabian Vowie Date: Tue, 4 Jan 2022 19:12:54 +0100 Subject: [PATCH] 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