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 ( import (
"encoding/json" "encoding/json"
"github.com/geplauder/lithium/pipelines"
"net/http" "net/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -15,7 +16,7 @@ type Metadata struct {
Version string 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") w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(pipeline) json.NewEncoder(w).Encode(pipeline)
} }
@ -25,7 +26,7 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(Metadata{Name, Version}) 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 { for _, pipeline := range pipelines {
r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) { r.HandleFunc("/"+pipeline.GetSlug(), func(w http.ResponseWriter, r *http.Request) {
PipelineHandler(pipeline, w, r) PipelineHandler(pipeline, w, r)
@ -34,12 +35,12 @@ func RegisterPipelineRoutes(r *mux.Router, pipelines []IPipeline) {
} }
func main() { func main() {
pipelines := LoadPipelines()
pipes := pipelines.LoadPipelines()
r := mux.NewRouter() r := mux.NewRouter()
r.HandleFunc("/", IndexHandler) r.HandleFunc("/", IndexHandler)
RegisterPipelineRoutes(r, pipelines)
RegisterPipelineRoutes(r, pipes)
http.ListenAndServe(":8000", r) http.ListenAndServe(":8000", r)
} }

5
main_test.go

@ -3,6 +3,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/geplauder/lithium/pipelines"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -25,7 +26,7 @@ func TestIndexRoute(t *testing.T) {
} }
func TestEndpointRoute(t *testing.T) { func TestEndpointRoute(t *testing.T) {
data := Pipeline{}
data := pipelines.Pipeline{}
err := faker.FakeData(&data) err := faker.FakeData(&data)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -33,7 +34,7 @@ func TestEndpointRoute(t *testing.T) {
t.Run("Registered pipelines are valid routes", func(t *testing.T) { t.Run("Registered pipelines are valid routes", func(t *testing.T) {
router := mux.NewRouter() router := mux.NewRouter()
RegisterPipelineRoutes(router, []IPipeline{data})
RegisterPipelineRoutes(router, []pipelines.IPipeline{data})
request, _ := http.NewRequest("GET", "/"+data.Slug, nil) request, _ := http.NewRequest("GET", "/"+data.Slug, nil)
responseRecorder := httptest.NewRecorder() responseRecorder := httptest.NewRecorder()

2
pipeline.go → pipelines/pipeline.go

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

2
pipeline_test.go → pipelines/pipeline_test.go

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