From 2300aa3a635118d02f5faad5b3dc27a52d091996 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 15 Sep 2015 21:40:46 +0100 Subject: [PATCH] add browse file; move function to utils pkg --- browse/browse.go | 14 ++++++++++++++ hugo.go | 30 ++++-------------------------- templates/content.tmpl | 0 utils/utils.go | 20 ++++++++++++++++++++ 4 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 browse/browse.go delete mode 100644 templates/content.tmpl diff --git a/browse/browse.go b/browse/browse.go new file mode 100644 index 00000000..aedf318e --- /dev/null +++ b/browse/browse.go @@ -0,0 +1,14 @@ +package browse + +import ( + "net/http" + + "github.com/hacdias/caddy-hugo/page" +) + +// Execute sth +func Execute(w http.ResponseWriter, r *http.Request) (int, error) { + page := new(page.Page) + page.Title = "Browse" + return page.Render(w, r, "browse") +} diff --git a/hugo.go b/hugo.go index d3d3e6f3..9c939766 100644 --- a/hugo.go +++ b/hugo.go @@ -9,8 +9,10 @@ import ( "strings" "github.com/hacdias/caddy-hugo/assets" + "github.com/hacdias/caddy-hugo/browse" "github.com/hacdias/caddy-hugo/edit" "github.com/hacdias/caddy-hugo/settings" + "github.com/hacdias/caddy-hugo/utils" "github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/middleware" "github.com/spf13/hugo/commands" @@ -29,7 +31,7 @@ type handler struct{ Next middleware.Handler } func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { if middleware.Path(r.URL.Path).Matches("/admin") { - page := parseComponents(r)[1] + page := utils.ParseComponents(r)[1] if page == "static" { filename := strings.Replace(r.URL.Path, "/admin/", "", 1) @@ -47,12 +49,8 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) w.Write(file) return 200, nil - } else if page == "content" { - w.Write([]byte("Content Page")) - return 200, nil } else if page == "browse" { - w.Write([]byte("Show Data Folder")) - return 200, nil + return browse.Execute(w, r) } else if page == "edit" { return edit.Execute(w, r) } else if page == "settings" { @@ -64,23 +62,3 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) return h.Next.ServeHTTP(w, r) } - -// TODO: utils package -func parseComponents(r *http.Request) []string { - //The URL that the user queried. - path := r.URL.Path - path = strings.TrimSpace(path) - //Cut off the leading and trailing forward slashes, if they exist. - //This cuts off the leading forward slash. - if strings.HasPrefix(path, "/") { - path = path[1:] - } - //This cuts off the trailing forward slash. - if strings.HasSuffix(path, "/") { - cutOffLastCharLen := len(path) - 1 - path = path[:cutOffLastCharLen] - } - //We need to isolate the individual components of the path. - components := strings.Split(path, "/") - return components -} diff --git a/templates/content.tmpl b/templates/content.tmpl deleted file mode 100644 index e69de29b..00000000 diff --git a/utils/utils.go b/utils/utils.go index 1f5433ff..8ad674d3 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -2,6 +2,7 @@ package utils import ( "errors" + "net/http" "reflect" "strings" "unicode" @@ -57,3 +58,22 @@ func SplitCapitalize(name string) string { return name } + +func ParseComponents(r *http.Request) []string { + //The URL that the user queried. + path := r.URL.Path + path = strings.TrimSpace(path) + //Cut off the leading and trailing forward slashes, if they exist. + //This cuts off the leading forward slash. + if strings.HasPrefix(path, "/") { + path = path[1:] + } + //This cuts off the trailing forward slash. + if strings.HasSuffix(path, "/") { + cutOffLastCharLen := len(path) - 1 + path = path[:cutOffLastCharLen] + } + //We need to isolate the individual components of the path. + components := strings.Split(path, "/") + return components +}