Refactor: reference config from templates
alex wennerberg alex@alexwennerberg.com
Thu, 07 Jan 2021 20:10:58 -0800
10 files changed,
69 insertions(+),
79 deletions(-)
M
http.go
→
http.go
@@ -31,10 +31,9 @@ }
func renderError(w http.ResponseWriter, errorMsg string, statusCode int) { data := struct { - PageTitle string StatusCode int ErrorMsg string - }{"Error!", statusCode, errorMsg} + }{statusCode, errorMsg} err := t.ExecuteTemplate(w, "error.html", data) if err != nil { // Shouldn't happen probably http.Error(w, errorMsg, statusCode)@@ -64,12 +63,11 @@ if err != nil {
panic(err) } data := struct { - Host string - PageTitle string - Files []*File - Users []string - AuthUser AuthUser - }{c.Host, c.SiteTitle, indexFiles, allUsers, user} + Config Config + AuthUser AuthUser + Files []*File + Users []string + }{c, user, indexFiles, allUsers} err = t.ExecuteTemplate(w, "index.html", data) if err != nil { panic(err)@@ -83,12 +81,11 @@ if err != nil {
panic(err) } data := struct { - Host string - PageTitle string + Config Config FeedEntries []FeedEntry Feeds []Gemfeed AuthUser AuthUser - }{c.Host, c.SiteTitle, feedEntries, feeds, user} + }{c, feedEntries, feeds, user} err = t.ExecuteTemplate(w, "feed.html", data) if err != nil { panic(err)@@ -178,17 +175,17 @@ if err != nil {
panic(err) } data := struct { - FileName string - FileText string - PageTitle string - AuthUser AuthUser - Host string - IsText bool - IsGemini bool - IsGemlog bool - Alert string - Warnings []string - }{fileName, string(fileBytes), c.SiteTitle, user, c.Host, isText, isGemini(fileName), strings.HasPrefix(fileName, "gemlog"), alert, warnings} + FileName string + FileText string + Config Config + AuthUser AuthUser + Host string + IsText bool + IsGemini bool + IsGemlog bool + Alert string + Warnings []string + }{fileName, string(fileBytes), c, user, c.Host, isText, isGemini(fileName), strings.HasPrefix(fileName, "gemlog"), alert, warnings} err = t.ExecuteTemplate(w, "edit_file.html", data) if err != nil { panic(err)@@ -266,12 +263,11 @@ userFolder := getUserDirectory(user.Username)
files, _ := getMyFilesRecursive(userFolder, user.Username) currentDate := time.Now().Format("2006-01-02") data := struct { - Host string - PageTitle string + Config Config Files []File AuthUser AuthUser CurrentDate string - }{c.Host, c.SiteTitle, files, user, currentDate} + }{c, files, user, currentDate} _ = t.ExecuteTemplate(w, "my_site.html", data) }@@ -284,12 +280,12 @@ return
} me, _ := getUserByName(user.Username) type pageData struct { - PageTitle string - AuthUser AuthUser - Email string - Errors []string + Config Config + AuthUser AuthUser + Email string + Errors []string } - data := pageData{"My Account", user, me.Email, nil} + data := pageData{c, user, me.Email, nil} if r.Method == "GET" { err := t.ExecuteTemplate(w, "me.html", data)@@ -352,9 +348,9 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" { // show page data := struct { - Error string - PageTitle string - }{"", "Login"} + Error string + Config Config + }{"", c} err := t.ExecuteTemplate(w, "login.html", data) if err != nil { panic(err)@@ -374,9 +370,9 @@ panic(err)
} if db_password != nil && !active { data := struct { - Error string - PageTitle string - }{"Your account is not active yet. Pending admin approval", c.SiteTitle} + Error string + Config Config + }{"Your account is not active yet. Pending admin approval", c} t.ExecuteTemplate(w, "login.html", data) return }@@ -389,9 +385,9 @@ session.Save(r, w)
http.Redirect(w, r, "/my_site", http.StatusSeeOther) } else { data := struct { - Error string - PageTitle string - }{"Invalid login or password", c.SiteTitle} + Error string + Config Config + }{"Invalid login or password", c} err := t.ExecuteTemplate(w, "login.html", data) if err != nil { panic(err)@@ -433,10 +429,9 @@ }
func registerHandler(w http.ResponseWriter, r *http.Request) { if r.Method == "GET" { data := struct { - Host string - Errors []string - PageTitle string - }{c.Host, nil, "Register"} + Errors []string + Config Config + }{nil, c} err := t.ExecuteTemplate(w, "register.html", data) if err != nil { panic(err)@@ -470,17 +465,16 @@ }
} if len(errors) > 0 { data := struct { - Host string - Errors []string - PageTitle string - }{c.Host, errors, "Register"} + Config Config + Errors []string + }{c, errors} t.ExecuteTemplate(w, "register.html", data) } else { data := struct { - Host string - Message string - PageTitle string - }{c.Host, "Registration complete! The server admin will approve your request before you can log in.", "Registration Complete"} + Config Config + Message string + Title string + }{c, "Registration complete! The server admin will approve your request before you can log in.", "Registration Complete"} t.ExecuteTemplate(w, "message.html", data) } }@@ -512,11 +506,10 @@ renderDefaultError(w, http.StatusInternalServerError)
return } data := struct { - Users []User - AuthUser AuthUser - PageTitle string - Host string - }{allUsers, user, "Admin", c.Host} + Users []User + AuthUser AuthUser + Config Config + }{allUsers, user, c} err = t.ExecuteTemplate(w, "admin.html", data) if err != nil { panic(err)@@ -638,10 +631,10 @@
func resetPasswordHandler(w http.ResponseWriter, r *http.Request) { user := newGetAuthUser(r) data := struct { - PageTitle string - AuthUser AuthUser - Error string - }{"Reset Password", user, ""} + Config Config + AuthUser AuthUser + Error string + }{c, user, ""} if r.Method == "GET" { err := t.ExecuteTemplate(w, "reset_pass.html", data) if err != nil {
M
templates/admin.html
→
templates/admin.html
@@ -1,13 +1,12 @@
-{{$domain := .Host}} {{template "header" .}} -<h1>{{.PageTitle}}</h1> +<h1>Admin</h1> {{template "nav.html" .}} <br> {{ range .Users }} <details> <summary><b>{{.Username}}</b> {{if not .Active}}<em>(inactive)</em>{{end}}</summary> <div class="user-admin-details"> - <p>Home: <a href="//{{.Username}}.{{$domain}}">{{.Username}}</a> </p> + <p>Home: <a href="//{{.Username}}.{{$.Config.Host}}">{{.Username}}</a> </p> <p>Email: <a href=mailto:{{.Email}}>{{.Email}}</a></p> <p>Reference: {{.Reference}}</p> {{ if not .Active }}
M
templates/error.html
→
templates/error.html
@@ -1,4 +1,5 @@
{{template "header" .}} +<title>Error!</title> <h1>Error</h1> <div class="error">{{.StatusCode}} {{ .ErrorMsg }}</div> {{template "footer" .}}
M
templates/feed.html
→
templates/feed.html
@@ -1,6 +1,5 @@
-{{$domain := .Host}} {{template "header" .}} -<h1>π{{.PageTitle}} -- Feeds</h1> +<h1>π{{.Config.SiteTitle}} -- Feeds</h1> {{template "nav.html" .}} <br> <p>
M
templates/header.html
→
templates/header.html
@@ -3,7 +3,7 @@ <!DOCTYPE html>
<html lang="en"> <head> <meta charset="utf-8" /> - <title>{{.PageTitle }}</title> + <title>{{.Config.SiteTitle}}</title> <meta name="viewport" content="width=device-width" /> <link rel="stylesheet" type="text/css" href="https://flounder.online/style.css" /> <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>π</text></svg>">
M
templates/index.html
→
templates/index.html
@@ -1,17 +1,16 @@
-{{$domain := .Host}} {{template "header" .}} -<h1>π{{.PageTitle}}!</h1> +<h1>π{{.Config.SiteTitle}}!</h1> {{template "nav.html" .}} <br> <p> -Welcome to flounder! For more information and site updates, check out the <a href="//admin.{{$domain}}">admin page</a></p> +Welcome to flounder! For more information and site updates, check out the <a href="//admin.{{$.Config.Host}}">admin page</a></p> <h2>Recently updated files:</h2> {{ range .Files }} <div> - <a href="//{{.Creator}}.{{$domain}}" class='person-link'> + <a href="//{{.Creator}}.{{$.Config.Host}}" class='person-link'> {{ .Creator }}</a> <em>{{.TimeAgo}}</em> - <a href="//{{.Creator}}.{{$domain}}/{{.Name}}"> + <a href="//{{.Creator}}.{{$.Config.Host}}/{{.Name}}"> {{ .Name}} </a> </div>@@ -19,6 +18,6 @@ {{end}}
<br> <h2>All users:</h2> {{ range .Users}} -<a href="//{{.}}.{{$domain}}" class='person-link'>{{.}}</a> +<a href="//{{.}}.{{$.Config.Host}}" class='person-link'>{{.}}</a> {{end}} {{template "footer" .}}
M
templates/login.html
→
templates/login.html
@@ -1,5 +1,5 @@
{{template "header" .}} -<h1>Login</h1> +<h1>πLogin</h1> <form action="/login" method="post"> <p> <label for="username">Username or Email</label>
M
templates/message.html
→
templates/message.html
@@ -1,5 +1,5 @@
{{template "header" .}} -<h1>{{.PageTitle}}</h1> +<h1>{{.Title}}</h1> {{ .Message }} -<a href="//{{.Host}}">Go home</a> +<a href="//{{.Config.Host}}">Go home</a> {{template "footer" .}}
M
templates/my_site.html
→
templates/my_site.html
@@ -1,9 +1,8 @@
-{{$domain := .Host}} {{$authUser := .AuthUser.Username}} {{template "header" .}} <h1>Managing - <a href="//{{$authUser}}.{{$domain}}"> - {{$authUser}}.{{$domain}} + <a href="//{{.AuthUser.Username}}.{{.Config.Host}}"> + {{.AuthUser.Username}}.{{.Config.Host}} </a> </h1> {{template "nav.html" .}}
M
templates/register.html
→
templates/register.html
@@ -9,7 +9,7 @@ name="username"
size="27" type="text" value="" - />.{{.Host}} + />.{{.Config.Host}} </div> <div> <label for="email">Email</label> <br>