From 16c91c1139d9f03f42c63298f2179822b8d50eb2 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 15 May 2023 18:47:06 +0000 Subject: properly implement subpath handling --- page/default.go | 56 ++++++++++++++++++++++++++++++++------------------------ page/page.go | 16 ++++++++-------- 2 files changed, 40 insertions(+), 32 deletions(-) (limited to 'page') diff --git a/page/default.go b/page/default.go index 822f310..9598200 100644 --- a/page/default.go +++ b/page/default.go @@ -3,12 +3,16 @@ package page import ( "strings" "text/template" + "sanine.net/git/phlox/config" ) type page struct { Title string - Body string + Head string + FormMethod string + FormAction string + FormBody string } @@ -22,7 +26,12 @@ func buildPage(p page) (string, error) { {{ .Title }} - {{ .Body }} +

{{ .Head }}

+ {{ if .FormBody }} +
+ {{ .FormBody }} +
+ {{ end }} `) @@ -41,48 +50,47 @@ func buildPage(p page) (string, error) { } -func DefaultLogin() (string, error) { +func DefaultLogin(api config.Api) (string, error) { return buildPage(page{ Title: "Login", - Body: ` -
- - -
- - -
- -
+ Head : "Log In", + FormMethod: "POST", + FormAction: api.Prefix + api.Login, + FormBody: ` + + +
+ + +
+ `, }) } -func DefaultLoggedIn() (string, error) { +func DefaultLoggedIn(api config.Api) (string, error) { return buildPage(page{ Title: "Logged In", - Body: ` -

You are logged in.

-
- -
- `, + Head: "You are logged in.", + FormMethod: "GET", + FormAction: api.Prefix + api.Logout, + FormBody: ``, }) } -func DefaultError404() (string, error) { +func DefaultError404(api config.Api) (string, error) { return buildPage(page{ Title: "404 Not Found", - Body: "

Error 404: Page not found

", + Head: "Error 404: Page not found", }) } -func DefaultError500() (string, error) { +func DefaultError500(api config.Api) (string, error) { return buildPage(page{ Title: "500 Internal Server Error", - Body: "

Error 500: Internal server error

", + Head: "Error 500: Internal server error", }) } diff --git a/page/page.go b/page/page.go index c58a344..8b44f06 100644 --- a/page/page.go +++ b/page/page.go @@ -40,7 +40,7 @@ func (p Pages) ServeError500(w http.ResponseWriter) { } -type DefaultPage func() (string, error) +type DefaultPage func(api config.Api) (string, error) func fileExists(filename string) (bool, error) { @@ -56,7 +56,7 @@ func fileExists(filename string) (bool, error) { } -func loadPage(c config.Config, filename string, fallback DefaultPage) (string, error) { +func loadPage(c config.Config, api config.Api, filename string, fallback DefaultPage) (string, error) { fullPath := filepath.Join(c.AssetDirectory, filename) exist, err := fileExists(fullPath) if err != nil { @@ -72,7 +72,7 @@ func loadPage(c config.Config, filename string, fallback DefaultPage) (string, e } else { // file does not exist // use built-in - str, err := fallback() + str, err := fallback(api) if err != nil { return "", err } @@ -81,27 +81,27 @@ func loadPage(c config.Config, filename string, fallback DefaultPage) (string, e } -func LoadPages(c config.Config) (Pages, error) { +func LoadPages(c config.Config, api config.Api) (Pages, error) { var pages Pages var err error - pages.Login, err = loadPage(c, "login.html", DefaultLogin) + pages.Login, err = loadPage(c, api, "login.html", DefaultLogin) if err != nil { return Pages{}, err } - pages.LoggedIn, err = loadPage(c, "logged_in.html", DefaultLoggedIn) + pages.LoggedIn, err = loadPage(c, api, "logged_in.html", DefaultLoggedIn) if err != nil { return Pages{}, err } - pages.Error404, err = loadPage(c, "404.html", DefaultError404) + pages.Error404, err = loadPage(c, api, "404.html", DefaultError404) if err != nil { return Pages{}, err } - pages.Error500, err = loadPage(c, "500.html", DefaultError500) + pages.Error500, err = loadPage(c, api, "500.html", DefaultError500) if err != nil { return Pages{}, err } -- cgit v1.2.1