Add debug flag

This commit is contained in:
Zlatko Čalušić 2016-12-27 12:26:41 +01:00
parent 061d31829d
commit 1f29574118
3 changed files with 31 additions and 0 deletions

View file

@ -63,6 +63,9 @@ func AuthHandler(f *HtpasswdFile, h http.Handler) http.HandlerFunc {
// CheckConfig returns a http.HandlerFunc that checks whether a configuration exists. // CheckConfig returns a http.HandlerFunc that checks whether a configuration exists.
func CheckConfig(c *Context) http.HandlerFunc { func CheckConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("CheckConfig()")
}
config := filepath.Join(c.path, "config") config := filepath.Join(c.path, "config")
st, err := os.Stat(config) st, err := os.Stat(config)
if err != nil { 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. // GetConfig returns a http.HandlerFunc that allows for a config to be retrieved.
func GetConfig(c *Context) http.HandlerFunc { func GetConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("GetConfig()")
}
config := filepath.Join(c.path, "config") config := filepath.Join(c.path, "config")
bytes, err := ioutil.ReadFile(config) bytes, err := ioutil.ReadFile(config)
if err != nil { 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. // SaveConfig returns a http.HandlerFunc that allows for a config to be saved.
func SaveConfig(c *Context) http.HandlerFunc { func SaveConfig(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("SaveConfig()")
}
config := filepath.Join(c.path, "config") config := filepath.Join(c.path, "config")
bytes, err := ioutil.ReadAll(r.Body) bytes, err := ioutil.ReadAll(r.Body)
if err != nil { 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. // ListBlobs returns a http.HandlerFunc that lists all blobs of a given type in an arbitrary order.
func ListBlobs(c *Context) http.HandlerFunc { func ListBlobs(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("ListBlobs()")
}
vars := strings.Split(r.RequestURI, "/") vars := strings.Split(r.RequestURI, "/")
dir := vars[1] dir := vars[1]
path := filepath.Join(c.path, dir) 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. // 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 { func CheckBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("CheckBlob()")
}
vars := strings.Split(r.RequestURI, "/") vars := strings.Split(r.RequestURI, "/")
dir := vars[1] dir := vars[1]
name := vars[2] 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. // GetBlob returns a http.HandlerFunc that retrieves a blob from the repository.
func GetBlob(c *Context) http.HandlerFunc { func GetBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("GetBlob()")
}
vars := strings.Split(r.RequestURI, "/") vars := strings.Split(r.RequestURI, "/")
dir := vars[1] dir := vars[1]
name := vars[2] 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. // SaveBlob returns a http.HandlerFunc that saves a blob to the repository.
func SaveBlob(c *Context) http.HandlerFunc { func SaveBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("SaveBlob()")
}
vars := strings.Split(r.RequestURI, "/") vars := strings.Split(r.RequestURI, "/")
dir := vars[1] dir := vars[1]
name := vars[2] 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. // DeleteBlob returns a http.HandlerFunc that deletes a blob from the repository.
func DeleteBlob(c *Context) http.HandlerFunc { func DeleteBlob(c *Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if *debug {
log.Println("DeleteBlob()")
}
vars := strings.Split(r.RequestURI, "/") vars := strings.Split(r.RequestURI, "/")
dir := vars[1] dir := vars[1]
name := vars[2] name := vars[2]

View file

@ -9,6 +9,8 @@ import (
"runtime/pprof" "runtime/pprof"
) )
var debug = flag.Bool("debug", false, "output debug messages")
func setupRoutes(path string) *Router { func setupRoutes(path string) *Router {
context := &Context{path} context := &Context{path}

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"log"
"net/http" "net/http"
"strings" "strings"
) )
@ -114,6 +115,10 @@ func (router *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
uri := r.RequestURI uri := r.RequestURI
path := strings.Split(uri, "/") path := strings.Split(uri, "/")
if *debug {
log.Printf("%s %s", method, uri)
}
ROUTE: ROUTE:
for _, route := range router.routes[method] { for _, route := range router.routes[method] {
if len(route.path) != len(path) { if len(route.path) != len(path) {