func getIndex(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { tpl, err := routes.LoadTemplates("base.tpl", "index.tpl") if err != nil { return tracederror.New(err) } recent, err := models.GetRecentBlogPosts(ctx.DB, 4, 0) if err != nil { return tracederror.New(err) } featured, err := models.GetRecentPostsInCategory(ctx.DB, "featured", 1, 0) if err != nil { return tracederror.New(err) } if len(featured) == 0 { return tracederror.New(fmt.Errorf("No featured posts?")) } data := indexPage{ Posts: recent, Featured: featured[0], } return routes.RenderTemplateWithData(w, r, tpl, &data) }
func getGenericArchive(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { vars := mux.Vars(r) page, err := strconv.ParseInt(vars["page"], 10, 32) if err != nil { return tracederror.New(err) } if page <= 0 { return routes.MakeHttpError(fmt.Errorf("Page must be larger than 0"), http.StatusBadRequest, r) } tpl, err := routes.LoadTemplates("base.tpl", "archive.tpl") if err != nil { return routes.MakeHttpError(err, http.StatusNotFound, r) } posts, err := models.GetRecentBlogPosts(ctx.DB, 100, int32((page-1)*100)) if err != nil { return tracederror.New(err) } data := archivePage{ Page: int32(page), Posts: posts, Category: "All", } return routes.RenderTemplateWithData(w, r, tpl, data) }
func getAbout(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { tpl, err := routes.LoadTemplates("base.tpl", "about.tpl") if err != nil { return routes.MakeHttpError(err, http.StatusNotFound, r) } return routes.RenderTemplateWithData(w, r, tpl, nil) }
func getHello(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { tpl, err := routes.LoadTemplates("base.tpl", "echo.tpl") if err != nil { return routes.MakeHttpError(err, http.StatusInternalServerError, r) } return routes.RenderTemplateWithData(w, r, tpl, nil) }
func getEdit(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { err := routes.CheckAuth(r, ctx) if err != nil { return err } vars := mux.Vars(r) id := int64(0) if vars["id"] != "new" { id, err = strconv.ParseInt(vars["id"], 10, 64) if err != nil { return tracederror.New(err) } } post := &models.BlogPost{} if id != 0 { post, err = models.GetBlogPostByID(ctx.DB, int32(id)) if err != nil { return routes.MakeHttpError(err, http.StatusNotFound, r) } } categories, err := models.GetCategories(ctx.DB) if err != nil { return tracederror.New(err) } tpl, err := routes.LoadTemplates("base.tpl", "edit.tpl") if err != nil { return tracederror.New(err) } data := editPage{ Post: post, Categories: categories, } return routes.RenderTemplateWithData(w, r, tpl, &data) }
func getCategoryArchive(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { vars := mux.Vars(r) page, err := strconv.ParseInt(vars["page"], 10, 32) if err != nil { return tracederror.New(err) } if page <= 0 { return routes.MakeHttpError(fmt.Errorf("Page must be larger than 0"), http.StatusBadRequest, r) } category := vars["category"] valid, err := models.IsValidCategory(ctx.DB, category) if err != nil { return tracederror.New(err) } if !valid { return routes.MakeHttpError(fmt.Errorf("Invalid category %s", category), http.StatusBadRequest, r) } tpl, err := routes.LoadTemplates("base.tpl", "archive.tpl") if err != nil { return routes.MakeHttpError(err, http.StatusNotFound, r) } posts, err := models.GetRecentPostsInCategory(ctx.DB, category, 100, int32((page-1)*100)) if err != nil { return tracederror.New(err) } data := archivePage{ Page: int32(page), Posts: posts, Category: strings.Title(category), } return routes.RenderTemplateWithData(w, r, tpl, data) }
func getPost(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { vars := mux.Vars(r) tpl, err := routes.LoadTemplates("base.tpl", "post.tpl") if err != nil { return tracederror.New(err) } recent, err := models.GetRecentBlogPosts(ctx.DB, 4, 0) if err != nil { return tracederror.New(err) } id, err := strconv.ParseInt(vars["id"], 10, 32) if err != nil { return tracederror.New(err) } self, err := models.GetBlogPostByID(ctx.DB, int32(id)) if err != nil { return tracederror.New(err) } authErr := routes.CheckAuth(r, ctx) if self.Publish == nil && authErr != nil { return routes.MakeHttpError(nil, http.StatusNotFound, r) } if self.Publish != nil && self.Publish.After(time.Now().UTC()) && authErr != nil { return routes.MakeHttpError(nil, http.StatusNotFound, r) } data := postPage{ Post: self, Recent: recent, } return routes.RenderTemplateWithData(w, r, tpl, &data) }
func postRender(w http.ResponseWriter, r *http.Request, ctx routes.Context) error { err := routes.CheckAuth(r, ctx) if err != nil { return err } tpl, err := routes.LoadTemplates("render.tpl") if err != nil { return tracederror.New(err) } body, err := ioutil.ReadAll(r.Body) if err != nil { return tracederror.New(err) } bodyStr := string(body) bodyStr = strings.Replace(bodyStr, "{:truncate}", "", 1) bodyStr = strings.Replace(bodyStr, "{:longtruncate}", "", 1) return routes.RenderTemplateWithData(w, r, tpl, bodyStr) }