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 (
|
2014-03-17 20:03:03 -06:00
|
|
|
"flag"
|
2012-02-24 10:09:05 -07:00
|
|
|
"html/template"
|
2010-04-27 20:36:39 -06:00
|
|
|
"io/ioutil"
|
2014-03-17 20:03:03 -06:00
|
|
|
"log"
|
|
|
|
"net"
|
2011-11-08 16:43:02 -07:00
|
|
|
"net/http"
|
2010-04-27 20:36:39 -06:00
|
|
|
"regexp"
|
|
|
|
)
|
|
|
|
|
2014-03-17 20:03:03 -06:00
|
|
|
var (
|
|
|
|
addr = flag.Bool("addr", false, "find open address and print to final-port.txt")
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-26 23:07:46 -06:00
|
|
|
var templates = template.Must(template.ParseFiles("edit.html", "view.html"))
|
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) {
|
2012-03-26 23:07:46 -06:00
|
|
|
err := templates.ExecuteTemplate(w, tmpl+".html", 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
|
|
|
|
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) {
|
2013-10-07 18:14:35 -06:00
|
|
|
m := validPath.FindStringSubmatch(r.URL.Path)
|
|
|
|
if m == nil {
|
2010-09-29 21:19:33 -06:00
|
|
|
http.NotFound(w, r)
|
2010-04-27 20:36:39 -06:00
|
|
|
return
|
|
|
|
}
|
2013-10-07 18:14:35 -06:00
|
|
|
fn(w, r, m[2])
|
2010-04-27 20:36:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2014-03-17 20:03:03 -06:00
|
|
|
flag.Parse()
|
2010-04-27 20:36:39 -06:00
|
|
|
http.HandleFunc("/view/", makeHandler(viewHandler))
|
|
|
|
http.HandleFunc("/edit/", makeHandler(editHandler))
|
|
|
|
http.HandleFunc("/save/", makeHandler(saveHandler))
|
2014-03-17 20:03:03 -06:00
|
|
|
|
|
|
|
if *addr {
|
|
|
|
l, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile("final-port.txt", []byte(l.Addr().String()), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
s := &http.Server{}
|
|
|
|
s.Serve(l)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2010-04-27 20:36:39 -06:00
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|