flounder

A simple gemini site builder
Log | Files | Refs | README | LICENSE

commit e9ad207d9aee1e48d19f429c68cc8c8b0d2ae2fd
parent b3d4a48d0e8de1edfdf8c9f4a7d549644f973da4
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu,  7 Jan 2021 20:10:58 -0800

Refactor: reference config from templates

Diffstat:
Mhttp.go | 113+++++++++++++++++++++++++++++++++++++------------------------------------------
Mtemplates/admin.html | 5++---
Mtemplates/error.html | 1+
Mtemplates/feed.html | 3+--
Mtemplates/header.html | 2+-
Mtemplates/index.html | 11+++++------
Mtemplates/login.html | 2+-
Mtemplates/message.html | 4++--
Mtemplates/my_site.html | 5++---
Mtemplates/register.html | 2+-
10 files changed, 69 insertions(+), 79 deletions(-)

diff --git a/http.go b/http.go @@ -31,10 +31,9 @@ func renderDefaultError(w http.ResponseWriter, statusCode int) { 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 @@ func rootHandler(w http.ResponseWriter, r *http.Request) { 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 @@ func feedHandler(w http.ResponseWriter, r *http.Request) { 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 @@ func editFileHandler(w http.ResponseWriter, r *http.Request) { 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 @@ func mySiteHandler(w http.ResponseWriter, r *http.Request) { 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 @@ func myAccountHandler(w http.ResponseWriter, r *http.Request) { } 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 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { } 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 @@ func loginHandler(w http.ResponseWriter, r *http.Request) { 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 isOkUsername(s string) error { 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 @@ func registerHandler(w http.ResponseWriter, r *http.Request) { } 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 @@ func adminHandler(w http.ResponseWriter, r *http.Request) { 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 deleteAccountHandler(w http.ResponseWriter, r *http.Request) { 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 { diff --git a/templates/admin.html b/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 }} diff --git a/templates/error.html b/templates/error.html @@ -1,4 +1,5 @@ {{template "header" .}} +<title>Error!</title> <h1>Error</h1> <div class="error">{{.StatusCode}} {{ .ErrorMsg }}</div> {{template "footer" .}} diff --git a/templates/feed.html b/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> diff --git a/templates/header.html b/templates/header.html @@ -3,7 +3,7 @@ <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>"> diff --git a/templates/index.html b/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 @@ Welcome to flounder! For more information and site updates, check out the <a hre <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" .}} diff --git a/templates/login.html b/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> diff --git a/templates/message.html b/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" .}} diff --git a/templates/my_site.html b/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" .}} diff --git a/templates/register.html b/templates/register.html @@ -9,7 +9,7 @@ size="27" type="text" value="" - />.{{.Host}} + />.{{.Config.Host}} </div> <div> <label for="email">Email</label> <br>