// serve handles Tessernote's page requests func serve(w http.ResponseWriter, r *http.Request) { if validDataURL.MatchString(r.URL.Path) { serveData(w, r) } else if validPageURL.MatchString(r.URL.Path) { c := appengine.NewContext(r) if !loggedIn(w, r, c) { return } notebook, err := tessernote.CurrentNotebook(c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } page := new(tessernote.Page) page.Tags, err = notebook.Tags(c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } selectedTags, err := parseSelectedTags(w, r, notebook, c) if err != nil { return } page.SetSelectedTags(selectedTags) relatedTags, err := notebook.RelatedTags(selectedTags, c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } page.SetRelatedTags(relatedTags) page.UntaggedNotes = len(notebook.UntaggedNoteKeys) > 0 if r.URL.Path == untaggedURL { page.Notes, err = notebook.UntaggedNotes(c) } else if len(selectedTags) == 0 { page.Notes, err = notebook.Notes(c) } else { page.Notes, err = tessernote.RelatedNotes(selectedTags, c) } if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } err = templates.ExecuteTemplate(w, "main.html", page) if err != nil { c.Errorf("executing template: %s", err) http.Error(w, err.Error(), http.StatusInternalServerError) } } else { http.NotFound(w, r) } }
// serveData handles requests to Tessernote's RESTful data API func serveData(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) u := user.Current(c) if u == nil { http.Error(w, "", http.StatusUnauthorized) return } notebook, err := tessernote.CurrentNotebook(c) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if r.URL.Path == NotesURL { switch r.Method { case "GET": GetAllNotes(w, c, notebook) case "PUT": ReplaceAllNotes(w, r, c, notebook) case "POST": CreateNote(w, r, c, notebook) case "DELETE": DeleteAllNotes(w, c, notebook) default: http.NotFound(w, r) } } else { switch r.Method { case "GET": GetNote(w, r, c, notebook) case "PUT": ReplaceNote(w, r, c, notebook) case "DELETE": DeleteNote(w, r, c, notebook) default: http.NotFound(w, r) } } }