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 (
|
2011-11-01 20:06:05 -06:00
|
|
|
"errors"
|
2012-02-24 10:09:05 -07:00
|
|
|
"html/template"
|
2010-04-27 20:36:39 -06:00
|
|
|
"io/ioutil"
|
2017-08-03 11:52:42 -06:00
|
|
|
"log"
|
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, err := getTitle(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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, err := getTitle(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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, err := getTitle(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
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) {
|
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, 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
|
|
|
|
2013-10-07 18:14:35 -06:00
|
|
|
func getTitle(w http.ResponseWriter, r *http.Request) (string, error) {
|
|
|
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m == nil {
|
2010-09-29 21:19:33 -06:00
|
|
|
http.NotFound(w, r)
|
2013-10-07 18:14:35 -06:00
|
|
|
return "", errors.New("Invalid Page Title")
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
2013-10-07 18:14:35 -06:00
|
|
|
return m[2], nil // The title is the second subexpression.
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
http.HandleFunc("/view/", viewHandler)
|
|
|
|
http.HandleFunc("/edit/", editHandler)
|
|
|
|
http.HandleFunc("/save/", saveHandler)
|
2017-08-03 11:52:42 -06:00
|
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|