From 701544df9cc8ef1632a9e625e2fb93b4525743e2 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Wed, 15 May 2024 07:56:14 +0100 Subject: [PATCH] Inherited templates, staticfiles --- main.go | 13 ++++--- static/reset.css | 80 ++++++++++++++++++++++++++++++++++++++++++++ templates/base.html | 7 ++++ templates/head.html | 5 +++ templates/index.html | 14 +++----- 5 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 static/reset.css create mode 100644 templates/base.html create mode 100644 templates/head.html diff --git a/main.go b/main.go index 63903a0..74e8189 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,10 @@ import ( ) //go:embed templates/* -var content embed.FS +var templates embed.FS + +//go:embed static/* +var staticFiles embed.FS func main() { db := data.Connect("test.sqlite3") @@ -33,7 +36,7 @@ func main() { ping.SendPing(db, failureSite) - http.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) { pings := ping.ListGroupedBySite(db) if _, err := json.Marshal(pings); err != nil { @@ -43,14 +46,16 @@ func main() { } else { w.Header().Set("Content-Type", "text/html") - t, _ := template.ParseFS(content, "templates/index.html") + t, _ := template.ParseFS(templates, "templates/index.html", "templates/base.html", "templates/head.html") t.Execute(w, nil) } }) - http.HandleFunc("POST /", func(w http.ResponseWriter, r *http.Request) { + http.HandleFunc("POST /{$}", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("This was a POST request!")) }) + http.Handle("/static/", http.FileServerFS(staticFiles)) + http.ListenAndServe(":8000", nil) } diff --git a/static/reset.css b/static/reset.css new file mode 100644 index 0000000..9717969 --- /dev/null +++ b/static/reset.css @@ -0,0 +1,80 @@ +* Modern reset: https://piccalil.li/blog/a-modern-css-reset/ */ + +/* Box sizing rules */ +*, +*::before, +*::after { + box-sizing: border-box; +} + +/* Remove default margin */ +body, +h1, +h2, +h3, +h4, +p, +figure, +blockquote, +dl, +dd { + margin: 0; +} + +/* Remove list styles on ul, ol elements with a list role, which suggests default styling will be removed */ +[role='list'] +{ + list-style: none; + padding: 0; + margin: 0; +} + +/* Set core root defaults */ +html:focus-within { + scroll-behavior: smooth; +} + +/* Set core body defaults */ +body { + min-height: 100vh; + min-height: 100dvh; /* safari-specific */ + text-rendering: optimizeSpeed; + line-height: 1.75; +} + +/* A elements that don't have a class get default styles */ +a:not([class]) { + text-decoration-skip-ink: auto; +} + +/* Make images easier to work with */ +img, +picture { + max-width: 100%; + display: block; + height: auto; +} + +/* Inherit fonts for inputs and buttons */ +input, +button, +textarea, +select { + font: inherit; +} + +/* Remove all animations, transitions and smooth scroll for people that prefer not to see them */ +@media (prefers-reduced-motion: reduce) { + html:focus-within { + scroll-behavior: auto; + } + + *, + *::before, + *::after { + animation-duration: 0.01ms !important; + animation-iteration-count: 1 !important; + transition-duration: 0.01ms !important; + scroll-behavior: auto !important; + } +} \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..a55ef35 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,7 @@ + + + {{ template "head.html" . }} + + {{ block "content" . }}{{ end }} + + \ No newline at end of file diff --git a/templates/head.html b/templates/head.html new file mode 100644 index 0000000..01e4526 --- /dev/null +++ b/templates/head.html @@ -0,0 +1,5 @@ + + Oopsie + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index fdaa5de..fc387b1 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,9 +1,5 @@ - - - - Oopsie - - -

Oopsie uptime monitoring

- - \ No newline at end of file +{{ template "base.html" . }} + +{{ define "content" }} +

Oopsie uptime monitoring

+{{ end }} \ No newline at end of file