Saving web app

drew/sql-it
Drew Bednar 7 months ago
parent 55378404b3
commit cf3e603031

@ -0,0 +1 @@
This is a test page for page two.

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{.Title}}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<form action="/save/{{.Title}}" method="POST">
<div><textarea name="body" cols="80" rows="20">{{printf "%s" .Body}}</textarea></div>
<div><input type="submit" value="Save"></div>
</form>
</body>
</html>

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ .Title }}</title>
</head>
<body>
<h1>{{.Title}}</h1>
<div>{{ printf "%s" .Body }}</div>
</body>
</html>

@ -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 := "<h1>%s</h1><div>%s</div>"
filename := r.URL.Path[len("/view/"):]
page, _ := loadPage(filename)
fmt.Fprintf(w, html, page.Title, page.Body)
// html := "<h1>%s</h1><div>%s</div>"
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, "<h1>Editing %s</h1>"+
"<form action=\"/save/%s\" method=\"POST\">"+
"<textarea name=\"body\">%s</textarea><br>"+
"<input type=\"submit\" value=\"Save\">"+
"</form>",
page.Title, page.Title, page.Body)
// First Take
// fmt.Fprintf(w, "<h1>Editing %s</h1>"+
// "<form action=\"/save/%s\" method=\"POST\">"+
// "<textarea name=\"body\">%s</textarea><br>"+
// "<input type=\"submit\" value=\"Save\">"+
// "</form>",
// 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() {

Loading…
Cancel
Save