From 1f29574118563863a7ba50d0511c124ecf32382d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zlatko=20=C4=8Calu=C5=A1i=C4=87?= Date: Tue, 27 Dec 2016 12:26:41 +0100 Subject: [PATCH] Add debug flag --- handlers.go | 24 ++++++++++++++++++++++++ main.go | 2 ++ router.go | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/handlers.go b/handlers.go index 5ac5f32..5d8846a 100644 --- a/handlers.go +++ b/handlers.go @@ -63,6 +63,9 @@ func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc { // CheckConfig returns a http.HandlerFunc that checks whether a configuration exists. func CheckConfig(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("CheckConfig()") + } config := filepath.Join(c.path, "config") st, err := os.Stat(config) if err != nil { @@ -77,6 +80,9 @@ func CheckConfig(c *Context) http.HandlerFunc { // GetConfig returns a http.HandlerFunc that allows for a config to be retrieved. func GetConfig(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("GetConfig()") + } config := filepath.Join(c.path, "config") bytes, err := ioutil.ReadFile(config) if err != nil { @@ -91,6 +97,9 @@ func GetConfig(c *Context) http.HandlerFunc { // SaveConfig returns a http.HandlerFunc that allows for a config to be saved. func SaveConfig(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("SaveConfig()") + } config := filepath.Join(c.path, "config") bytes, err := ioutil.ReadAll(r.Body) if err != nil { @@ -109,6 +118,9 @@ func SaveConfig(c *Context) http.HandlerFunc { // ListBlobs returns a http.HandlerFunc that lists all blobs of a given type in an arbitrary order. func ListBlobs(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("ListBlobs()") + } vars := strings.Split(r.RequestURI, "/") dir := vars[1] path := filepath.Join(c.path, dir) @@ -149,6 +161,9 @@ func ListBlobs(c *Context) http.HandlerFunc { // CheckBlob returns a http.HandlerFunc that tests whether a blob exists and returns 200, if it does, or 404 otherwise. func CheckBlob(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("CheckBlob()") + } vars := strings.Split(r.RequestURI, "/") dir := vars[1] name := vars[2] @@ -171,6 +186,9 @@ func CheckBlob(c *Context) http.HandlerFunc { // GetBlob returns a http.HandlerFunc that retrieves a blob from the repository. func GetBlob(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("GetBlob()") + } vars := strings.Split(r.RequestURI, "/") dir := vars[1] name := vars[2] @@ -194,6 +212,9 @@ func GetBlob(c *Context) http.HandlerFunc { // SaveBlob returns a http.HandlerFunc that saves a blob to the repository. func SaveBlob(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("SaveBlob()") + } vars := strings.Split(r.RequestURI, "/") dir := vars[1] name := vars[2] @@ -249,6 +270,9 @@ func SaveBlob(c *Context) http.HandlerFunc { // DeleteBlob returns a http.HandlerFunc that deletes a blob from the repository. func DeleteBlob(c *Context) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { + if *debug { + log.Println("DeleteBlob()") + } vars := strings.Split(r.RequestURI, "/") dir := vars[1] name := vars[2] diff --git a/main.go b/main.go index b6d6a3f..108bb9f 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,8 @@ import ( "runtime/pprof" ) +var debug = flag.Bool("debug", false, "output debug messages") + func setupRoutes(path string) *Router { context := &Context{path} diff --git a/router.go b/router.go index d4cef89..07f0edf 100644 --- a/router.go +++ b/router.go @@ -1,6 +1,7 @@ package main import ( + "log" "net/http" "strings" ) @@ -114,6 +115,10 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) { uri := r.RequestURI path := strings.Split(uri, "/") + if *debug { + log.Printf("%s %s", method, uri) + } + ROUTE: for _, route := range router.routes[method] { if len(route.path) != len(path) {