Browse Source

Move pipeline files to subpackage

feature/restructure-pipelines
Roman Zipp 3 years ago
parent
commit
7869d1e96f
  1. 9
      main.go
  2. 5
      main_test.go
  3. 2
      pipelines/pipeline.go
  4. 2
      pipelines/pipeline_test.go

9
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 PipelineHandler(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,7 +26,7 @@ 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) {
PipelineHandler(pipeline, w, r)
@ -34,12 +35,12 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) {
}
func main() {
pipelines := LoadPipelines()
pipes := pipelines.LoadPipelines()
r := mux.NewRouter()
r.HandleFunc("/", IndexHandler)
RegisterPipelineRoutes(r, pipelines)
RegisterPipelineRoutes(r, pipes)
http.ListenAndServe(":8000", r)
}

5
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()

2
pipeline.go → pipelines/pipeline.go

@ -1,4 +1,4 @@
package main
package pipelines
import (
"encoding/json"

2
pipeline_test.go → pipelines/pipeline_test.go

@ -1,4 +1,4 @@
package main
package pipelines
import (
"testing"
Loading…
Cancel
Save