add browse file; move function to utils pkg

pull/20/head
Henrique Dias 2015-09-15 21:40:46 +01:00
parent 7a5e184610
commit 2300aa3a63
4 changed files with 38 additions and 26 deletions

14
browse/browse.go Normal file
View File

@ -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")
}

30
hugo.go
View File

@ -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
}

View File

View File

@ -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
}