Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created July 31, 2013 02:44
Show Gist options
  • Select an option

  • Save draftcode/6118902 to your computer and use it in GitHub Desktop.

Select an option

Save draftcode/6118902 to your computer and use it in GitHub Desktop.
<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>
<h1>{{.Title}}</h1>
<p>[<a href="/edit/{{.Title}}">edit</a>]</p>
<div>{{.EscapedBody}}</div>
package main
import (
"html/template"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
)
const lenPath = len("/view/")
var templates = template.Must(template.ParseFiles("tmpl/edit.html", "tmpl/view.html"))
var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
var linkPattern = regexp.MustCompile("\\[[a-zA-Z0-9]+\\]")
type Page struct {
Title string
Body []byte
}
func (p *Page) save() error {
filename := filepath.Join("data", p.Title+".txt")
err := os.MkdirAll(filepath.Dir(filename), 0755)
if err != nil {
return err
}
return ioutil.WriteFile(filename, p.Body, 0600)
}
func loadPage(title string) (*Page, error) {
filename := filepath.Join("data", title+".txt")
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: body}, nil
}
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)
}
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[lenPath:]
if !titleValidator.MatchString(title) {
http.NotFound(w, r)
return
}
fn(w, r, title)
}
}
func (page Page) EscapedBody() template.HTML {
body := linkPattern.ReplaceAllFunc(page.Body, func(matched []byte) []byte {
title := string(matched[1 : len(matched)-1])
return []byte(`<a href="/view/` + title + `">` + title + `</a>`)
})
return template.HTML(body)
}
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)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func redirectFrontPage(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/view/FrontPage", http.StatusFound)
}
func main() {
http.HandleFunc("/", redirectFrontPage)
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment