1
Fork 0

Embed template files, new.css for forms and table
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Jan-Lukas Else 2021-08-11 15:07:28 +02:00
parent 5f62857286
commit c5dd0e6e40
5 changed files with 51 additions and 38 deletions

View File

@ -3,7 +3,8 @@ WORKDIR /app
ENV GOFLAGS="-tags=linux,libsqlite3"
RUN apk add --no-cache git gcc musl-dev
RUN apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/main sqlite-dev
ADD . /app
ADD *.go go.mod go.sum /app/
ADD templates/ /app/templates/
RUN go test -cover ./...
RUN go build -ldflags '-w -s' -o goshort

View File

@ -1,6 +1,7 @@
package main
import (
_ "embed"
"html/template"
"log"
"strings"
@ -17,55 +18,24 @@ func init() {
}
}
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>
`
//go:embed templates/list.gohtml
var listTemplateString string
func initListTemplate() (err error) {
listTemplate, err = template.New("List").Parse(strings.TrimSpace(listTemplateString))
return
}
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>
`
//go:embed templates/urlform.gohtml
var urlFormTemplateString string
func initURLFormTemplate() (err error) {
urlFormTemplate, err = template.New("UrlForm").Parse(strings.TrimSpace(urlFormTemplateString))
return
}
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>
`
//go:embed templates/textform.gohtml
var textFormTemplateString string
func initTextFormTemplate() (err error) {
textFormTemplate, err = template.New("TextForm").Parse(strings.TrimSpace(textFormTemplateString))

19
templates/list.gohtml Normal file
View File

@ -0,0 +1,19 @@
<!doctype html>
<html lang=en>
<meta name=viewport content="width=device-width, initial-scale=1.0">
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@exampledev/new.css@1.1.3/new.min.css>
<title>Short URLs</title>
<h1>Short URLs</h1>
<table>
<thead>
<tr>
<th><span style="white-space: nowrap;">slug</span></th>
<th><span style="white-space: nowrap;">url</span></th>
<th><span style="white-space: nowrap;">hits</span></th>
</tr>
</thead>
<tbody>
{{range .}}<tr><td>{{.Slug}}</td><td>{{.URL}}</td><td>{{.Hits}}</td></tr>{{end}}
</tbody>
</table>
</html>

12
templates/textform.gohtml Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html lang=en>
<meta name=viewport content="width=device-width, initial-scale=1.0">
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@exampledev/new.css@1.1.3/new.min.css>
<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>{{end}}
{{range .TextAreas}}<textarea name={{index . 0}} placeholder={{index . 0}}>{{index . 1}}</textarea><br>{{end}}
<input type=submit value={{.Title}}>
</form>
</html>

11
templates/urlform.gohtml Normal file
View File

@ -0,0 +1,11 @@
<!doctype html>
<html lang=en>
<meta name=viewport content="width=device-width, initial-scale=1.0">
<link rel=stylesheet href=https://cdn.jsdelivr.net/npm/@exampledev/new.css@1.1.3/new.min.css>
<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>{{end}}
<input type=submit value={{.Title}}>
</form>
</html>