func ArticlePermaLinkHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) vars := mux.Vars(r) id, err := strconv.ParseInt(vars["id"], 10, 64) if err != nil { core.HandleError(c, w, err) return } article, err := GetArticleById(c, id, true) if err != nil { core.HandleNotFound(c, w) return } if !article.IsPublic { user := auth.CurrentUser(c) if !user.IsAdmin { core.HandleAuthRequired(c, w) return } } redirectTo, err := article.URL() if err != nil { core.HandleNotFound(c, w) return } http.Redirect(w, r, redirectTo.Path, 302) }
func ArticlePageHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) user := auth.CurrentUser(c) vars := mux.Vars(r) page, err := strconv.ParseInt(vars["page"], 10, 32) if err != nil { page = 1 } q := NewArticleQuery().Order("-CreatedOn") if !user.IsAdmin { q = q.Filter("IsPublic=", true) } p := NewArticlePager(c, q, int(page)) articles, err := GetArticles(c, p) if err != nil { core.HandleError(c, w, err) return } context := tmplt.Context{ "articles": articles, "pager": p, } core.RenderTemplate(c, w, context, "templates/blog/articleList.html", "templates/pager.html", LAYOUT) }
func ArticleHandler(w http.ResponseWriter, r *http.Request) { c := appengine.NewContext(r) user := auth.CurrentUser(c) vars := mux.Vars(r) id, err := strconv.ParseInt(vars["id"], 10, 64) if err != nil { core.HandleError(c, w, err) return } article, err := GetArticleById(c, id, !user.IsAdmin) if err != nil { core.HandleNotFound(c, w) return } if !article.IsPublic && !user.IsAdmin { core.HandleAuthRequired(c, w) return } viewedArticles := make([]string, 0) if cookie, err := r.Cookie("viewedArticles"); err != http.ErrNoCookie { viewedArticles = strings.Split(cookie.Value, ",") } articleID := strconv.FormatInt(article.Key().IntID(), 32) if !isViewedArticle(viewedArticles, articleID) { viewedArticles = append(viewedArticles, articleID) http.SetCookie(w, &http.Cookie{ Name: "viewedArticles", Value: strings.Join(viewedArticles, ","), Path: "/", Expires: time.Now().Add(time.Duration(30*24) * time.Hour), }) if err := ChangeArticleViewsCount(c, article.Key(), +1); err != nil { core.HandleError(c, w, err) return } } context := tmplt.Context{"article": article} core.RenderTemplate(c, w, context, "templates/blog/article.html", LAYOUT) }
func RenderTemplate(c appengine.Context, w http.ResponseWriter, context tmplt.Context, templateNames ...string) { if len(templateNames) == 0 { panic("expected at least 1 template, but got 0") } newFunc := func() (*template.Template, error) { var err error newT := template.New(path.Base(templateNames[len(templateNames)-1])) newT = AddTemplateFuncs(newT) newT, err = newT.ParseFiles(templateNames...) if err != nil { return nil, err } return newT, nil } t, err := tmplt.Holder.Get(strings.Join(templateNames, ","), newFunc) if err != nil { HandleError(c, w, err) return } if context == nil { context = tmplt.Context{} } context["appengineContext"] = c context["user"] = auth.CurrentUser(c) if w.Header().Get("content-type") == "" { w.Header().Add("content-type", "text/html") } buf := &bytes.Buffer{} err = t.Execute(buf, context) if err != nil { HandleError(c, w, err) return } io.Copy(w, buf) }