Add MustWriteTemplate func for the common case of panicking

This commit is contained in:
Ben Visness 2021-07-17 10:19:17 -05:00
parent 6c53688e06
commit 17e9e0b735
10 changed files with 47 additions and 104 deletions

View File

@ -94,7 +94,7 @@ func Feed(c *RequestContext) ResponseData {
baseData.BodyClasses = append(baseData.BodyClasses, "feed")
var res ResponseData
err = res.WriteTemplate("feed.html", FeedData{
res.MustWriteTemplate("feed.html", FeedData{
BaseData: baseData,
AtomFeedUrl: hmnurl.BuildAtomFeed(),
@ -102,10 +102,6 @@ func Feed(c *RequestContext) ResponseData {
Posts: posts,
Pagination: pagination,
}, c.Perf)
if err != nil {
panic(err)
}
return res
}
@ -303,11 +299,7 @@ func AtomFeed(c *RequestContext) ResponseData {
}
var res ResponseData
err := res.WriteTemplate("atom.xml", feedData, c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("atom.xml", feedData, c.Perf)
return res
}

View File

@ -260,7 +260,7 @@ func ForumCategory(c *RequestContext) ResponseData {
}
var res ResponseData
err = res.WriteTemplate("forum_category.html", forumCategoryData{
res.MustWriteTemplate("forum_category.html", forumCategoryData{
BaseData: baseData,
NewThreadUrl: hmnurl.BuildForumNewThread(c.CurrentProject.Slug, currentSubforumSlugs, false),
MarkReadUrl: hmnurl.BuildMarkRead(currentCatId),
@ -276,10 +276,6 @@ func ForumCategory(c *RequestContext) ResponseData {
},
Subcategories: subcats,
}, c.Perf)
if err != nil {
panic(err)
}
return res
}
@ -417,7 +413,7 @@ func ForumThread(c *RequestContext) ResponseData {
// TODO(asaf): Set breadcrumbs
var res ResponseData
err = res.WriteTemplate("forum_thread.html", forumThreadData{
res.MustWriteTemplate("forum_thread.html", forumThreadData{
BaseData: baseData,
Thread: templates.ThreadToTemplate(&thread),
Posts: posts,
@ -425,10 +421,6 @@ func ForumThread(c *RequestContext) ResponseData {
ReplyUrl: hmnurl.BuildForumPostReply(c.CurrentProject.Slug, currentSubforumSlugs, thread.ID, *thread.FirstID),
Pagination: pagination,
}, c.Perf)
if err != nil {
panic(err)
}
return res
}
@ -543,15 +535,11 @@ func ForumNewThread(c *RequestContext) ResponseData {
}
var res ResponseData
err := res.WriteTemplate("editor.html", editorData{
res.MustWriteTemplate("editor.html", editorData{
BaseData: baseData,
SubmitUrl: hmnurl.BuildForumNewThread(c.CurrentProject.Slug, lineageBuilder.GetSubforumLineageSlugs(currentCatId), true),
SubmitLabel: "Post New Thread",
}, c.Perf)
if err != nil {
panic(err)
}
return res
}

View File

@ -186,7 +186,7 @@ func ProjectIndex(c *RequestContext) ResponseData {
baseData := getBaseData(c)
baseData.Title = "Project List"
var res ResponseData
err = res.WriteTemplate("project_index.html", ProjectTemplateData{
res.MustWriteTemplate("project_index.html", ProjectTemplateData{
BaseData: baseData,
Pagination: pagination,
@ -203,9 +203,6 @@ func ProjectIndex(c *RequestContext) ResponseData {
RegisterUrl: hmnurl.BuildRegister(),
LoginUrl: hmnurl.BuildLoginPage(c.FullUrl()),
}, c.Perf)
if err != nil {
panic(err)
}
return res
}

View File

@ -249,6 +249,13 @@ func (rd *ResponseData) WriteTemplate(name string, data interface{}, rp *perf.Re
return template.Execute(rd, data)
}
func (rd *ResponseData) MustWriteTemplate(name string, data interface{}, rp *perf.RequestPerf) {
err := rd.WriteTemplate(name, data, rp)
if err != nil {
panic(err)
}
}
func ErrorResponse(status int, errs ...error) ResponseData {
return ResponseData{
StatusCode: status,

View File

@ -298,10 +298,7 @@ func FourOhFour(c *RequestContext) ResponseData {
BaseData: getBaseData(c),
Wanted: c.FullUrl(),
}
err := res.WriteTemplate("404.html", templateData, c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("404.html", templateData, c.Perf)
} else {
res.Write([]byte("Not Found"))
}

View File

@ -56,13 +56,10 @@ func Showcase(c *RequestContext) ResponseData {
baseData := getBaseData(c)
baseData.Title = "Community Showcase"
var res ResponseData
err = res.WriteTemplate("showcase.html", ShowcaseData{
res.MustWriteTemplate("showcase.html", ShowcaseData{
BaseData: baseData,
ShowcaseItems: jsonItems,
ShowcaseAtomFeedUrl: hmnurl.BuildAtomFeedForShowcase(),
}, c.Perf)
if err != nil {
panic(err)
}
return res
}

View File

@ -63,42 +63,42 @@ func Snippet(c *RequestContext) ResponseData {
snippet := SnippetToTimelineItem(&snippetData.Snippet, snippetData.Asset, snippetData.DiscordMessage, &snippetData.Owner, c.Theme)
opengraph := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:site_name", Value: "Handmade.Network"},
templates.OpenGraphItem{Property: "og:type", Value: "article"},
templates.OpenGraphItem{Property: "og:url", Value: snippet.Url},
templates.OpenGraphItem{Property: "og:title", Value: fmt.Sprintf("Snippet by %s", snippet.OwnerName)},
templates.OpenGraphItem{Property: "og:description", Value: string(snippet.Description)},
{Property: "og:site_name", Value: "Handmade.Network"},
{Property: "og:type", Value: "article"},
{Property: "og:url", Value: snippet.Url},
{Property: "og:title", Value: fmt.Sprintf("Snippet by %s", snippet.OwnerName)},
{Property: "og:description", Value: string(snippet.Description)},
}
if snippet.Type == templates.TimelineTypeSnippetImage {
opengraphImage := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:image", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:image:width", Value: strconv.Itoa(snippet.Width)},
templates.OpenGraphItem{Property: "og:image:height", Value: strconv.Itoa(snippet.Height)},
templates.OpenGraphItem{Property: "og:image:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "summary_large_image"},
{Property: "og:image", Value: snippet.AssetUrl},
{Property: "og:image:width", Value: strconv.Itoa(snippet.Width)},
{Property: "og:image:height", Value: strconv.Itoa(snippet.Height)},
{Property: "og:image:type", Value: snippet.MimeType},
{Name: "twitter:card", Value: "summary_large_image"},
}
opengraph = append(opengraph, opengraphImage...)
} else if snippet.Type == templates.TimelineTypeSnippetVideo {
opengraphVideo := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:video", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:video:width", Value: strconv.Itoa(snippet.Width)},
templates.OpenGraphItem{Property: "og:video:height", Value: strconv.Itoa(snippet.Height)},
templates.OpenGraphItem{Property: "og:video:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"},
{Property: "og:video", Value: snippet.AssetUrl},
{Property: "og:video:width", Value: strconv.Itoa(snippet.Width)},
{Property: "og:video:height", Value: strconv.Itoa(snippet.Height)},
{Property: "og:video:type", Value: snippet.MimeType},
{Name: "twitter:card", Value: "player"},
}
opengraph = append(opengraph, opengraphVideo...)
} else if snippet.Type == templates.TimelineTypeSnippetAudio {
opengraphAudio := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:audio", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:audio:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"},
{Property: "og:audio", Value: snippet.AssetUrl},
{Property: "og:audio:type", Value: snippet.MimeType},
{Name: "twitter:card", Value: "player"},
}
opengraph = append(opengraph, opengraphAudio...)
} else if snippet.Type == templates.TimelineTypeSnippetYoutube {
opengraphYoutube := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:video", Value: fmt.Sprintf("https://youtube.com/watch?v=%s", snippet.YoutubeID)},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"},
{Property: "og:video", Value: fmt.Sprintf("https://youtube.com/watch?v=%s", snippet.YoutubeID)},
{Name: "twitter:card", Value: "player"},
}
opengraph = append(opengraph, opengraphYoutube...)
}

View File

@ -1,66 +1,43 @@
package website
import ()
func Manifesto(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("manifesto.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("manifesto.html", getBaseData(c), c.Perf)
return res
}
func About(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("about.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("about.html", getBaseData(c), c.Perf)
return res
}
func CodeOfConduct(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("code_of_conduct.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("code_of_conduct.html", getBaseData(c), c.Perf)
return res
}
func CommunicationGuidelines(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("communication_guidelines.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("communication_guidelines.html", getBaseData(c), c.Perf)
return res
}
func ContactPage(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("contact.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("contact.html", getBaseData(c), c.Perf)
return res
}
func MonthlyUpdatePolicy(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("monthly_update_policy.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("monthly_update_policy.html", getBaseData(c), c.Perf)
return res
}
func ProjectSubmissionGuidelines(c *RequestContext) ResponseData {
var res ResponseData
err := res.WriteTemplate("project_submission_guidelines.html", getBaseData(c), c.Perf)
if err != nil {
panic(err)
}
res.MustWriteTemplate("project_submission_guidelines.html", getBaseData(c), c.Perf)
return res
}

View File

@ -42,7 +42,7 @@ var TimelineItemClassMap = map[templates.TimelineType]string{
var TimelineTypeTitleMap = map[templates.TimelineType]string{
templates.TimelineTypeUnknown: "",
templates.TimelineTypeForumThread: "New forums thread",
templates.TimelineTypeForumThread: "New forum thread",
templates.TimelineTypeForumReply: "Forum reply",
templates.TimelineTypeBlogPost: "New blog post",

View File

@ -222,21 +222,12 @@ func UserProfile(c *RequestContext) ResponseData {
c.Theme,
)
switch timelineItem.Type {
case templates.TimelineTypeForumThread:
case templates.TimelineTypeForumThread, templates.TimelineTypeForumReply:
numForums += 1
case templates.TimelineTypeForumReply:
numForums += 1
case templates.TimelineTypeBlogPost:
case templates.TimelineTypeBlogPost, templates.TimelineTypeBlogComment:
numBlogs += 1
case templates.TimelineTypeBlogComment:
numBlogs += 1
case templates.TimelineTypeWikiCreate:
case templates.TimelineTypeWikiCreate, templates.TimelineTypeWikiTalk:
numWiki += 1
case templates.TimelineTypeWikiTalk:
numWiki += 1
case templates.TimelineTypeLibraryComment:
numLibrary += 1
}
@ -283,7 +274,7 @@ func UserProfile(c *RequestContext) ResponseData {
baseData := getBaseData(c)
var res ResponseData
err = res.WriteTemplate("user_profile.html", UserProfileTemplateData{
res.MustWriteTemplate("user_profile.html", UserProfileTemplateData{
BaseData: baseData,
ProfileUser: templates.UserToTemplate(profileUser, c.Theme),
ProfileUserLinks: profileUserLinks,
@ -295,8 +286,5 @@ func UserProfile(c *RequestContext) ResponseData {
NumLibrary: numLibrary,
NumSnippets: numSnippets,
}, c.Perf)
if err != nil {
panic(err)
}
return res
}