From 01e96380a4e48b5d338c71fad690382124195b17 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 8 Apr 2024 00:12:48 +0200 Subject: Initial commit --- .gitignore | 1 + go.mod | 5 +++++ go.sum | 2 ++ static/style.css | 8 ++++++++ template/main.tpl.html | 11 +++++++++++ timer.go | 40 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 .gitignore create mode 100644 go.mod create mode 100644 go.sum create mode 100644 static/style.css create mode 100644 template/main.tpl.html create mode 100644 timer.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1feae78 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.exe diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..13fb741 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module stevenlr.com/timer + +go 1.22.2 + +require github.com/mattn/go-sqlite3 v1.14.22 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e8d092a --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= +github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y= diff --git a/static/style.css b/static/style.css new file mode 100644 index 0000000..63193d7 --- /dev/null +++ b/static/style.css @@ -0,0 +1,8 @@ +body { + font-size: 16px; +} + +* { + font-family: sans-serif; +} + diff --git a/template/main.tpl.html b/template/main.tpl.html new file mode 100644 index 0000000..020a259 --- /dev/null +++ b/template/main.tpl.html @@ -0,0 +1,11 @@ + + + + Cool timer app + + + + Hello + {{ . }} + + diff --git a/timer.go b/timer.go new file mode 100644 index 0000000..fe52bb0 --- /dev/null +++ b/timer.go @@ -0,0 +1,40 @@ +package main + +import ( + "log" + "fmt" + "net/http" + "html/template" +) + +type myServer struct { + mainTemplate *template.Template +} + +func (server *myServer) HandleMain(w http.ResponseWriter, r *http.Request) { + server.mainTemplate.Execute(w, template.HTML(`

Hello, world!

`)) +} + +func (server *myServer) HandleTimer(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "

Timer %s

", r.PathValue("timer_id")) +} + +func main() { + log.Println("Hello") + + tpl, err := template.ParseFiles("template/main.tpl.html") + if err != nil { + log.Fatalln(err) + } + + myServer := myServer{ mainTemplate: tpl } + + fs := http.FileServer(http.Dir("static/")) + http.Handle("/static/", http.StripPrefix("/static/", fs)) + + http.HandleFunc("/", myServer.HandleMain) + http.HandleFunc("/timer/{timer_id}", myServer.HandleTimer) + + http.ListenAndServe("0.0.0.0:80", nil) +} + -- cgit