Example #1
0
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)
}
Example #2
0
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)
}
Example #3
0
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)
}
Example #4
0
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)
}
Example #5
0
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)
}
Example #6
0
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)
}
Example #7
0
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)
}
Example #8
0
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)
}