Adding template organzing, handlers, page editing
This commit is contained in:
parent
2bf278c312
commit
043b1ed7c6
4 changed files with 73 additions and 8 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
galley
|
||||||
|
data
|
||||||
68
galley.go
68
galley.go
|
|
@ -1,15 +1,23 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"html/template"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// cache templates on initialization
|
||||||
|
var templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
|
||||||
|
|
||||||
|
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
p1 := &Page{Title: "TestPage", Body: []byte("This is a sample page.")}
|
http.HandleFunc("/view/", makeHandler(viewHandler))
|
||||||
p1.save()
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
||||||
p2, _ := loadPage("TestPage")
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
||||||
fmt.Println(string(p2.Body))
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
type Page struct {
|
type Page struct {
|
||||||
|
|
@ -18,15 +26,61 @@ type Page struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Page) save() error {
|
func (p *Page) save() error {
|
||||||
filename := p.Title + ".txt"
|
filename := "data/" + p.Title + ".txt"
|
||||||
return os.WriteFile(filename, p.Body, 0600)
|
return os.WriteFile(filename, p.Body, 0600)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadPage(title string) (*Page, error) {
|
func loadPage(title string) (*Page, error) {
|
||||||
filename := title + ".txt"
|
filename := "data/" + title + ".txt"
|
||||||
body, err := os.ReadFile(filename)
|
body, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Page{Title: title, Body: body}, nil
|
return &Page{Title: title, Body: body}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
p, err := loadPage(title)
|
||||||
|
if err != nil {
|
||||||
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
renderTemplate(w, "view", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
p, err := loadPage(title)
|
||||||
|
if err != nil {
|
||||||
|
p = &Page{Title: title}
|
||||||
|
}
|
||||||
|
renderTemplate(w, "edit", p)
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
||||||
|
body := r.FormValue("body")
|
||||||
|
p := &Page{Title: title, Body: []byte(body)}
|
||||||
|
err := p.save()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
|
||||||
|
err := templates.ExecuteTemplate(w, tmpl + ".html", p)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// function that takes in and modifies a function
|
||||||
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
||||||
|
if m == nil {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fn(w, r, m[2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
6
tmpl/edit.html
Normal file
6
tmpl/edit.html
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
<h1>Editing {{.Title}}</h1>
|
||||||
|
|
||||||
|
<form action="/save/{{.Title}}" method="POST">
|
||||||
|
<div><textarea name="body" rows="20" cols="80">{{printf "%s" .Body}}</textarea></div>
|
||||||
|
<div><input type="submit" value="Save"></div>
|
||||||
|
</form>
|
||||||
3
tmpl/view.html
Normal file
3
tmpl/view.html
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
<h1>{{.Title}}</h1>
|
||||||
|
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
|
||||||
|
<div>{{printf "%s" .Body}}<div>
|
||||||
Loading…
Reference in a new issue