You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.7 KiB
Go

package main
import (
"log"
"net/http"
"os"
7 months ago
"regexp"
"text/template"
)
7 months ago
// Template caching
// var templates = template.Must(template.ParseFiles("edit.html", "view.html"))
var templates = template.Must(template.ParseGlob("./tmpl/*.html"))
// var templates = template.Must(template.ParseFiles("edit.html", "view.html"))
7 months ago
// Validation to prevent abitrary paths
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
// A wiki consists of a series of interconnected pages, each of which has a title and a body
type Page struct {
Title string
//The Body element is a []byte rather than string because that is the type expected by the io libraries we will use
Body []byte
}
// save Saves a page to disk.
func (p *Page) save() error {
filename := p.Title + ".txt"
return os.WriteFile(filename, p.Body, 0600)
}
7 months ago
// Not neccessary after makeHandler implemented this.
// func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
// m := validPath.FindStringSubmatch(r.URL.Path)
// if m == nil {
// http.NotFound(w, r)
// return "", errors.New("invalid Page Title")
// }
// return m[2], nil // The title is the second subexpression
// }
// loadPage loads a Page from disk.
// It takes the title of the page as an argument, reads the corresponding
// file, and returns a pointer to a Page struct containing the title and body.
// If an error occurs during reading, it returns the error.
func loadPage(title string) (*Page, error) {
filename := title + ".txt"
body, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
7 months ago
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
7 months ago
// In efficient. No cacheing
// t, err := template.ParseFiles(tmpl + ".html")
// if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
// }
// err = t.Execute(w, p)
//With caching
err := templates.ExecuteTemplate(w, tmpl+".html", p)
7 months ago
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
7 months ago
}
// Wraps a handler function, includes validation checks on titles
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])
7 months ago
}
}
7 months ago
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
7 months ago
// html := "<h1>%s</h1><div>%s</div>"
7 months ago
// title := r.URL.Path[len("/view/"):]
// Not neccesary after makeHandler was implemented
// title, err := getTitle(w, r)
// if err != nil {
// return
// }
7 months ago
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)
}
7 months ago
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
// title := r.URL.Path[len("/edit/"):]
// Not neccesary after makeHandler was implemented
// title, err := getTitle(w, r)
// if err != nil {
// return
// }
page, err := loadPage(title)
if err != nil {
page = &Page{Title: title}
}
7 months ago
// 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)
}
7 months ago
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
// title := r.URL.Path[len("/save/"):]
// Not neccesary after makeHandler was implemented
// title, err := getTitle(w, r)
// if err != nil {
// return
// }
7 months ago
body := r.FormValue("body")
p := &Page{Title: title, Body: []byte(body)}
err := p.save()
7 months ago
7 months ago
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func main() {
7 months ago
log.Printf("%s\n", templates.DefinedTemplates())
// Using function literal and closures to handle not found
// http.HandleFunc("/view/", viewHandler)
// http.HandleFunc("/edit/", editHandler)
// http.HandleFunc("/save/", saveHandler)
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
log.Println("Serving on: http://0.0.0.0:5001")
log.Fatal(http.ListenAndServe(":5001", nil))
}
// func main() {
// p1 := &Page{Title: "TestPage", Body: []byte("This is a test page.")}
// p1.save()
// p2, _ := loadPage("TestPage")
// fmt.Println(string(p2.Body))
// }