diff --git a/glide.yaml b/glide.yaml index 5c70395..5c61c39 100644 --- a/glide.yaml +++ b/glide.yaml @@ -2,3 +2,5 @@ package: github.com/mastertinner/s3manager import: - package: github.com/minio/minio-go version: ^2.0.2 +- package: github.com/gorilla/mux + version: ^1.1.0 diff --git a/handlers.go b/handlers.go index 934e02d..eff4f84 100644 --- a/handlers.go +++ b/handlers.go @@ -11,13 +11,13 @@ import ( minio "github.com/minio/minio-go" ) -// indexHandler forwards to "/buckets" -func indexHandler(w http.ResponseWriter, r *http.Request) { +// indexPageHandler forwards to "/buckets" +func indexPageHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/buckets", http.StatusPermanentRedirect) } // bucketsHandler handles the main page -func bucketsHandler(w http.ResponseWriter, r *http.Request) { +func bucketsPageHandler(w http.ResponseWriter, r *http.Request) { lp := path.Join("templates", "layout.html") ip := path.Join("templates", "index.html") @@ -38,7 +38,7 @@ func bucketsHandler(w http.ResponseWriter, r *http.Request) { } // bucketHandler handles the main page -func bucketHandler(w http.ResponseWriter, r *http.Request) { +func bucketPageHandler(w http.ResponseWriter, r *http.Request) { bucket := strings.Split(r.URL.Path, "/")[2] var objects []minio.ObjectInfo diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..905b578 --- /dev/null +++ b/logger.go @@ -0,0 +1,24 @@ +package main + +import ( + "log" + "net/http" + "time" +) + +// Logger logs HTTP requests +func Logger(inner http.Handler, name string) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + inner.ServeHTTP(w, r) + + log.Printf( + "%s\t%s\t%s\t%s", + r.Method, + r.RequestURI, + name, + time.Since(start), + ) + }) +} diff --git a/main.go b/main.go index 19b20c4..9223566 100644 --- a/main.go +++ b/main.go @@ -41,9 +41,7 @@ func main() { panic(err) } - http.HandleFunc("/", indexHandler) - http.HandleFunc("/buckets", bucketsHandler) - http.HandleFunc("/buckets/", bucketHandler) + router := NewRouter() - log.Fatal(http.ListenAndServe(":"+port, nil)) + log.Fatal(http.ListenAndServe(":"+port, router)) } diff --git a/router.go b/router.go new file mode 100644 index 0000000..327aeb3 --- /dev/null +++ b/router.go @@ -0,0 +1,27 @@ +package main + +import ( + "net/http" + + "github.com/gorilla/mux" +) + +// NewRouter creates a new router +func NewRouter() *mux.Router { + router := mux.NewRouter().StrictSlash(true) + + for _, route := range routes { + var handler http.Handler + + handler = route.HandlerFunc + handler = Logger(handler, route.Name) + + router. + Methods(route.Method). + Path(route.Pattern). + Name(route.Name). + Handler(handler) + } + + return router +} diff --git a/routes.go b/routes.go new file mode 100644 index 0000000..66f401a --- /dev/null +++ b/routes.go @@ -0,0 +1,35 @@ +package main + +import "net/http" + +// Route represents a path of the API +type Route struct { + Name string + Method string + Pattern string + HandlerFunc http.HandlerFunc +} + +// Routes is an array of routes +type Routes []Route + +var routes = Routes{ + Route{ + "Redirect to /buckets", + "GET", + "/", + indexPageHandler, + }, + Route{ + "Load Buckets Page", + "GET", + "/buckets", + bucketsPageHandler, + }, + Route{ + "Load Bucket Page", + "GET", + "/buckets/{bucketID}", + bucketPageHandler, + }, +}