From 17e9e0b73555316c26320920e4484d2d26dbdb48 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Sat, 17 Jul 2021 10:19:17 -0500 Subject: [PATCH] Add MustWriteTemplate func for the common case of panicking --- src/website/feed.go | 12 ++-------- src/website/forums.go | 18 +++------------ src/website/projects.go | 5 +---- src/website/requesthandling.go | 7 ++++++ src/website/routes.go | 5 +---- src/website/showcase.go | 5 +---- src/website/snippet.go | 40 +++++++++++++++++----------------- src/website/staticpages.go | 37 ++++++------------------------- src/website/timeline_helper.go | 2 +- src/website/user.go | 20 ++++------------- 10 files changed, 47 insertions(+), 104 deletions(-) diff --git a/src/website/feed.go b/src/website/feed.go index 0d56bc1c..22eb7f78 100644 --- a/src/website/feed.go +++ b/src/website/feed.go @@ -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 } diff --git a/src/website/forums.go b/src/website/forums.go index 74715c91..397e93bc 100644 --- a/src/website/forums.go +++ b/src/website/forums.go @@ -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 } diff --git a/src/website/projects.go b/src/website/projects.go index 62d862a6..b727c598 100644 --- a/src/website/projects.go +++ b/src/website/projects.go @@ -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 } diff --git a/src/website/requesthandling.go b/src/website/requesthandling.go index c046b928..d0d0db12 100644 --- a/src/website/requesthandling.go +++ b/src/website/requesthandling.go @@ -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, diff --git a/src/website/routes.go b/src/website/routes.go index 54fb9634..983dab85 100644 --- a/src/website/routes.go +++ b/src/website/routes.go @@ -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")) } diff --git a/src/website/showcase.go b/src/website/showcase.go index c531b5e4..f7b7f015 100644 --- a/src/website/showcase.go +++ b/src/website/showcase.go @@ -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 } diff --git a/src/website/snippet.go b/src/website/snippet.go index aa6d744a..fa384fb7 100644 --- a/src/website/snippet.go +++ b/src/website/snippet.go @@ -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...) } diff --git a/src/website/staticpages.go b/src/website/staticpages.go index 156e6bf2..9eb5cedf 100644 --- a/src/website/staticpages.go +++ b/src/website/staticpages.go @@ -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 } diff --git a/src/website/timeline_helper.go b/src/website/timeline_helper.go index 0cf54f4e..f9367a55 100644 --- a/src/website/timeline_helper.go +++ b/src/website/timeline_helper.go @@ -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", diff --git a/src/website/user.go b/src/website/user.go index 6fb5f036..c80255b9 100644 --- a/src/website/user.go +++ b/src/website/user.go @@ -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 }