Home | History | Annotate | Download | only in wiki
      1 // Copyright 2010 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package main
      6 
      7 import (
      8 	"errors"
      9 	"html/template"
     10 	"io/ioutil"
     11 	"log"
     12 	"net/http"
     13 	"regexp"
     14 )
     15 
     16 type Page struct {
     17 	Title string
     18 	Body  []byte
     19 }
     20 
     21 func (p *Page) save() error {
     22 	filename := p.Title + ".txt"
     23 	return ioutil.WriteFile(filename, p.Body, 0600)
     24 }
     25 
     26 func loadPage(title string) (*Page, error) {
     27 	filename := title + ".txt"
     28 	body, err := ioutil.ReadFile(filename)
     29 	if err != nil {
     30 		return nil, err
     31 	}
     32 	return &Page{Title: title, Body: body}, nil
     33 }
     34 
     35 func viewHandler(w http.ResponseWriter, r *http.Request) {
     36 	title, err := getTitle(w, r)
     37 	if err != nil {
     38 		return
     39 	}
     40 	p, err := loadPage(title)
     41 	if err != nil {
     42 		http.Redirect(w, r, "/edit/"+title, http.StatusFound)
     43 		return
     44 	}
     45 	renderTemplate(w, "view", p)
     46 }
     47 
     48 func editHandler(w http.ResponseWriter, r *http.Request) {
     49 	title, err := getTitle(w, r)
     50 	if err != nil {
     51 		return
     52 	}
     53 	p, err := loadPage(title)
     54 	if err != nil {
     55 		p = &Page{Title: title}
     56 	}
     57 	renderTemplate(w, "edit", p)
     58 }
     59 
     60 func saveHandler(w http.ResponseWriter, r *http.Request) {
     61 	title, err := getTitle(w, r)
     62 	if err != nil {
     63 		return
     64 	}
     65 	body := r.FormValue("body")
     66 	p := &Page{Title: title, Body: []byte(body)}
     67 	err = p.save()
     68 	if err != nil {
     69 		http.Error(w, err.Error(), http.StatusInternalServerError)
     70 		return
     71 	}
     72 	http.Redirect(w, r, "/view/"+title, http.StatusFound)
     73 }
     74 
     75 func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
     76 	t, err := template.ParseFiles(tmpl + ".html")
     77 	if err != nil {
     78 		http.Error(w, err.Error(), http.StatusInternalServerError)
     79 		return
     80 	}
     81 	err = t.Execute(w, p)
     82 	if err != nil {
     83 		http.Error(w, err.Error(), http.StatusInternalServerError)
     84 	}
     85 }
     86 
     87 var validPath = regexp.MustCompile("^/(edit|save|view)/([a-zA-Z0-9]+)$")
     88 
     89 func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
     90 	m := validPath.FindStringSubmatch(r.URL.Path)
     91 	if m == nil {
     92 		http.NotFound(w, r)
     93 		return "", errors.New("Invalid Page Title")
     94 	}
     95 	return m[2], nil // The title is the second subexpression.
     96 }
     97 
     98 func main() {
     99 	http.HandleFunc("/view/", viewHandler)
    100 	http.HandleFunc("/edit/", editHandler)
    101 	http.HandleFunc("/save/", saveHandler)
    102 	log.Fatal(http.ListenAndServe(":8080", nil))
    103 }
    104