diff --git a/edit/edit.go b/edit/edit.go index 92ff90d2..20fc562f 100644 --- a/edit/edit.go +++ b/edit/edit.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "net/http" "os" + "strings" "github.com/hacdias/caddy-hugo/page" "github.com/spf13/hugo/commands" @@ -15,7 +16,9 @@ type fileInfo struct { } // Execute sth -func Execute(w http.ResponseWriter, r *http.Request, filename string) (int, error) { +func Execute(w http.ResponseWriter, r *http.Request) (int, error) { + filename := strings.Replace(r.URL.Path, "edit/", "", 1) + if r.Method == "POST" { r.ParseForm() err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666) diff --git a/hugo.go b/hugo.go index 98b07bc7..70e44cae 100644 --- a/hugo.go +++ b/hugo.go @@ -15,15 +15,6 @@ import ( "github.com/spf13/hugo/commands" ) -const ( - mainURL string = "/admin" - contentURL string = mainURL + "/content" - browseURL string = mainURL + "/browse" - editURL string = mainURL + "/edit" - settingsURL string = mainURL + "/settings" - assetsURL string = mainURL + "/assets" -) - // Setup function func Setup(c *setup.Controller) (middleware.Middleware, error) { commands.Execute() @@ -44,21 +35,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) } func route(w http.ResponseWriter, r *http.Request) (int, error) { - if urlMatch(r, contentURL) { - // content folder management - w.Write([]byte("Show Content Folder")) - } else if urlMatch(r, browseURL) { - // browse files - w.Write([]byte("Show Data Folder")) - } else if urlMatch(r, editURL) { - // edit file - return edit.Execute(w, r, strings.Replace(r.URL.Path, editURL+"/", "", 1)) - } else if urlMatch(r, settingsURL) { - // edit settings - w.Write([]byte("Settings Page")) + page := parseComponents(r)[1] - } else if urlMatch(r, assetsURL) { - // assets like css, javascript and images + if page == "assets" { filename := strings.Replace(r.URL.Path, assetsURL, "static", 1) file, err := assets.Asset(filename) @@ -73,9 +52,14 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) { header.Set("Content-Type", mime) w.Write(file) - } else if r.URL.Path == mainURL || r.URL.Path == mainURL+"/" { - // dashboard - w.Write([]byte("Dashboard")) + } else if page == "content" { + w.Write([]byte("Content Page")) + } else if page == "browse" { + w.Write([]byte("Show Data Folder")) + } else if page == "edit" { + return edit.Execute(w, r) + } else if page == "settings" { + w.Write([]byte("Settings Page")) } else { return 404, nil } @@ -83,6 +67,21 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) { return 200, nil } -func urlMatch(r *http.Request, str string) bool { - return middleware.Path(r.URL.Path).Matches(str) +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/static/js/app.js b/static/js/app.js index ddce029d..e69de29b 100644 --- a/static/js/app.js +++ b/static/js/app.js @@ -1 +0,0 @@ -$("textarea").resizable(); diff --git a/templates/browse.tmpl b/templates/browse.tmpl new file mode 100644 index 00000000..e69de29b diff --git a/templates/content.tmpl b/templates/content.tmpl new file mode 100644 index 00000000..e69de29b diff --git a/templates/edit.tmpl b/templates/edit.tmpl index 6acf4bcd..dfe2d4d0 100644 --- a/templates/edit.tmpl +++ b/templates/edit.tmpl @@ -5,7 +5,7 @@