2012-01-22 15:28:32 -07:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2010-04-27 20:36:39 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-02-24 10:09:05 -07:00
|
|
|
"html/template"
|
2010-04-27 20:36:39 -06:00
|
|
|
"io/ioutil"
|
2011-11-08 16:43:02 -07:00
|
|
|
"net/http"
|
2010-04-27 20:36:39 -06:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2011-01-25 21:56:52 -07:00
|
|
|
type Page struct {
|
|
|
|
Title string
|
|
|
|
Body []byte
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func (p *Page) save() error {
|
2011-01-25 21:56:52 -07:00
|
|
|
filename := p.Title + ".txt"
|
|
|
|
return ioutil.WriteFile(filename, p.Body, 0600)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2011-11-01 20:06:05 -06:00
|
|
|
func loadPage(title string) (*Page, error) {
|
2010-04-27 20:36:39 -06:00
|
|
|
filename := title + ".txt"
|
|
|
|
body, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-01-25 21:56:52 -07:00
|
|
|
return &Page{Title: title, Body: body}, nil
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2010-09-29 21:19:33 -06:00
|
|
|
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
|
2010-04-27 20:36:39 -06:00
|
|
|
p, err := loadPage(title)
|
|
|
|
if err != nil {
|
2010-09-29 21:19:33 -06:00
|
|
|
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2010-09-29 21:19:33 -06:00
|
|
|
renderTemplate(w, "view", p)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2010-09-29 21:19:33 -06:00
|
|
|
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
|
2010-04-27 20:36:39 -06:00
|
|
|
p, err := loadPage(title)
|
|
|
|
if err != nil {
|
2011-01-25 21:56:52 -07:00
|
|
|
p = &Page{Title: title}
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
2010-09-29 21:19:33 -06:00
|
|
|
renderTemplate(w, "edit", p)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2010-09-29 21:19:33 -06:00
|
|
|
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
|
2010-04-27 20:36:39 -06:00
|
|
|
body := r.FormValue("body")
|
2011-01-25 21:56:52 -07:00
|
|
|
p := &Page{Title: title, Body: []byte(body)}
|
2010-04-27 20:36:39 -06:00
|
|
|
err := p.save()
|
|
|
|
if err != nil {
|
2011-11-01 20:06:05 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2010-09-29 21:19:33 -06:00
|
|
|
http.Redirect(w, r, "/view/"+title, http.StatusFound)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
2011-01-25 21:56:52 -07:00
|
|
|
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
|
2012-02-24 10:09:05 -07:00
|
|
|
t, err := template.ParseFiles(tmpl + ".html")
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
2011-11-01 20:06:05 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2011-03-08 18:59:13 -07:00
|
|
|
err = t.Execute(w, p)
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
2011-11-01 20:06:05 -06:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-07 18:14:35 -06:00
|
|
|
var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
|
2010-04-27 20:36:39 -06:00
|
|
|
|
2010-09-29 21:19:33 -06:00
|
|
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2013-10-07 18:14:35 -06:00
|
|
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m == nil {
|
2010-09-29 21:19:33 -06:00
|
|
|
http.NotFound(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2013-10-07 18:14:35 -06:00
|
|
|
fn(w, r, m[2])
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/view/", makeHandler(viewHandler))
|
|
|
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
|
|
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|