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

55
hugo.go
View File

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

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>
<form id="editForm" method="POST" action="">
<form method="POST" action="">
<textarea name="content">{{ .Content }}</textarea>
<input type="submit" value="Save">
</form>

0
templates/settings.tmpl Normal file
View File