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
|
|
|
}
|
|
|
|
|
|
|
|
var templates = make(map[string]*template.Template)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
for _, tmpl := range []string{"edit", "view"} {
|
text/template: new, simpler API
The Set type is gone. Instead, templates are automatically associated by
being parsed together; nested definitions implicitly create associations.
Only associated templates can invoke one another.
This approach dramatically reduces the breadth of the construction API.
For now, html/template is deleted from src/pkg/Makefile, so this can
be checked in. Nothing in the tree depends on it. It will be updated next.
R=dsymonds, adg, rsc, r, gri, mikesamuel, nigeltao
CC=golang-dev
https://golang.org/cl/5415060
2011-11-23 21:17:22 -07:00
|
|
|
t := template.Must(template.ParseFiles(tmpl + ".html"))
|
2011-08-17 18:38:08 -06:00
|
|
|
templates[tmpl] = t
|
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) {
|
2011-02-09 15:23:01 -07:00
|
|
|
err := templates[tmpl].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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const lenPath = len("/view/")
|
|
|
|
|
|
|
|
var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
|
|
|
|
|
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) {
|
2010-04-27 20:36:39 -06:00
|
|
|
title := r.URL.Path[lenPath:]
|
|
|
|
if !titleValidator.MatchString(title) {
|
2010-09-29 21:19:33 -06:00
|
|
|
http.NotFound(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2010-09-29 21:19:33 -06:00
|
|
|
fn(w, r, title)
|
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)
|
|
|
|
}
|