func permalinkHandler(req *web.Request) { slug := req.URLParam["slug"] year, month, day := req.URLParam["year"], req.URLParam["month"], req.URLParam["day"] y, _ := strconv.Atoi(year) m, _ := strconv.Atoi(month) d, _ := strconv.Atoi(day) post, err := posts.FindByPermalink(y, time.Month(m), d, slug) if err != nil { switch err.(type) { case errors.NotFound: notFound(req) default: logger.Printf("failed finding post with year(%#v) month(%#v) day(%#v) slug(%#v): %s (%T)", year, month, day, slug, err, err) serverError(req, err) } } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ Post: post, Title: post.Title, Canonical: view.PostCanonical(post), Description: post.Description, }) } }
func notFound(req *web.Request) { w := req.Respond(web.StatusNotFound, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ NotFound: true, Title: "Not Found", }) }
func serverError(req *web.Request, err error) { w := req.Respond(web.StatusInternalServerError, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ Error: true, Title: "Oh. Sorry about that.", }) }
func rootHandler(req *web.Request) { posts, err := posts.FindLatest(6) if err != nil { logger.Printf("failed finding latest posts: %s", err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ PostPreview: posts, Canonical: "/", ArchiveLinks: true, Description: config.SiteDescription, }) } }
func searchHandler(req *web.Request) { query := req.Param.Get("query") posts, err := posts.Search(query) if err != nil { logger.Printf("failed finding posts with query %#v: %s", query, err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") title := fmt.Sprintf("Search results for %#v", query) view.RenderLayout(w, &view.RenderInfo{ PostPreview: posts, Title: title, PageTitle: title, ArchiveLinks: true, }) } }
func fullArchiveHandler(req *web.Request) { posts, err := posts.FindLatest(posts.Len()) if err != nil { logger.Printf("failed getting posts for full archive: %s", err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") title := "Full archives" view.RenderLayout(w, &view.RenderInfo{ FullArchive: posts, Description: title, Title: title, ArchiveLinks: true, Canonical: req.URL.Path, }) } }
func tagHandler(req *web.Request) { tag := req.URLParam["tag"] posts, err := posts.FindByTag(tag) if err != nil { logger.Printf("failed finding posts with tag %#v: %s", tag, err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") title := fmt.Sprintf("Articles tagged with %#v", tag) view.RenderLayout(w, &view.RenderInfo{ PostPreview: posts, Title: title, PageTitle: title, Canonical: req.URL.Path, Description: fmt.Sprintf("Articles with the %#v tag", tag), }) } }
func categoryHandler(req *web.Request) { category := req.URLParam["category"] posts, err := posts.FindByCategory(category) if err != nil { logger.Printf("failed finding posts with category %#v: %s", category, err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") category = strings.Title(category) title := fmt.Sprintf("%s Articles", category) view.RenderLayout(w, &view.RenderInfo{ PostPreview: posts, Title: title, PageTitle: title, Canonical: req.URL.Path, Description: fmt.Sprintf("Articles in the %s category", category), }) } }
func monthlyHandler(req *web.Request) { year, month := req.URLParam["year"], req.URLParam["month"] y, _ := strconv.Atoi(year) m, _ := strconv.Atoi(month) posts, err := posts.FindByMonth(y, time.Month(m)) if err != nil { logger.Printf("failed finding posts in month %#v of %#v: %s", month, year, err) serverError(req, err) } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") title := fmt.Sprintf("Archives for %s-%s", month, year) view.RenderLayout(w, &view.RenderInfo{ PostPreview: posts, Title: title, Canonical: req.URL.Path, ArchiveLinks: true, Description: title, }) } }
func pageHandler(req *web.Request) { slug := req.URLParam["slug"] page, err := pages.FindBySlug(slug) if err != nil { switch err.(type) { case errors.NotFound: notFound(req) default: logger.Printf("failed finding page with slug %#v: %s (%T)", slug, err, err) serverError(req, err) } } else { w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ Page: page, Title: page.Title, Canonical: view.PageCanonical(page), Description: page.Description, }) } }
func categoryArchiveHandler(req *web.Request) { posts, err := posts.FindLatest(posts.Len()) if err != nil { logger.Printf("failed getting posts for category archive: %s", err) serverError(req, err) } else { grouped := make(map[string][]*post.Post) for _, post := range posts { key := post.Category grouped[key] = append(grouped[key], post) } w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ CategoryArchive: grouped, Description: "Archives by category", Title: "Category archives", ArchiveLinks: true, Canonical: req.URL.Path, }) } }
func monthlyArchiveHandler(req *web.Request) { posts, err := posts.FindLatest(posts.Len()) if err != nil { logger.Printf("failed getting posts for monthly archive: %s", err) serverError(req, err) } else { grouped := make(map[int64][]*post.Post) for _, post := range posts { t := post.PublishedOn key := -time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, time.Local).Unix() grouped[key] = append(grouped[key], post) } w := req.Respond(web.StatusOK, web.HeaderContentType, "text/html; charset=utf-8") view.RenderLayout(w, &view.RenderInfo{ MonthlyArchive: grouped, Description: "Archives by month", Title: "Monthly archives", ArchiveLinks: true, Canonical: req.URL.Path, }) } }