func HandlePageDetail(ctx context.Context, w http.ResponseWriter, r *http.Request) { var content struct { Page *Page User *auth.User } if u, ok := auth.CurrentUser(ctx); ok { content.User = u.User } page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug")) if err == ErrNotFound { http.Redirect(w, r, r.URL.Path+"/edit", http.StatusFound) return } if err != nil { log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err) web.StdHTMLResp(w, http.StatusServiceUnavailable) return } if protectPrivatePage(ctx, w, r, page) { return } content.Page = page if web.CheckLastModified(ctx, w, r, page.Updated) { return } render(w, "page-details", content) }
func HandlePageHistory(ctx context.Context, w http.ResponseWriter, r *http.Request) { page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug")) if err != nil { if err == ErrNotFound { web.HTMLErr(w, "Page does not exist", http.StatusNotFound) } else { log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err) web.StdHTMLResp(w, http.StatusServiceUnavailable) } return } if protectPrivatePage(ctx, w, r, page) { return } content := struct { Page *Page User *auth.User }{ Page: page, } if u, ok := auth.CurrentUser(ctx); ok { content.User = u.User } render(w, "page-history", content) }
// protectPrivatePage return true and write standard forbidden response if // given page is private and currently authenticated user is not the owner. func protectPrivatePage(ctx context.Context, w http.ResponseWriter, r *http.Request, page *Page) bool { if page.IsPublic { return false } u, ok := auth.CurrentUser(ctx) if !ok || page.OwnerID != u.User.UserID { urlStr := "/login?next=" + url.QueryEscape(r.URL.Path) http.Redirect(w, r, urlStr, http.StatusTemporaryRedirect) return true } return false }
func HandlePageList(ctx context.Context, w http.ResponseWriter, r *http.Request) { var user *auth.User if u, ok := auth.CurrentUser(ctx); ok { user = u.User } pages, err := Store(ctx).List(user, 10, 0) if err != nil { log.Printf("cannot list pages: %s", err) web.StdHTMLResp(w, http.StatusServiceUnavailable) return } content := struct { User *auth.User Pages []*Page }{ Pages: pages, User: user, } render(w, "page-list", content) }
func HandlePageSave(ctx context.Context, w http.ResponseWriter, r *http.Request) { page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug")) if err != nil && err != ErrNotFound { log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err) web.StdHTMLResp(w, http.StatusServiceUnavailable) return } user, _ := auth.CurrentUser(ctx) if err == ErrNotFound { page = &Page{ IsPublic: true, OwnerID: user.User.UserID, Slug: web.Args(ctx).ByName("slug"), } } if protectPrivatePage(ctx, w, r, page) { return } content := r.FormValue("content") if content == "" { web.HTMLErr(w, "content is required", http.StatusBadRequest) return } page.IsPublic = r.FormValue("public") == "on" // checkbox page.Content = content if p, err := Store(ctx).Put(*page); err != nil { log.Printf("cannot update page: %s", err) web.StdHTMLResp(w, http.StatusServiceUnavailable) } else { http.Redirect(w, r, fmt.Sprintf("/p/%s", p.Slug), http.StatusFound) } }