Initial commit

This commit is contained in:
2024-04-08 00:12:48 +02:00
commit 01e96380a4
6 changed files with 67 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.exe

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module stevenlr.com/timer
go 1.22.2
require github.com/mattn/go-sqlite3 v1.14.22 // indirect

2
go.sum Normal file
View File

@ -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=

8
static/style.css Normal file
View File

@ -0,0 +1,8 @@
body {
font-size: 16px;
}
* {
font-family: sans-serif;
}

11
template/main.tpl.html Normal file
View File

@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>Cool timer app</title>
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
Hello
{{ . }}
</body>
</html>

40
timer.go Normal file
View File

@ -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(`<h1><a href="/timer/test">Hello, world!</a></h1>`))
}
func (server *myServer) HandleTimer(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<p>Timer %s</p>", 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)
}