diff --git a/cmd/data/user.go b/cmd/data/user.go index 79cc090..386c27e 100644 --- a/cmd/data/user.go +++ b/cmd/data/user.go @@ -9,10 +9,10 @@ const ( type Role int type User struct { - RejectedArticles *ArticleList UserName string FirstName string LastName string + RejectedArticles []*Article ID int64 Role } diff --git a/cmd/ui/admin.go b/cmd/ui/admin.go index ce93901..74b1329 100644 --- a/cmd/ui/admin.go +++ b/cmd/ui/admin.go @@ -11,8 +11,8 @@ import ( ) type AddUserData struct { - data.User Msg string + data.User } func inputsEmpty(user data.User, pass, pass2 string) bool { @@ -46,16 +46,27 @@ func HomePage(db *data.DB) http.HandlerFunc { } if numRows == 0 { - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil) + files := []string{ + "web/templates/index.html", + "web/templates/add-user.html", + } + tmpl, err := template.ParseFiles(files...) + template.Must(tmpl, err).Execute(w, nil) } else { - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) + files := []string{ + "web/templates/index.html", + "web/templates/login.html", + } + tmpl, err := template.ParseFiles(files...) + template.Must(tmpl, err).Execute(w, nil) } } } func CreateUser() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/add-user.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } @@ -81,24 +92,30 @@ func AddUser(db *data.DB) http.HandlerFunc { if inputsEmpty(htmlData.User, pass, pass2) { htmlData.Msg = "Alle Felder müssen ausgefüllt werden." - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) + tmpl, err := template.ParseFiles("web/templates/add-user.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } userString, stringLen, ok := checkUserStrings(htmlData.User) if !ok { - htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) + htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", + stringLen, " Zeichen erlaubt.") + tmpl, err := template.ParseFiles("web/templates/add-user.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } id, _ := db.GetID(htmlData.UserName) if id != 0 { - htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) + htmlData.Msg = fmt.Sprint(htmlData.UserName, + " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") + tmpl, err := template.ParseFiles("web/templates/add-user.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } if pass != pass2 { htmlData.Msg = "Die Passwörter stimmen nicht überein." - template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) + tmpl, err := template.ParseFiles("web/templates/add-user.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData) return } @@ -107,6 +124,7 @@ func AddUser(db *data.DB) http.HandlerFunc { http.Error(w, err.Error(), http.StatusInternalServerError) return } - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } diff --git a/cmd/ui/articles.go b/cmd/ui/articles.go index a3c9281..456b7b8 100644 --- a/cmd/ui/articles.go +++ b/cmd/ui/articles.go @@ -13,13 +13,15 @@ import ( func ShowHub() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func WriteArticle() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/editor.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } @@ -54,13 +56,15 @@ func FinishArticle(l *data.ArticleList) http.HandlerFunc { l.Add(article) - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - template.Must(template.ParseFiles("web/templates/unpublished-articles.html")).ExecuteTemplate(w, "page-content", l.Get()) + tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", l.Get()) } } @@ -75,11 +79,13 @@ func ReviewArticle(l *data.ArticleList) http.HandlerFunc { for _, article := range l.Get() { if article.UUID == uuid { - template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article) + tmpl, err := template.ParseFiles("web/templates/to-be-published.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article) return } } - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } @@ -96,7 +102,8 @@ func PublishArticle(f *data.Feed, l *data.ArticleList) http.HandlerFunc { if !ok { // TODO: Warnung anzeigen // msg = "Alle Felder müssen ausgefüllt werden." - // template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", msg) + // tmpl, err := template.ParseFiles("web/templates/add-user.html") + // template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) return } @@ -108,6 +115,7 @@ func PublishArticle(f *data.Feed, l *data.ArticleList) http.HandlerFunc { }) f.Save("tmp/rss.gob") - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } diff --git a/cmd/ui/rss.go b/cmd/ui/rss.go index 1b7e4bb..267bef9 100644 --- a/cmd/ui/rss.go +++ b/cmd/ui/rss.go @@ -17,6 +17,9 @@ func ShowRSS(f *data.Feed) http.HandlerFunc { http.Error(w, err.Error(), http.StatusInternalServerError) return } - template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss) + + files := []string{"web/templates/index.html", "web/templates/feed.rss"} + tmpl, err := template.ParseFiles(files...) + template.Must(tmpl, err).Execute(w, rss) } } diff --git a/cmd/ui/session.go b/cmd/ui/session.go index fd801fe..7ca3619 100644 --- a/cmd/ui/session.go +++ b/cmd/ui/session.go @@ -26,6 +26,7 @@ func Login(db *data.DB) http.HandlerFunc { return } - template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) + tmpl, err := template.ParseFiles("web/templates/hub.html") + template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } }