summaryrefslogtreecommitdiff
path: root/page/default.go
blob: 9598200d1d695887bf470eeb16ee898a40caa0be (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
89
90
91
92
93
94
95
96
package page

import (
	"strings"
	"text/template"
	"sanine.net/git/phlox/config"
)


type page struct {
	Title string
	Head string
	FormMethod string
	FormAction string
	FormBody 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>
		<h1>{{ .Head }}</h1>
		{{ if .FormBody }}
		<form method="{{ .FormMethod }}" action="{{ .FormAction }}">
			{{ .FormBody }}
		</form>
		{{ end }}
	</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(api config.Api) (string, error) {
	return buildPage(page{
		Title: "Login",
		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(api config.Api) (string, error) {
	return buildPage(page{
		Title: "Logged In",
		Head: "You are logged in.",
		FormMethod: "GET",
		FormAction: api.Prefix + api.Logout,
		FormBody: `<input type="submit" value="Log Out">`,
	})
}


func DefaultError404(api config.Api) (string, error) {
	return buildPage(page{
		Title: "404 Not Found",
		Head: "Error 404: Page not found",
	})
}


func DefaultError500(api config.Api) (string, error) {
	return buildPage(page{
		Title: "500 Internal Server Error",
		Head: "Error 500: Internal server error",
	})
}