Exemplo n.º 1
0
func init() {
	var err error
	newPageTempate, err = loadLayoutTemplate().Parse(newContents)
	mst.MustNotErr(err)
	editPageTempate, err = loadLayoutTemplate().Parse(editContents)
	mst.MustNotErr(err)
}
Exemplo n.º 2
0
func handleViewPage(w http.ResponseWriter, r *http.Request) {
	noteId := r.URL.Path[3:]
	// Guard ourselves again matching /n/gibberish (check for uuid length)
	if len(noteId) != 36 {
		w.WriteHeader(404)
		w.Write([]byte("404 - Page not found"))
		return
	}

	note, err := data.GetNote(DB(), noteId)
	if err == sql.ErrNoRows {
		w.WriteHeader(404)
		w.Write([]byte("404 - Page not found"))
	} else if err != nil {
		w.WriteHeader(500)
		w.Write([]byte("500 - Error fetching note"))
		log.Fatal(err)
	} else {
		contents, err := RenderViewPage(Page{
			Notes:       data.MustGetAllNotes(DB()),
			Id:          note.Id,
			Title:       note.Title,
			Body:        note.Body,
			PostbackURL: "/n/" + note.Id,
		})
		mst.MustNotErr(err)
		w.Write(contents)
	}
}
Exemplo n.º 3
0
func handleNewPage(w http.ResponseWriter, r *http.Request) {
	contents, err := RenderNewPage(Page{
		Notes:       data.MustGetAllNotes(DB()),
		Title:       "New note",
		Body:        "",
		PostbackURL: "/new",
	})
	mst.MustNotErr(err)
	w.Write(contents)
}
Exemplo n.º 4
0
func handleNewPagePost(w http.ResponseWriter, r *http.Request) {
	title, body := r.FormValue("title"), r.FormValue("body")
	if savedNote, err := data.CreateNote(DB(), title, body); err != nil {
		contents, rerr := RenderNewPage(Page{
			Notes:       data.MustGetAllNotes(DB()),
			Title:       title,
			Body:        err.Error() + "\n\n" + body,
			PostbackURL: "/new",
		})
		mst.MustNotErr(rerr)
		w.WriteHeader(400)
		w.Write(contents)
	} else {
		http.Redirect(w, r, "/n/"+savedNote.Id, 302)
	}
}
Exemplo n.º 5
0
func handleEditPage(w http.ResponseWriter, r *http.Request) {
	noteId := r.URL.Path[3 : len(r.URL.Path)-5]
	note, err := data.GetNote(DB(), noteId)
	if err == sql.ErrNoRows {
		w.WriteHeader(404)
		w.Write([]byte("404 - Page not found"))
	} else if err != nil {
		w.WriteHeader(500)
		w.Write([]byte("500 - Error fetching note"))
		log.Fatal(err)
	} else {
		contents, err := RenderEditPage(Page{
			Notes:       data.MustGetAllNotes(DB()),
			Id:          note.Id,
			Title:       note.Title,
			Body:        note.Body,
			PostbackURL: "/n/" + note.Id + "/edit",
		})
		mst.MustNotErr(err)
		w.Write(contents)
	}
}
Exemplo n.º 6
0
func MustGetAllNotes(db *sql.DB) []Note {
	notes, err := GetAllNotes(db)
	mst.MustNotErr(err)
	return notes
}
Exemplo n.º 7
0
func loadLayoutTemplate() *template.Template {
	t, err := template.New("page").Parse(layoutContents)
	mst.MustNotErr(err)
	return t
}