summaryrefslogtreecommitdiff
path: root/page/default.go
blob: 822f310c6868c8ffbe4359b8aafca29a2d983664 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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="password" 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 Internal Server Error",
		Body: "<h1>Error 500: Internal server error</h1>",
	})
}