2020-05-14 10:20:05 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
2021-08-10 20:34:25 +00:00
|
|
|
"strings"
|
2020-05-14 10:20:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var listTemplate *template.Template
|
|
|
|
var urlFormTemplate *template.Template
|
|
|
|
var textFormTemplate *template.Template
|
|
|
|
|
|
|
|
func init() {
|
2020-11-15 15:52:03 +00:00
|
|
|
if initListTemplate() != nil || initURLFormTemplate() != nil || initTextFormTemplate() != nil {
|
2020-05-14 10:20:05 +00:00
|
|
|
log.Fatal("Failed to initialize templates")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 20:34:25 +00:00
|
|
|
const listTemplateString = `
|
|
|
|
<!doctype html>
|
|
|
|
<html lang=en>
|
|
|
|
<meta name=viewport content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Short URLs</title>
|
|
|
|
<h1>Short URLs</h1>
|
|
|
|
<table>
|
|
|
|
<tr><th>slug</th><th>url</th><th>hits</th></tr>
|
|
|
|
{{range .}}<tr><td>{{.Slug}}</td><td>{{.URL}}</td><td>{{.Hits}}</td></tr>{{end}}
|
|
|
|
</table>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2020-05-14 10:20:05 +00:00
|
|
|
func initListTemplate() (err error) {
|
2021-08-10 20:34:25 +00:00
|
|
|
listTemplate, err = template.New("List").Parse(strings.TrimSpace(listTemplateString))
|
2020-05-14 10:20:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 20:34:25 +00:00
|
|
|
const urlFormTemplateString = `
|
|
|
|
<!doctype html>
|
|
|
|
<html lang=en>
|
|
|
|
<meta name=viewport content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>{{.Title}}</title>
|
|
|
|
<h1>{{.Title}}</h1>
|
|
|
|
<form action={{.URL}} method=post>
|
|
|
|
{{range .Fields}}<input type=text name={{index . 0}} placeholder={{index . 0}} value="{{index . 1}}"><br><br>{{end}}
|
|
|
|
<input type=submit value={{.Title}}>
|
|
|
|
</form>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2020-11-15 15:52:03 +00:00
|
|
|
func initURLFormTemplate() (err error) {
|
2021-08-10 20:34:25 +00:00
|
|
|
urlFormTemplate, err = template.New("UrlForm").Parse(strings.TrimSpace(urlFormTemplateString))
|
2020-05-14 10:20:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 20:34:25 +00:00
|
|
|
const textFormTemplateString = `
|
|
|
|
<!doctype html>
|
|
|
|
<html lang=en>
|
|
|
|
<meta name=viewport content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>{{.Title}}</title>
|
|
|
|
<h1>{{.Title}}</h1>
|
|
|
|
<form action={{.URL}} method=post>
|
|
|
|
{{range .Fields}}<input type=text name={{index . 0}} placeholder={{index . 0}} value="{{index . 1}}"><br><br>{{end}}
|
|
|
|
{{range .TextAreas}}<textarea name={{index . 0}} placeholder={{index . 0}}>{{index . 1}}</textarea><br><br>{{end}}
|
|
|
|
<input type=submit value={{.Title}}>
|
|
|
|
</form>
|
|
|
|
</html>
|
|
|
|
`
|
|
|
|
|
2020-05-14 10:20:05 +00:00
|
|
|
func initTextFormTemplate() (err error) {
|
2021-08-10 20:34:25 +00:00
|
|
|
textFormTemplate, err = template.New("TextForm").Parse(strings.TrimSpace(textFormTemplateString))
|
2020-05-14 10:20:05 +00:00
|
|
|
return
|
|
|
|
}
|