func HandlePageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) { slug := web.Args(ctx).ByName("slug") page, err := Store(ctx).GetBySlug(slug) if err != nil { if err == ErrNotFound { web.HTMLErr(w, "Page does not exist", http.StatusNotFound) } else { log.Printf("cannot get page %q: %s", slug, err) web.StdHTMLResp(w, http.StatusServiceUnavailable) } return } if protectPrivatePage(ctx, w, r, page) { return } if r.Method == "GET" { render(w, "page-delete-confirm", page) return } page.Deleted = true if _, err := Store(ctx).Put(*page); err != nil { log.Printf("cannot delete page %d: %s", page.PageID, err) web.StdHTMLResp(w, http.StatusServiceUnavailable) } else { http.Redirect(w, r, "/", http.StatusFound) } }
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) }
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) } }
func RequireAuth(fn web.HandlerFunc) web.HandlerFunc { return func(ctx context.Context, w http.ResponseWriter, r *http.Request) { key := sessionKey(r) user, err := Store(ctx).GetBySessionKey(key) switch err { case nil: ctx = context.WithValue(ctx, "auth:authenticated", user) fn(ctx, w, r) case ErrNotFound: urlStr := "/login?next=" + url.QueryEscape(r.URL.Path) http.Redirect(w, r, urlStr, http.StatusTemporaryRedirect) default: log.Printf("cannot get user by session %q: %s", key, err) web.StdHTMLResp(w, http.StatusInternalServerError) } } }
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 HandlePageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) { page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug")) if err == ErrNotFound { page = &Page{ Slug: web.Args(ctx).ByName("slug"), } render(w, "page-edit", page) 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 } render(w, "page-edit", page) }