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

View File

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

View File

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

View File

@ -249,6 +249,13 @@ func (rd *ResponseData) WriteTemplate(name string, data interface{}, rp *perf.Re
return template.Execute(rd, data) 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 { func ErrorResponse(status int, errs ...error) ResponseData {
return ResponseData{ return ResponseData{
StatusCode: status, StatusCode: status,

View File

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

View File

@ -56,13 +56,10 @@ func Showcase(c *RequestContext) ResponseData {
baseData := getBaseData(c) baseData := getBaseData(c)
baseData.Title = "Community Showcase" baseData.Title = "Community Showcase"
var res ResponseData var res ResponseData
err = res.WriteTemplate("showcase.html", ShowcaseData{ res.MustWriteTemplate("showcase.html", ShowcaseData{
BaseData: baseData, BaseData: baseData,
ShowcaseItems: jsonItems, ShowcaseItems: jsonItems,
ShowcaseAtomFeedUrl: hmnurl.BuildAtomFeedForShowcase(), ShowcaseAtomFeedUrl: hmnurl.BuildAtomFeedForShowcase(),
}, c.Perf) }, c.Perf)
if err != nil {
panic(err)
}
return res 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) snippet := SnippetToTimelineItem(&snippetData.Snippet, snippetData.Asset, snippetData.DiscordMessage, &snippetData.Owner, c.Theme)
opengraph := []templates.OpenGraphItem{ opengraph := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:site_name", Value: "Handmade.Network"}, {Property: "og:site_name", Value: "Handmade.Network"},
templates.OpenGraphItem{Property: "og:type", Value: "article"}, {Property: "og:type", Value: "article"},
templates.OpenGraphItem{Property: "og:url", Value: snippet.Url}, {Property: "og:url", Value: snippet.Url},
templates.OpenGraphItem{Property: "og:title", Value: fmt.Sprintf("Snippet by %s", snippet.OwnerName)}, {Property: "og:title", Value: fmt.Sprintf("Snippet by %s", snippet.OwnerName)},
templates.OpenGraphItem{Property: "og:description", Value: string(snippet.Description)}, {Property: "og:description", Value: string(snippet.Description)},
} }
if snippet.Type == templates.TimelineTypeSnippetImage { if snippet.Type == templates.TimelineTypeSnippetImage {
opengraphImage := []templates.OpenGraphItem{ opengraphImage := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:image", Value: snippet.AssetUrl}, {Property: "og:image", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:image:width", Value: strconv.Itoa(snippet.Width)}, {Property: "og:image:width", Value: strconv.Itoa(snippet.Width)},
templates.OpenGraphItem{Property: "og:image:height", Value: strconv.Itoa(snippet.Height)}, {Property: "og:image:height", Value: strconv.Itoa(snippet.Height)},
templates.OpenGraphItem{Property: "og:image:type", Value: snippet.MimeType}, {Property: "og:image:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "summary_large_image"}, {Name: "twitter:card", Value: "summary_large_image"},
} }
opengraph = append(opengraph, opengraphImage...) opengraph = append(opengraph, opengraphImage...)
} else if snippet.Type == templates.TimelineTypeSnippetVideo { } else if snippet.Type == templates.TimelineTypeSnippetVideo {
opengraphVideo := []templates.OpenGraphItem{ opengraphVideo := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:video", Value: snippet.AssetUrl}, {Property: "og:video", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:video:width", Value: strconv.Itoa(snippet.Width)}, {Property: "og:video:width", Value: strconv.Itoa(snippet.Width)},
templates.OpenGraphItem{Property: "og:video:height", Value: strconv.Itoa(snippet.Height)}, {Property: "og:video:height", Value: strconv.Itoa(snippet.Height)},
templates.OpenGraphItem{Property: "og:video:type", Value: snippet.MimeType}, {Property: "og:video:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"}, {Name: "twitter:card", Value: "player"},
} }
opengraph = append(opengraph, opengraphVideo...) opengraph = append(opengraph, opengraphVideo...)
} else if snippet.Type == templates.TimelineTypeSnippetAudio { } else if snippet.Type == templates.TimelineTypeSnippetAudio {
opengraphAudio := []templates.OpenGraphItem{ opengraphAudio := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:audio", Value: snippet.AssetUrl}, {Property: "og:audio", Value: snippet.AssetUrl},
templates.OpenGraphItem{Property: "og:audio:type", Value: snippet.MimeType}, {Property: "og:audio:type", Value: snippet.MimeType},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"}, {Name: "twitter:card", Value: "player"},
} }
opengraph = append(opengraph, opengraphAudio...) opengraph = append(opengraph, opengraphAudio...)
} else if snippet.Type == templates.TimelineTypeSnippetYoutube { } else if snippet.Type == templates.TimelineTypeSnippetYoutube {
opengraphYoutube := []templates.OpenGraphItem{ opengraphYoutube := []templates.OpenGraphItem{
templates.OpenGraphItem{Property: "og:video", Value: fmt.Sprintf("https://youtube.com/watch?v=%s", snippet.YoutubeID)}, {Property: "og:video", Value: fmt.Sprintf("https://youtube.com/watch?v=%s", snippet.YoutubeID)},
templates.OpenGraphItem{Name: "twitter:card", Value: "player"}, {Name: "twitter:card", Value: "player"},
} }
opengraph = append(opengraph, opengraphYoutube...) opengraph = append(opengraph, opengraphYoutube...)
} }

View File

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

View File

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

View File

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