diff --git a/pipelines/example.json b/config/example.json similarity index 100% rename from pipelines/example.json rename to config/example.json diff --git a/go.mod b/go.mod index 434e79d..9e60077 100644 --- a/go.mod +++ b/go.mod @@ -2,20 +2,17 @@ module github.com/geplauder/lithium go 1.17 -require github.com/gorilla/mux v1.8.0 +require ( + github.com/bxcodec/faker/v3 v3.7.0 + github.com/gorilla/mux v1.8.0 + github.com/stretchr/testify v1.7.0 +) require ( - github.com/bxcodec/faker/v3 v3.7.0 // indirect github.com/davecgh/go-spew v1.1.0 // indirect - github.com/google/go-cmp v0.5.6 // indirect - github.com/hexops/valast v1.4.1 // indirect + github.com/kr/pretty v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 // indirect - github.com/stretchr/testify v1.7.0 // indirect - golang.org/x/mod v0.5.1 // indirect - golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 // indirect - golang.org/x/tools v0.1.8-0.20211102182255-bb4add04ddef // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4 // indirect + gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect - mvdan.cc/gofumpt v0.2.0 // indirect ) diff --git a/go.sum b/go.sum index 59db30e..4a3d8c4 100644 --- a/go.sum +++ b/go.sum @@ -1,70 +1,30 @@ -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/bxcodec/faker/v3 v3.7.0 h1:qWAFFwcyVS0ukF0UoJju1wBLO0cuPQ7JdVBPggM8kNo= github.com/bxcodec/faker/v3 v3.7.0/go.mod h1:gF31YgnMSMKgkvl+fyEo1xuSMbEuieyqfeslGYFjneM= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og= -github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= -github.com/hexops/autogold v0.8.1/go.mod h1:97HLDXyG23akzAoRYJh/2OBs3kd80eHyKPvZw0S5ZBY= -github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= -github.com/hexops/valast v1.4.1 h1:vlB+usah+MLacCyDDqACn2yhAoCDlpHYkpEKtej+RXE= -github.com/hexops/valast v1.4.1/go.mod h1:G+D6TExWuKs5he+hYlPMfYyhQ8w8qbc2vm4gDWwLdDg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= +github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4 h1:Ha8xCaq6ln1a+R91Km45Oq6lPXj2Mla6CRJYcuV2h1w= github.com/rogpeppe/go-internal v1.8.1-0.20211023094830-115ce09fd6b4/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o= -github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041 h1:llrF3Fs4018ePo4+G/HV/uQUqEI1HMDjCeOf2V6puPc= -github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= -golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211102192858-4dd72447c267/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1 h1:kwrAHlwJ0DUBZwQ238v+Uod/3eZ8B2K5rYsUHBQvzmI= -golang.org/x/sys v0.0.0-20211117180635-dee7805ff2e1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.8-0.20211102182255-bb4add04ddef h1:/DaKawnTFFxdq/mJT3pM+OkeJlq5gc3ZhkbGVYbqOCw= -golang.org/x/tools v0.1.8-0.20211102182255-bb4add04ddef/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -mvdan.cc/gofumpt v0.2.0 h1:AInyCTHfhp4bFrP2VYC5kR2wPwgWj7eGSb+7437zn7I= -mvdan.cc/gofumpt v0.2.0/go.mod h1:TiGmrf914DAuT6+hDIxOqoDb4QXIzAuEUSXqEf9hGKY= diff --git a/main.go b/main.go index 0f43e55..490b7ee 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "github.com/geplauder/lithium/pipelines" "net/http" "github.com/gorilla/mux" @@ -15,7 +16,7 @@ type Metadata struct { Version string } -func PipelineHandle(pipeline IPipeline, w http.ResponseWriter, r *http.Request) { +func PipelineHandler(pipeline pipelines.IPipeline, w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(pipeline) } @@ -25,21 +26,21 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(Metadata{Name, Version}) } -func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) { +func RegisterPipelineRoutes(r *mux.Router, pipelines []pipelines.IPipeline) { for _, pipeline := range pipelines { r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { - PipelineHandle(pipeline, w, r) + PipelineHandler(pipeline, w, r) }) } } func main() { - pipelines := LoadPipelines() + pipes := pipelines.LoadPipelines() r := mux.NewRouter() r.HandleFunc("/", IndexHandler) - RegisterPipelineRoutes(r, pipelines) + RegisterPipelineRoutes(r, pipes) http.ListenAndServe(":8000", r) } diff --git a/main_test.go b/main_test.go index f887bc5..e183155 100644 --- a/main_test.go +++ b/main_test.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "fmt" + "github.com/geplauder/lithium/pipelines" "net/http" "net/http/httptest" "testing" @@ -25,7 +26,7 @@ func TestIndexRoute(t *testing.T) { } func TestEndpointRoute(t *testing.T) { - data := Pipeline{} + data := pipelines.Pipeline{} err := faker.FakeData(&data) if err != nil { fmt.Println(err) @@ -33,7 +34,7 @@ func TestEndpointRoute(t *testing.T) { t.Run("Registered pipelines are valid routes", func(t *testing.T) { router := mux.NewRouter() - RegisterPipelineRoutes(router, []IPipeline{data}) + RegisterPipelineRoutes(router, []pipelines.IPipeline{data}) request, _ := http.NewRequest("GET", "/"+data.Slug, nil) responseRecorder := httptest.NewRecorder() diff --git a/pipelines/executable_step.go b/pipelines/executable_step.go new file mode 100644 index 0000000..1ef14b7 --- /dev/null +++ b/pipelines/executable_step.go @@ -0,0 +1,25 @@ +package pipelines + +type IExecutableStep interface { + Execute() +} + +// Resize image + +type ResizeImageStep struct { + Step +} + +func (s ResizeImageStep) Execute() { + // TODO +} + +// Compress image + +type CompressImageStep struct { + Step +} + +func (s CompressImageStep) Execute() { + // TODO +} diff --git a/pipeline.go b/pipelines/pipeline.go similarity index 61% rename from pipeline.go rename to pipelines/pipeline.go index 56ecc4d..76d6e10 100644 --- a/pipeline.go +++ b/pipelines/pipeline.go @@ -1,8 +1,7 @@ -package main +package pipelines import ( "encoding/json" - "errors" "fmt" "io/fs" "log" @@ -58,68 +57,6 @@ type VideoPipeline struct { Pipeline } -// Steps - -type StepType int - -const ( - TypeResizeImageStep StepType = iota - TypeCompressImageStep -) - -type Step struct { - Name string `faker:"name"` - Type StepType `faker:"-"` -} - -func (s Step) Translate() (IExecutableStep, error) { - var step IExecutableStep - switch s.GetType() { - case TypeResizeImageStep: - step = ResizeImageStep{s} - case TypeCompressImageStep: - step = CompressImageStep{s} - } - - if step == nil { - return nil, errors.New("invalid type") - } - - return step, nil -} - -func (s Step) GetType() StepType { - return s.Type -} - -func (s Step) GetName() string { - return s.Name -} - -type IExecutableStep interface { - Execute() -} - -// Resize image - -type ResizeImageStep struct { - Step -} - -func (s ResizeImageStep) Execute() { - // TODO -} - -// Compress image - -type CompressImageStep struct { - Step -} - -func (s CompressImageStep) Execute() { - // TODO -} - // Deserialization func DeserializePipelines(pipelines [][]byte) []IPipeline { @@ -142,7 +79,7 @@ func LoadPipelines() []IPipeline { path, _ := os.Getwd() - err := filepath.Walk(path+"/pipelines", func(path string, info fs.FileInfo, err error) error { + err := filepath.Walk(path+"/config", func(path string, info fs.FileInfo, err error) error { if err == nil && info.IsDir() == false { fmt.Println(path) data, _ := os.ReadFile(path) diff --git a/pipelines/pipeline_test.go b/pipelines/pipeline_test.go new file mode 100644 index 0000000..abd03c4 --- /dev/null +++ b/pipelines/pipeline_test.go @@ -0,0 +1,59 @@ +package pipelines + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestImagePipelineDeserialization(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 0, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] + }` + + t.Run("Image pipeline deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + + assert.Equal(t, 1, len(values), "Output should contain one element") + assert.Equal(t, "example pipeline", values[0].GetName()) + assert.Equal(t, Image, values[0].GetType()) + }) +} + +func TestVideoPipelineDeserialization(t *testing.T) { + const Payload string = `{ + "name": "example pipeline", + "type": 1, + "removeMetadata": false, + "steps": [ + { + "name": "resize image", + "type": 0 + }, + { + "name": "compress image", + "type": 1 + } + ] + }` + + t.Run("Video pipelines deserialization is successful", func(t *testing.T) { + values := DeserializePipelines([][]byte{[]byte(Payload)}) + + assert.Equal(t, 1, len(values), "Output should contain one element") + assert.Equal(t, "example pipeline", values[0].GetName()) + assert.Equal(t, Video, values[0].GetType()) + }) +} diff --git a/pipelines/step.go b/pipelines/step.go new file mode 100644 index 0000000..eb186c4 --- /dev/null +++ b/pipelines/step.go @@ -0,0 +1,39 @@ +package pipelines + +import "errors" + +type StepType int + +const ( + TypeResizeImageStep StepType = iota + TypeCompressImageStep +) + +type Step struct { + Name string `faker:"name"` + Type StepType `faker:"-"` +} + +func (s Step) Translate() (IExecutableStep, error) { + var step IExecutableStep + switch s.GetType() { + case TypeResizeImageStep: + step = ResizeImageStep{s} + case TypeCompressImageStep: + step = CompressImageStep{s} + } + + if step == nil { + return nil, errors.New("invalid type") + } + + return step, nil +} + +func (s Step) GetType() StepType { + return s.Type +} + +func (s Step) GetName() string { + return s.Name +} diff --git a/pipeline_test.go b/pipelines/step_test.go similarity index 55% rename from pipeline_test.go rename to pipelines/step_test.go index 0c91123..a61eca2 100644 --- a/pipeline_test.go +++ b/pipelines/step_test.go @@ -1,63 +1,10 @@ -package main +package pipelines import ( - "testing" - "github.com/stretchr/testify/assert" + "testing" ) -func TestImagePipelineDeserialization(t *testing.T) { - const Payload string = `{ - "name": "example pipeline", - "type": 0, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] - }` - - t.Run("Image pipeline deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(Payload)}) - - assert.Equal(t, 1, len(values), "Output should contain one element") - assert.Equal(t, "example pipeline", values[0].GetName()) - assert.Equal(t, Image, values[0].GetType()) - }) -} - -func TestVideoPipelineDeserialization(t *testing.T) { - const Payload string = `{ - "name": "example pipeline", - "type": 1, - "removeMetadata": false, - "steps": [ - { - "name": "resize image", - "type": 0 - }, - { - "name": "compress image", - "type": 1 - } - ] - }` - - t.Run("Video pipelines deserialization is successful", func(t *testing.T) { - values := DeserializePipelines([][]byte{[]byte(Payload)}) - - assert.Equal(t, 1, len(values), "Output should contain one element") - assert.Equal(t, "example pipeline", values[0].GetName()) - assert.Equal(t, Video, values[0].GetType()) - }) -} - func TestTranslateStep(t *testing.T) { const Payload string = `{ "name": "example pipeline",