add browse file; move function to utils pkg
This commit is contained in:
parent
7a5e184610
commit
2300aa3a63
|
@ -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
30
hugo.go
|
@ -9,8 +9,10 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/hacdias/caddy-hugo/assets"
|
"github.com/hacdias/caddy-hugo/assets"
|
||||||
|
"github.com/hacdias/caddy-hugo/browse"
|
||||||
"github.com/hacdias/caddy-hugo/edit"
|
"github.com/hacdias/caddy-hugo/edit"
|
||||||
"github.com/hacdias/caddy-hugo/settings"
|
"github.com/hacdias/caddy-hugo/settings"
|
||||||
|
"github.com/hacdias/caddy-hugo/utils"
|
||||||
"github.com/mholt/caddy/config/setup"
|
"github.com/mholt/caddy/config/setup"
|
||||||
"github.com/mholt/caddy/middleware"
|
"github.com/mholt/caddy/middleware"
|
||||||
"github.com/spf13/hugo/commands"
|
"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) {
|
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
if middleware.Path(r.URL.Path).Matches("/admin") {
|
if middleware.Path(r.URL.Path).Matches("/admin") {
|
||||||
page := parseComponents(r)[1]
|
page := utils.ParseComponents(r)[1]
|
||||||
|
|
||||||
if page == "static" {
|
if page == "static" {
|
||||||
filename := strings.Replace(r.URL.Path, "/admin/", "", 1)
|
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)
|
w.Write(file)
|
||||||
return 200, nil
|
return 200, nil
|
||||||
} else if page == "content" {
|
|
||||||
w.Write([]byte("Content Page"))
|
|
||||||
return 200, nil
|
|
||||||
} else if page == "browse" {
|
} else if page == "browse" {
|
||||||
w.Write([]byte("Show Data Folder"))
|
return browse.Execute(w, r)
|
||||||
return 200, nil
|
|
||||||
} else if page == "edit" {
|
} else if page == "edit" {
|
||||||
return edit.Execute(w, r)
|
return edit.Execute(w, r)
|
||||||
} else if page == "settings" {
|
} 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)
|
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
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -57,3 +58,22 @@ func SplitCapitalize(name string) string {
|
||||||
|
|
||||||
return name
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue