Basic site running

This commit is contained in:
Louis Hollingworth 2024-08-05 20:26:04 +01:00
parent 70fca57571
commit 2e24996510
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
6 changed files with 161 additions and 2 deletions

View file

@ -1,2 +1,8 @@
# Cal # Cal
An open-source shared calendar An open-source shared calendar
## Acknowledgements
- [SmolCSS](https://smolcss.dev/) for the stylings
- [git-pr](https://pr.pico.sh/) for an example of how to implement http
functions with Go 1.22.5

59
main.go
View file

@ -1,7 +1,62 @@
package main package main
import "fmt" import (
"embed"
"fmt"
"html/template"
"io"
"io/fs"
"mime"
"net/http"
"path/filepath"
)
//go:embed static
var staticFiles embed.FS
func main() { func main() {
fmt.Println("Saluton, mondo") staticFS := fs.FS(staticFiles)
http.HandleFunc("GET /", index)
http.Handle("GET /static/{file}", http.FileServer(http.FS(staticFS)))
if err := http.ListenAndServe(":4321", nil); err != nil {
fmt.Printf("Unable to serve: %v\n", err)
}
}
func serveFile(system fs.FS) func(w http.ResponseWriter, r http.Request) {
return func(w http.ResponseWriter, r http.Request) {
file := r.PathValue("file")
reader, err := system.Open(file)
if err != nil {
http.Error(w, "File not found", 404)
return
}
defer reader.Close()
cont, err := io.ReadAll(reader)
handleHTTPError("Error reading file", err, w)
contType := mime.TypeByExtension(filepath.Ext(file))
w.Header().Add("Content-Type", contType)
_, err = w.Write(cont)
handleHTTPError("Error writing HTTP resp", err, w)
}
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf8")
tmpl, err := template.ParseFiles("./tmpl/index.html", "./tmpl/base.html")
handleHTTPError("Error parsing template", err, w)
err = tmpl.Execute(w, nil)
handleHTTPError("Error executing on template", err, w)
}
func handleHTTPError(msg string, err error, w http.ResponseWriter) {
if err != nil {
fmt.Printf("%v: %v", msg, err)
http.Error(w, "Internal server error", 500)
return
}
} }

57
static/smol.css Normal file
View file

@ -0,0 +1,57 @@
* {
box-sizing: border-box;
margin: 0;
}
body {
background-color: var(--bg-colour);
color: var(--text-colour) !important;
display: flex;
flex-direction: column;
min-height: 100vh;
padding: 5vh clamp(1rem, 5vw, 3rem) 1rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
color: #222;
}
body > * {
--layout-spacing: max(8vh, 3rem);
--max-width: 70ch;
width: min(100%, var(--max-width));
margin-left: auto;
margin-right: auto;
}
main {
margin-top: var(--layout-spacing);
}
footer {
margin-top: auto;
padding-top: var(--layout-spacing);
}
footer p {
border-top: 1px solid #ccc;
padding-top: 0.25em;
font-size: 0.9rem;
color: #767676;
}
:is(h1, h2, h3) {
line-height: 1.2;
}
:is(h2, h3):not(:first-child) {
margin-top: 2em;
}
article * + * {
margin-top: 1em;
}
a {
color: navy;
text-underline-offset: 0.15em;
}

12
static/vars.css Normal file
View file

@ -0,0 +1,12 @@
:root {
--bg-colour: #ffffff;
--text-colour: #282a36;
}
@media (prefers-color-scheme: dark) {
:root {
--bg-colour: #282a36;
--text-colour: #ffffff;
}
}

16
tmpl/base.html Normal file
View file

@ -0,0 +1,16 @@
{{define "base"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{template "title" .}}</title>
{{template "meta" .}}
<link rel="stylesheet" href="/static/vars.css">
<link rel="stylesheet" href="/static/smol.css">
</head>
<body>
{{template "body" .}}
</body>
</html>
{{end}}

13
tmpl/index.html Normal file
View file

@ -0,0 +1,13 @@
{{template "base" .}}
{{define "title"}}Calendar{{end}}
{{define "meta"}} {{end}}
{{define "body"}}
<header class="group">
<h1 class="text-2xl">Cal</h1>
</header>
{{end}}