Example #1
0
// NewGreet creates a new greet (POST "/greets"). Creates a greet and redirects
// you to the created greet.
//
// To post a new greet, try this at a shell:
// $ now=$(date +'%Y-%m-%mT%H:%M:%SZ')
// $ curl -i -d "user=carl&message=Hello+World&time=$now" localhost:8000/greets
func NewGreet(w http.ResponseWriter, r *http.Request) {
	var greet Greet

	// Parse the POST body into the Greet struct. The format is the same as
	// is emitted by (e.g.) jQuery.param.
	r.ParseForm()
	err := param.Parse(r.Form, &greet)

	if err != nil || len(greet.Message) > 140 {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// We make no effort to prevent races against other insertions.
	Greets = append(Greets, greet)
	url := fmt.Sprintf("/greets/%d", len(Greets)-1)
	http.Redirect(w, r, url, http.StatusCreated)
}
Example #2
0
func (w *Wiki) Update(c web.C, rw http.ResponseWriter, r *http.Request) {
	name := w.getPageName(c.URLParams["name"])

	// Parse the POST body
	r.ParseForm()

	var fd formData
	param.Parse(r.Form, &fd)

	w.DB().Update(func(tx *db.Tx) error {
		p := db.Page{Tx: tx, Name: name}
		p.Text = []byte(fd.Text)

		return p.Save()
	})

	path := "/" + string(name)

	if path == "/root" {
		path = "/"
	}

	http.Redirect(rw, r, path, 302)
}