diff options
Diffstat (limited to 'page')
-rw-r--r-- | page/default.go | 88 | ||||
-rw-r--r-- | page/page.go | 110 |
2 files changed, 198 insertions, 0 deletions
diff --git a/page/default.go b/page/default.go new file mode 100644 index 0000000..ea51acc --- /dev/null +++ b/page/default.go @@ -0,0 +1,88 @@ +package page + +import ( + "strings" + "text/template" +) + + +type page struct { + Title string + Body string +} + + +func buildPage(p page) (string, error) { + t, err := template.New("").Parse(` +<!doctype html> +<html> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>{{ .Title }}</title> + </head> + <body> + {{ .Body }} + </body> +</html> + `) + if err != nil { + return "", err + } + + var b strings.Builder + + err = t.Execute(&b, p) + if err != nil { + return "", err + } + + return b.String(), nil +} + + +func DefaultLogin() (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="text" id="password" name="password"> + <br> + <input type="submit" value="Submit"> + </form> + `, + }) +} + + +func DefaultLoggedIn() (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> + `, + }) +} + + +func DefaultError404() (string, error) { + return buildPage(page{ + Title: "404 Not Found", + Body: "<h1>Error 404: Page not found</h1>", + }) +} + + +func DefaultError500() (string, error) { + return buildPage(page{ + Title: "500 Not Found", + Body: "<h1>Error 500: Page not found</h1>", + }) +} diff --git a/page/page.go b/page/page.go new file mode 100644 index 0000000..c58a344 --- /dev/null +++ b/page/page.go @@ -0,0 +1,110 @@ +package page + +import ( + "os" + "io/fs" + "errors" + "path/filepath" + "net/http" + "sanine.net/git/phlox/config" +) + + +type Pages struct { + Login string + LoggedIn string + Error404 string + Error500 string +} + + +func (p Pages) ServeLogin(w http.ResponseWriter) { + w.Write([]byte(p.Login)) +} + + +func (p Pages) ServeLoggedIn(w http.ResponseWriter) { + w.Write([]byte(p.LoggedIn)) +} + + +func (p Pages) ServeError404(w http.ResponseWriter) { + w.WriteHeader(http.StatusNotFound) + w.Write([]byte(p.Error404)) +} + + +func (p Pages) ServeError500(w http.ResponseWriter) { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(p.Error500)) +} + + +type DefaultPage func() (string, error) + + +func fileExists(filename string) (bool, error) { + _, err := os.Stat(filename) + if err == nil { + return true, nil + } else if errors.Is(err, fs.ErrNotExist) { + return false, nil + } else { + // unknown error + return false, err + } +} + + +func loadPage(c config.Config, filename string, fallback DefaultPage) (string, error) { + fullPath := filepath.Join(c.AssetDirectory, filename) + exist, err := fileExists(fullPath) + if err != nil { + return "", err + } + + if exist { + bytes, err := os.ReadFile(fullPath) + if err != nil { + return "", err + } + return string(bytes), nil + } else { + // file does not exist + // use built-in + str, err := fallback() + if err != nil { + return "", err + } + return str, nil + } +} + + +func LoadPages(c config.Config) (Pages, error) { + var pages Pages + var err error + + pages.Login, err = loadPage(c, "login.html", DefaultLogin) + if err != nil { + return Pages{}, err + } + + pages.LoggedIn, err = loadPage(c, "logged_in.html", DefaultLoggedIn) + if err != nil { + return Pages{}, err + } + + + pages.Error404, err = loadPage(c, "404.html", DefaultError404) + if err != nil { + return Pages{}, err + } + + pages.Error500, err = loadPage(c, "500.html", DefaultError500) + if err != nil { + return Pages{}, err + } + + return pages, nil +} |