Inherited templates, staticfiles
Build and copy to prod / build-and-copy (push) Successful in 43s Details

This commit is contained in:
Lewis Dale 2024-05-15 07:56:14 +01:00
parent 27f6d749d3
commit 701544df9c
5 changed files with 106 additions and 13 deletions

13
main.go
View File

@ -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)
}

80
static/reset.css Normal file
View File

@ -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;
}
}

7
templates/base.html Normal file
View File

@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
{{ template "head.html" . }}
<body>
{{ block "content" . }}{{ end }}
</body>
</html>

5
templates/head.html Normal file
View File

@ -0,0 +1,5 @@
<head>
<title>Oopsie</title>
<link rel="stylesheet" href="/static/reset.css" />
</head>

View File

@ -1,9 +1,5 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Oopsie</title>
</head>
<body>
<h1>Oopsie uptime monitoring</h1>
</body>
</html>
{{ template "base.html" . }}
{{ define "content" }}
<h1>Oopsie uptime monitoring</h1>
{{ end }}