summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-08 00:12:48 +0200
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-04-08 00:12:48 +0200
commit01e96380a4e48b5d338c71fad690382124195b17 (patch)
treedc40f7d639fda7c77439bd935d497503d241cfe6
Initial commit
-rw-r--r--.gitignore1
-rw-r--r--go.mod5
-rw-r--r--go.sum2
-rw-r--r--static/style.css8
-rw-r--r--template/main.tpl.html11
-rw-r--r--timer.go40
6 files changed, 67 insertions, 0 deletions
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 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Cool timer app</title>
+ <link rel="stylesheet" href="/static/style.css" />
+</head>
+<body>
+ Hello
+ {{ . }}
+</body>
+</html>
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(`<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)
+}
+