diff --git a/edit/edit.go b/edit/edit.go new file mode 100644 index 00000000..c0a7ddc5 --- /dev/null +++ b/edit/edit.go @@ -0,0 +1,27 @@ +package edit + +import ( + "io/ioutil" + "net/http" + "os" +) + +type Page struct { +} + +// Execute sth +func Execute(w http.ResponseWriter, r *http.Request, file string) (int, error) { + if r.Method == "POST" { + // it's saving the post + } else { + // check if the file exists + if _, err := os.Stat(file); os.IsNotExist(err) { + return 404, nil + } + + file, _ := ioutil.ReadFile(file) + w.Write([]byte(string(file))) + } + + return 200, nil +} diff --git a/hugo.go b/hugo.go index 3c3be8e7..c6f76b68 100644 --- a/hugo.go +++ b/hugo.go @@ -2,12 +2,13 @@ package hugo import ( "net/http" - "strings" "github.com/spf13/hugo/commands" "github.com/mholt/caddy/config/setup" "github.com/mholt/caddy/middleware" + + "github.com/hacdias/caddy-hugo/routing" ) // Setup function @@ -23,20 +24,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") { - - if middleware.Path(r.URL.Path).Matches("/admin/new") { - w.Write([]byte("New")) - } else if middleware.Path(r.URL.Path).Matches("/admin/edit") { - var fileName string - - fileName = strings.Replace(r.URL.Path, "/admin/edit", "", 1) - - w.Write([]byte("Edit " + fileName)) - } else { - w.Write([]byte("Admin")) - } - - return 200, nil + return routing.Route(w, r) } return h.Next.ServeHTTP(w, r) diff --git a/routing/routing.go b/routing/routing.go new file mode 100644 index 00000000..ad922fe4 --- /dev/null +++ b/routing/routing.go @@ -0,0 +1,43 @@ +package routing + +import ( + "net/http" + "strings" + + "github.com/hacdias/caddy-hugo/edit" + "github.com/mholt/caddy/middleware" +) + +const ( + mainURL string = "/admin" + contentURL string = mainURL + "/content" + dataURL string = mainURL + "/data" + editURL string = mainURL + "/edit" + newURL string = mainURL + "/new" + settingsURL string = mainURL + "/settings" + staticURL string = mainURL + "/static" +) + +// Route the admin path +func Route(w http.ResponseWriter, r *http.Request) (int, error) { + if middleware.Path(r.URL.Path).Matches(contentURL) { + w.Write([]byte("Show Content Folder")) + } else if middleware.Path(r.URL.Path).Matches(dataURL) { + w.Write([]byte("Show Data Folder")) + } else if middleware.Path(r.URL.Path).Matches(editURL) { + return edit.Execute(w, r, strings.Replace(r.URL.Path, editURL+"/", "", 1)) + } else if middleware.Path(r.URL.Path).Matches(newURL) { + w.Write([]byte("New Thing Page")) + } else if middleware.Path(r.URL.Path).Matches(settingsURL) { + w.Write([]byte("Settings Page")) + } else if middleware.Path(r.URL.Path).Matches(staticURL) { + w.Write([]byte("Static things management")) + } else if r.URL.Path == mainURL || r.URL.Path == mainURL+"/" { + w.Write([]byte("Dashboard")) + } else { + return 404, nil + } + + return 200, nil + +} diff --git a/templates/edit_form.tpl b/templates/edit_form.tpl new file mode 100644 index 00000000..68a09ae6 --- /dev/null +++ b/templates/edit_form.tpl @@ -0,0 +1 @@ +{{ .Content }}