~sotirisp/kindleto

ref: faaff15151ac14fbdf0cb20b3b6a145bf127af3a kindleto/templates.go -rw-r--r-- 7.3 KiB
faaff151Sotiris Papatheodorou Reduce the TOFU cache save interval 3 months ago
                                                                                
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// SPDX-FileCopyrightText: 2020 Paul Gorman
// SPDX-FileCopyrightText: 2021 Sotiris Papatheodorou
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import (
	"html/template"
	"os"
)

var templateCertificates string = `{{template "header" .}}
{{if .Error}}
<div id="error">ERROR: {{.Error}}</div>
{{end}}
<div id="manage-client-certs">
<h1>Manage Client Certificates</h1>
{{range .Certs}}
<div class="client-cert">
<h3>{{.URL}}</h3>
<p>Expires: {{.Expires}}{{if .CertName}}<br>
Name: {{.CertName}}{{end}}</p>
<form class="delete-client-cert" action="/settings/certificates" method="POST">
<input type="hidden" name="delete" value="delete">
<input type="hidden" id="url" name="url" value="{{.URL}}">
<button id="delete-client-cert-button">DELETE certificate</button>
</form>
</div>
{{else}}
<p>No client certs found.</p>
{{end}}
{{template "footer"}}`

var templateFooterOnly string = `{{template "footer"}}`

var templateFooter string = `{{define "footer"}}
<div id="footer">
<div id="footer-menu">
<a href="#header">Back to the top</a>
</div>
</div>
</body>
</html>
{{end}}`

var templateGeminiCertificate string = `{{template "header" .}}
{{if .Error}}
<div id="error">ERROR: {{.Error}}</div>
{{end}}
<div id="client-cert-ask">
<h1>Send Client Certificate?</h1>
<p>The following site has asked you to send a client certificate before it will serve the content you requested:</p>
<p id="url-asking-for-client-cert">{{.URL}}</p>
<p>We can create a temporary/transient TLS client certificate to send to the server. This will temporarily uniquely identify you to this server, effectively creating a user session. This identity will not be available to other sites, and will expire after {{.Count}} hours.</p>
<p>Optionally, you may enter a name that will be used for the certificates Organizaton and CommonName values. This name will be sent to the server. If you leave Certificate Name empty, Kindleto will generate a random value. If in doubt, leave the name empty.</p>
<form id="client-cert-form" action="/certificate" method="POST">
<label for="client-cert-name-input">Certificate Name (OPTIONAL; will be sent to server)</label>
<input type="text" id="client-cert-name-input" name="name">
<input type="hidden" id="url" name="url" value="{{.URL}}">
<button id="client-cert-form-button">Yes, create and send a transient client certificate</button>
</form>
{{template "footer"}}`

var templateGeminiInput string = `{{template "header" .}}
{{if .Error}}
<div id="error">ERROR: {{.Error}}</div>
{{end}}
{{if .Meta}}
<div id="gemini-input">
<h1>{{.Meta}}</h1>
<form id="input-form" action="/" method="POST">
<input type="hidden" id="url" name="url" value="{{.URL}}">
<textarea id="input" name="input" rows="5" cols="80"></textarea>
<button id="input-form-button">Submit</button>
</form>
</div>
{{end}}
{{template "footer"}}`

var templateGeminiSensitiveInput string = `{{template "header" .}}
{{if .Error}}
<div id="error">ERROR: {{.Error}}</div>
{{end}}
{{if .Meta}}
<div id="gemini-input-secret">
<h1>{{.Meta}}</h1>
<form id="input-secret-form" action="/" method="POST">
<input type="hidden" id="url" name="url" value="{{.URL}}">
<input type="password" id="secret" name="secret">
<button id="input-form-button">Submit</button>
</form>
</div>
{{end}}
{{template "footer"}}`

var templateHeaderOnly string = `{{template "header" .}}
{{if .Error}}<div id="error">ERROR: {{.Error}}</div>{{end}}
{{if .Warning}}<div id="warning">Warning: {{.Warning}}</div>{{end}}`

var templateHeader string = `{{define "header"}}
<!DOCTYPE html>
<html lang="{{if .Lang}}{{.Lang}}{{else}}en-US{{end}}">
<head>
<meta charset="{{if .Charset}}{{.Charset}}{{else}}utf-8{{end}}">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/kindleto.css">
<title>{{.Title}}</title>
</head>
<body>
<div id="header">
<div id="header-menu">
<a href="/">Home</a>
{{if .URLUp}}<a href="/?url={{.URLUp}}">Up</a>{{end}}
<span style="float:right;">
{{if .URL}}<a href="/?source=1&url={{.URL}}">Source</a>{{end}}
{{if .ManageCerts}}<a href="/settings/certificates">Certificates</a>{{end}}
<a href="/about.html">About</a>
</span>
</div>
<form id="url-form" action="/" method="POST">
<input id="url-input" maxlength="1024" name="url" type="url" value="{{.URL}}">
<button id="url-form-button">Go</button>
</form>
</div>
{{end}}`

var templateAbout string = `{{define "title"}}Kindleto{{end}}
{{template "header" .}}
{{if .Error}}
<div id="error">{{.Error}}</div>
{{end}}
<h1>Kindleto</h1>

<p>Kindleto is based on <a href="https://github.com/pgorman/gneto">Gneto</a> by Paul Gorman.

<p>Copyright 2020 Paul Gorman</br>Copyright 2021 Sotiris Papatheodorou</p>

<p>This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the <a href="https://www.gnu.org/licenses/gpl.html">GNU General Public License</a> as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.</p>
{{template "footer"}}`

var templateHome string = `{{template "header" .}}
{{if .Error}}<div id="error">ERROR: {{.Error}}</div>
{{else if .HTML}}{{if .Warning}}<div id="warning">Warning: {{.Warning}}</div>{{end}}
<div id="content">{{.HTML}}</div>
{{else}}
<div id="home">
<p>Looking to get started? Enter a Gemini URL in the form above, or follow one of the links below!</p>
<p><a href="?url=gemini%3A%2F%2Fgemini.circumlunar.space%2Fcapcom%2F">CAPCOM</a>, a Gemini content aggregator</p>
<p><a href="?url=gemini%3A%2F%2Frawtext.club%3A1965%2F~sloum%2Fspacewalk.gmi">Spacewalk</a>, a Gemini content aggregator</p>
<p><a href="?url=gemini%3A%2F%2Fgemini.circumlunar.space%2F">Project Gemini</a></p>
<p><a href="?url=gemini%3A%2F%2Ftildeverse.org/">Tildeverse</a>, communities organized around public access unix servers</p>
</div>
{{end}}
{{template "footer"}}`

func loadTemplates(webRoot string) *template.Template {
	tmpls = template.New("")
	tmpls = template.Must(tmpls.New("about.html.tmpl").Parse(templateAbout))
	tmpls = template.Must(tmpls.New("certificates.html.tmpl").Parse(templateCertificates))
	tmpls = template.Must(tmpls.New("footer-only.html.tmpl").Parse(templateFooterOnly))
	tmpls = template.Must(tmpls.New("footer.html.tmpl").Parse(templateFooter))
	tmpls = template.Must(tmpls.New("gemini-certificate.html.tmpl").Parse(templateGeminiCertificate))
	tmpls = template.Must(tmpls.New("gemini-input.html.tmpl").Parse(templateGeminiInput))
	tmpls = template.Must(tmpls.New("gemini-sensitive-input.html.tmpl").Parse(templateGeminiSensitiveInput))
	tmpls = template.Must(tmpls.New("header-only.html.tmpl").Parse(templateHeaderOnly))
	tmpls = template.Must(tmpls.New("header.html.tmpl").Parse(templateHeader))
	tmpls = template.Must(tmpls.New("home.html.tmpl").Parse(templateHome))
	if webRoot != "" {
		templateFiles := []string{
			webRoot + "/about.html.tmpl",
			webRoot + "/certificates.html.tmpl",
			webRoot + "/footer-only.html.tmpl",
			webRoot + "/footer.html.tmpl",
			webRoot + "/gemini-certificate.html.tmpl",
			webRoot + "/gemini-input.html.tmpl",
			webRoot + "/gemini-sensitive-input.html.tmpl",
			webRoot + "/header-only.html.tmpl",
			webRoot + "/header.html.tmpl",
			webRoot + "/home.html.tmpl",
		}
		for _, filename := range templateFiles {
			if _, err := os.Stat(filename); err == nil {
				tmpls = template.Must(tmpls.ParseFiles(filename))
			}
		}
	}
	return tmpls
}