update url routing, simplify; new templates

This commit is contained in:
Henrique Dias 2015-09-13 21:10:54 +01:00
parent 60eefa3915
commit 93b0ab8dd9
7 changed files with 32 additions and 31 deletions

View File

@ -4,6 +4,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os" "os"
"strings"
"github.com/hacdias/caddy-hugo/page" "github.com/hacdias/caddy-hugo/page"
"github.com/spf13/hugo/commands" "github.com/spf13/hugo/commands"
@ -15,7 +16,9 @@ type fileInfo struct {
} }
// Execute sth // 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" { if r.Method == "POST" {
r.ParseForm() r.ParseForm()
err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666) err := ioutil.WriteFile(filename, []byte(r.Form["content"][0]), 0666)

55
hugo.go
View File

@ -15,15 +15,6 @@ import (
"github.com/spf13/hugo/commands" "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 // Setup function
func Setup(c *setup.Controller) (middleware.Middleware, error) { func Setup(c *setup.Controller) (middleware.Middleware, error) {
commands.Execute() 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) { func route(w http.ResponseWriter, r *http.Request) (int, error) {
if urlMatch(r, contentURL) { page := parseComponents(r)[1]
// 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"))
} else if urlMatch(r, assetsURL) { if page == "assets" {
// assets like css, javascript and images
filename := strings.Replace(r.URL.Path, assetsURL, "static", 1) filename := strings.Replace(r.URL.Path, assetsURL, "static", 1)
file, err := assets.Asset(filename) file, err := assets.Asset(filename)
@ -73,9 +52,14 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) {
header.Set("Content-Type", mime) header.Set("Content-Type", mime)
w.Write(file) w.Write(file)
} else if r.URL.Path == mainURL || r.URL.Path == mainURL+"/" { } else if page == "content" {
// dashboard w.Write([]byte("Content Page"))
w.Write([]byte("Dashboard")) } 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 { } else {
return 404, nil return 404, nil
} }
@ -83,6 +67,21 @@ func route(w http.ResponseWriter, r *http.Request) (int, error) {
return 200, nil return 200, nil
} }
func urlMatch(r *http.Request, str string) bool { func parseComponents(r *http.Request) []string {
return middleware.Path(r.URL.Path).Matches(str) //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

@ -1 +0,0 @@
$("textarea").resizable();

0
templates/browse.tmpl Normal file
View File

0
templates/content.tmpl Normal file
View File

View File

@ -5,7 +5,7 @@
<h1>Editing {{ .Name }}</h1> <h1>Editing {{ .Name }}</h1>
<form id="editForm" method="POST" action=""> <form method="POST" action="">
<textarea name="content">{{ .Content }}</textarea> <textarea name="content">{{ .Content }}</textarea>
<input type="submit" value="Save"> <input type="submit" value="Save">
</form> </form>

0
templates/settings.tmpl Normal file
View File