コード例 #1
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #2
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #3
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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",
	})
}
コード例 #4
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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.",
	})
}
コード例 #5
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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})
	}
}
コード例 #6
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #7
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #8
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #9
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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),
		})
	}
}
コード例 #10
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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),
		})
	}
}
コード例 #11
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #12
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #13
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #14
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
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,
		})
	}
}
コード例 #15
0
ファイル: handlers.go プロジェクト: darkhelmet/verboselogging
func opensearchHandler(req *web.Request) {
	w := req.Respond(web.StatusOK, web.HeaderContentType, "application/xml; charset=utf-8")
	view.RenderPartial(w, "opensearch.tmpl", nil)
}