Exemplo n.º 1
0
func feedHandler(req *web.Request) {
	if !feedburner.Match([]byte(req.Header.Get(web.HeaderUserAgent))) {
		// Not Feedburner
		if "" == req.Param.Get("no_fb") {
			// And nothing saying to ignore
			req.Respond(web.StatusMovedPermanently, web.HeaderLocation, feedburnerUrl)
			return
		}
	}

	posts, err := posts.FindLatest(10)
	if err != nil {
		logger.Printf("failed getting posts for feed: %s", err)
		serverError(req, err)
	} else {
		w := req.Respond(web.StatusOK, web.HeaderContentType, "application/rss+xml; charset=utf-8")
		view.RenderPartial(w, "feed.tmpl", &view.RenderInfo{
			Post:            posts,
			SiteTitle:       config.SiteTitle,
			SiteDescription: config.SiteDescription,
			SiteContact:     config.SiteContact,
			SiteAuthor:      config.SiteAuthor,
		})
	}
}
Exemplo n.º 2
0
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,
		})
	}
}
Exemplo n.º 3
0
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",
	})
}
Exemplo n.º 4
0
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.",
	})
}
Exemplo n.º 5
0
func sitemapHandler(req *web.Request) {
	posts, err := posts.FindLatest(posts.Len())
	if err != nil {
		logger.Printf("failed getting posts for sitemap: %s", err)
		serverError(req, err)
	} else {
		w := req.Respond(web.StatusOK, web.HeaderContentType, "application/xml; charset=utf-8")
		view.RenderPartial(w, "sitemap.tmpl", &view.RenderInfo{Post: posts})
	}
}
Exemplo n.º 6
0
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,
		})
	}
}
Exemplo n.º 7
0
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,
		})
	}
}
Exemplo n.º 8
0
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,
		})
	}
}
Exemplo n.º 9
0
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),
		})
	}
}
Exemplo n.º 10
0
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),
		})
	}
}
Exemplo n.º 11
0
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,
		})
	}
}
Exemplo n.º 12
0
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,
		})
	}
}
Exemplo n.º 13
0
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,
		})
	}
}
Exemplo n.º 14
0
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,
		})
	}
}
Exemplo n.º 15
0
func opensearchHandler(req *web.Request) {
	w := req.Respond(web.StatusOK, web.HeaderContentType, "application/xml; charset=utf-8")
	view.RenderPartial(w, "opensearch.tmpl", nil)
}