1 package main
2
3 import (
4 "fmt"
5 "http"
6 "io/ioutil"
7 "os"
8 )
9
10 type Page struct {
11 Title string
12 Body []byte
13 }
14
15 func (p *Page) save() os.Error {
16 filename := p.Title + ".txt"
17 return ioutil.WriteFile(filename, p.Body, 0600)
18 }
19
20 func loadPage(title string) (*Page, os.Error) {
21 filename := title + ".txt"
22 body, err := ioutil.ReadFile(filename)
23 if err != nil {
24 return nil, err
25 }
26 return &Page{Title: title, Body: body}, nil
27 }
28
29 const lenPath = len("/view/")
30
31 func viewHandler(w http.ResponseWriter, r *http.Request) {
32 title := r.URL.Path[lenPath:]
33 p, _ := loadPage(title)
34 fmt.Fprintf(w, "<h1>%s</h1><div>%s</div>", p.Title, p.Body)
35 }
36
37 func main() {
38 http.HandleFunc("/view/", viewHandler)
39 http.ListenAndServe(":8080", nil)
40 }