From cf3e603031423ef5d4d6c18f2cb51d1544742471 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Thu, 20 Jun 2024 19:54:24 -0400 Subject: [PATCH] Saving web app --- go-web-app/gowiki/TestPage2.txt | 1 + go-web-app/gowiki/edit.html | 15 +++++++++ go-web-app/gowiki/view.html | 12 +++++++ go-web-app/gowiki/wiki.go | 60 ++++++++++++++++++++++++++------- 4 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 go-web-app/gowiki/TestPage2.txt create mode 100644 go-web-app/gowiki/edit.html create mode 100644 go-web-app/gowiki/view.html diff --git a/go-web-app/gowiki/TestPage2.txt b/go-web-app/gowiki/TestPage2.txt new file mode 100644 index 0000000..6d8fbf7 --- /dev/null +++ b/go-web-app/gowiki/TestPage2.txt @@ -0,0 +1 @@ +This is a test page for page two. \ No newline at end of file diff --git a/go-web-app/gowiki/edit.html b/go-web-app/gowiki/edit.html new file mode 100644 index 0000000..970aea7 --- /dev/null +++ b/go-web-app/gowiki/edit.html @@ -0,0 +1,15 @@ + + + + + + {{.Title}} + + +

{{.Title}}

+
+
+
+
+ + \ No newline at end of file diff --git a/go-web-app/gowiki/view.html b/go-web-app/gowiki/view.html new file mode 100644 index 0000000..5872f1e --- /dev/null +++ b/go-web-app/gowiki/view.html @@ -0,0 +1,12 @@ + + + + + + {{ .Title }} + + +

{{.Title}}

+
{{ printf "%s" .Body }}
+ + \ No newline at end of file diff --git a/go-web-app/gowiki/wiki.go b/go-web-app/gowiki/wiki.go index efc8589..c8d752e 100644 --- a/go-web-app/gowiki/wiki.go +++ b/go-web-app/gowiki/wiki.go @@ -1,7 +1,7 @@ package main import ( - "fmt" + "html/template" "log" "net/http" "os" @@ -34,11 +34,32 @@ func loadPage(title string) (*Page, error) { return &Page{Title: title, Body: body}, nil } +func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { + t, err := template.ParseFiles(tmpl + ".html") + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + err = t.Execute(w, p) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} + func viewHandler(w http.ResponseWriter, r *http.Request) { - html := "

%s

%s
" - filename := r.URL.Path[len("/view/"):] - page, _ := loadPage(filename) - fmt.Fprintf(w, html, page.Title, page.Body) + // html := "

%s

%s
" + title := r.URL.Path[len("/view/"):] + page, err := loadPage(title) + // fmt.Fprintf(w, html, page.Title, page.Body) + // t, _ := template.ParseFiles("view.html") + // t.Execute(w, page) + + // handle non-existant pages + if err != nil { + http.Redirect(w, r, "/edit/"+title, http.StatusFound) + return + } + renderTemplate(w, "view", page) } func editHandler(w http.ResponseWriter, r *http.Request) { @@ -47,17 +68,30 @@ func editHandler(w http.ResponseWriter, r *http.Request) { if err != nil { page = &Page{Title: title} } - fmt.Fprintf(w, "

Editing %s

"+ - "
"+ - "
"+ - ""+ - "
", - page.Title, page.Title, page.Body) - + // First Take + // fmt.Fprintf(w, "

Editing %s

"+ + // "
"+ + // "
"+ + // ""+ + // "
", + // page.Title, page.Title, page.Body) + // Take Two + // t, _ := template.ParseFiles("edit.html") + // t.Execute(w, page) + // Take Three + renderTemplate(w, "edit", page) } func saveHandler(w http.ResponseWriter, r *http.Request) { - + title := r.URL.Path[len("/save/"):] + body := r.FormValue("body") + p := &Page{Title: title, Body: []byte(body)} + err := p.save() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/view/"+title, http.StatusFound) } func main() {