From fc6a2a45cb3f45482a72754c8dc3f6a6071671a4 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Sun, 2 Nov 2014 18:56:33 -0500 Subject: [PATCH] Setting up initial cli options and http handlers --- Makefile | 5 ++++- gateway.go | 15 +++++++++++++++ gateway_test.go | 27 +++++++++++++++++++++++++++ minio/main.go | 34 +++++++++++++++++++++++++++++++--- server.go | 21 --------------------- storage.go | 15 +++++++++++++++ storage_test.go | 27 +++++++++++++++++++++++++++ 7 files changed, 119 insertions(+), 25 deletions(-) create mode 100644 gateway.go create mode 100644 gateway_test.go delete mode 100644 server.go create mode 100644 storage.go create mode 100644 storage_test.go diff --git a/Makefile b/Makefile index 53a8a4201..2aa4591ad 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ GOPATH := $(CURDIR)/tmp/gopath -all: build copy_bin +all: build test copy_bin copy_bin: cp tmp/gopath/bin/* bin/ @@ -12,6 +12,9 @@ stage_build: rsync -a . tmp/gopath/src/github.com/minios/minios/ rsync -a third_party/* tmp/gopath +test: + go test github.com/minios/minios + go test github.com/minios/minios/minio build: stage_build go install github.com/minios/minios/minio diff --git a/gateway.go b/gateway.go new file mode 100644 index 000000000..554981b9a --- /dev/null +++ b/gateway.go @@ -0,0 +1,15 @@ +package minio + +import ( + "fmt" + "github.com/gorilla/mux" + "net/http" +) + +func GatewayHandler(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Gateway") +} + +func RegisterGatewayHandlers(router *mux.Router) { + router.HandleFunc("/gateway/rpc", GatewayHandler) +} diff --git a/gateway_test.go b/gateway_test.go new file mode 100644 index 000000000..d4712b17d --- /dev/null +++ b/gateway_test.go @@ -0,0 +1,27 @@ +package minio + +import ( + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "testing" +) + +func TestPrintsGateway(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(GatewayHandler)) + defer server.Close() + res, err := http.Get(server.URL) + if err != nil { + log.Fatal(err) + } + body, err := ioutil.ReadAll(res.Body) + res.Body.Close() + if err != nil { + log.Fatal(err) + } + bodyString := string(body) + if bodyString != "Gateway" { + log.Fatal("Expected 'Gateway', Received '" + bodyString + "'") + } +} diff --git a/minio/main.go b/minio/main.go index 7470e762a..cfb4e60b4 100644 --- a/minio/main.go +++ b/minio/main.go @@ -2,12 +2,40 @@ package main import ( "github.com/codegangsta/cli" + "github.com/gorilla/mux" "github.com/minios/minios" + "log" + "net/http" "os" ) func main() { - cli.NewApp().Run(os.Args) - server := minio.Server{} - server.Start() + app := cli.NewApp() + router := mux.NewRouter() + runServer := false + app.Commands = []cli.Command{ + { + Name: "storage", + Usage: "Start a storage node", + Action: func(c *cli.Context) { + minio.RegisterStorageHandlers(router) + runServer = true + }, + }, + { + Name: "gateway", + Usage: "Start a gateway node", + Action: func(c *cli.Context) { + minio.RegisterGatewayHandlers(router) + runServer = true + }, + }, + } + err := app.Run(os.Args) + if err != nil { + log.Fatal("App failed to load", err) + } + if runServer { + log.Fatal(http.ListenAndServe(":8080", router)) + } } diff --git a/server.go b/server.go deleted file mode 100644 index 2e992a1a6..000000000 --- a/server.go +++ /dev/null @@ -1,21 +0,0 @@ -package minio - -import ( - "fmt" - "github.com/gorilla/mux" - "net/http" -) - -type Server struct { -} - -func (server *Server) Start() error { - r := mux.NewRouter() - r.HandleFunc("/", HelloHandler) - fmt.Println("Running http server on port 8080") - return http.ListenAndServe(":8080", r) -} - -func HelloHandler(w http.ResponseWriter, req *http.Request) { - fmt.Fprintf(w, "Host: "+req.Host) -} diff --git a/storage.go b/storage.go new file mode 100644 index 000000000..4861996d0 --- /dev/null +++ b/storage.go @@ -0,0 +1,15 @@ +package minio + +import ( + "fmt" + "github.com/gorilla/mux" + "net/http" +) + +func RegisterStorageHandlers(router *mux.Router) { + router.HandleFunc("/storage/rpc", StorageHandler) +} + +func StorageHandler(w http.ResponseWriter, req *http.Request) { + fmt.Fprintf(w, "Storage") +} diff --git a/storage_test.go b/storage_test.go new file mode 100644 index 000000000..5f2c67135 --- /dev/null +++ b/storage_test.go @@ -0,0 +1,27 @@ +package minio + +import ( + "io/ioutil" + "log" + "net/http" + "net/http/httptest" + "testing" +) + +func TestPrintsStorage(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(StorageHandler)) + defer server.Close() + res, err := http.Get(server.URL) + if err != nil { + log.Fatal(err) + } + body, err := ioutil.ReadAll(res.Body) + res.Body.Close() + if err != nil { + log.Fatal(err) + } + bodyString := string(body) + if bodyString != "Storage" { + log.Fatal("Expected 'Storage', Received '" + bodyString + "'") + } +}