package main import ( "html/template" "log" "net/http" "os" "regexp" ) // Template caching var templates = template.Must(template.ParseFiles("edit.html", "view.html")) // 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) } // 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 } func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) { // 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) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } // 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]) } } func viewHandler(w http.ResponseWriter, r *http.Request, title string) { // html := "