diff options
Diffstat (limited to 'page')
-rw-r--r-- | page/default.go | 56 | ||||
-rw-r--r-- | page/page.go | 16 |
2 files changed, 40 insertions, 32 deletions
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>{{ .Title }}</title> </head> <body> - {{ .Body }} + <h1>{{ .Head }}</h1> + {{ if .FormBody }} + <form method="{{ .FormMethod }}" action="{{ .FormAction }}"> + {{ .FormBody }} + </form> + {{ end }} </body> </html> `) @@ -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: ` - <form method="post"> - <label for="username">Username</label> - <input type="text" id="username" name="username"> - <br> - <label for="password">Password</label> - <input type="password" id="password" name="password"> - <br> - <input type="submit" value="Submit"> - </form> + Head : "Log In", + FormMethod: "POST", + FormAction: api.Prefix + api.Login, + FormBody: ` + <label for="username">Username</label> + <input type="text" id="username" name="username"> + <br> + <label for="password">Password</label> + <input type="password" id="password" name="password"> + <br> + <input type="submit" value="Submit"> `, }) } -func DefaultLoggedIn() (string, error) { +func DefaultLoggedIn(api config.Api) (string, error) { return buildPage(page{ Title: "Logged In", - Body: ` - <h1>You are logged in.</h1> - <form method="get" action="/phlox/logout"> - <input type="submit" value="Log Out"> - </form> - `, + Head: "You are logged in.", + FormMethod: "GET", + FormAction: api.Prefix + api.Logout, + FormBody: `<input type="submit" value="Log Out">`, }) } -func DefaultError404() (string, error) { +func DefaultError404(api config.Api) (string, error) { return buildPage(page{ Title: "404 Not Found", - Body: "<h1>Error 404: Page not found</h1>", + 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: "<h1>Error 500: Internal server error</h1>", + 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 } |