Some work

This commit is contained in:
2024-04-08 23:23:23 +02:00
parent 01e96380a4
commit c60dd3357f
15 changed files with 407 additions and 23 deletions

6
go.mod
View File

@ -2,4 +2,8 @@ module stevenlr.com/timer
go 1.22.2 go 1.22.2
require github.com/mattn/go-sqlite3 v1.14.22 // indirect require (
github.com/a-h/templ v0.2.648 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-sqlite3 v1.14.22 // indirect
)

4
go.sum
View File

@ -1,2 +1,6 @@
github.com/a-h/templ v0.2.648 h1:A1ggHGIE7AONOHrFaDTM8SrqgqHL6fWgWCijQ21Zy9I=
github.com/a-h/templ v0.2.648/go.mod h1:SA7mtYwVEajbIXFRh3vKdYm/4FYyLQAtPH1+KxzGPA8=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= 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= github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

7
model/timer.go Normal file
View File

@ -0,0 +1,7 @@
package model
type Timer struct {
Id UUID
Name string
}

28
model/uuid.go Normal file
View File

@ -0,0 +1,28 @@
package model
import (
sqldriver "database/sql/driver"
"github.com/google/uuid"
)
type UUID struct {
payload uuid.UUID
}
func NewUUID() UUID {
id, _ := uuid.NewRandom()
return UUID { payload: id }
}
func (self UUID) Value() (sqldriver.Value, error) {
return self.payload.MarshalBinary()
}
func (self *UUID) Scan(value any) error {
return self.payload.Scan(value)
}
func (self UUID) String() string {
return self.payload.String()
}

1
static/htmx.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

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

103
timer.go
View File

@ -2,38 +2,119 @@ package main
import ( import (
"log" "log"
"fmt"
"net/http" "net/http"
"html/template" "database/sql"
"context"
_ "github.com/mattn/go-sqlite3"
"stevenlr.com/timer/view"
"stevenlr.com/timer/model"
) )
func initializeDatabase(db *sql.DB) error {
tx, err := db.Begin()
if err != nil { return err }
defer tx.Rollback()
_, err = tx.Exec(`
CREATE TABLE Timer (
id BLOB NOT NULL UNIQUE,
name TEXT NOT NULL,
PRIMARY KEY (id)
)
`)
if err != nil { return err }
id := model.NewUUID()
_, err = tx.Exec(`
INSERT INTO Timer VALUES ($1, $2)`, id, "My timer");
if err != nil { return err }
id = model.NewUUID()
_, err = tx.Exec(`
INSERT INTO Timer VALUES ($1, $2)`, id, "My timer 2");
if err != nil { return err }
return tx.Commit()
}
func queryAllTimers(db *sql.DB) []model.Timer {
rows, err := db.Query("SELECT id, name FROM Timer")
if err != nil { log.Fatalln(err) }
timers := []model.Timer{}
for rows.Next() {
var t model.Timer
if err := rows.Scan(&t.Id, &t.Name); err == nil {
timers = append(timers, t)
}
}
return timers
}
func queryTimer(db *sql.DB, idStr string) *model.Timer {
var id model.UUID
if err := id.Scan(idStr); err != nil { return nil }
row := db.QueryRow("SELECT id, name FROM Timer WHERE id=$1", id)
var t model.Timer
if err := row.Scan(&t.Id, &t.Name); err == nil {
return &t
}
return nil
}
type myServer struct { type myServer struct {
mainTemplate *template.Template db *sql.DB
} }
func (server *myServer) HandleMain(w http.ResponseWriter, r *http.Request) { func (server *myServer) handleNotFound(w http.ResponseWriter, _ *http.Request) {
server.mainTemplate.Execute(w, template.HTML(`<h1><a href="/timer/test">Hello, world!</a></h1>`)) w.WriteHeader(http.StatusNotFound)
view.Main(view.Error404()).Render(context.Background(), w)
} }
func (server *myServer) HandleTimer(w http.ResponseWriter, r *http.Request) { func (server *myServer) handleMain(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<p>Timer %s</p>", r.PathValue("timer_id")) if r.URL.Path == "/" {
timers := queryAllTimers(server.db)
view.Main(view.TimersList(timers)).Render(context.Background(), w)
} else {
server.handleNotFound(w, r)
}
}
func (server *myServer) handleTimer(w http.ResponseWriter, r *http.Request) {
timer := queryTimer(server.db, r.PathValue("timerId"))
if timer != nil {
view.Main(view.TimerView(*timer)).Render(context.Background(), w)
} else {
server.handleNotFound(w, r)
}
} }
func main() { func main() {
log.Println("Hello") log.Println("Hello")
tpl, err := template.ParseFiles("template/main.tpl.html") db, err := sql.Open("sqlite3", ":memory:")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer db.Close()
myServer := myServer{ mainTemplate: tpl } if err := initializeDatabase(db); err != nil {
log.Fatalln(err)
}
myServer := myServer{ db: db }
fs := http.FileServer(http.Dir("static/")) fs := http.FileServer(http.Dir("static/"))
http.Handle("/static/", http.StripPrefix("/static/", fs)) http.Handle("/static/", http.StripPrefix("/static/", fs))
http.HandleFunc("/", myServer.HandleMain) http.HandleFunc("/timer/{timerId}", myServer.handleTimer)
http.HandleFunc("/timer/{timer_id}", myServer.HandleTimer) http.HandleFunc("/", myServer.handleMain)
http.ListenAndServe("0.0.0.0:80", nil) http.ListenAndServe("0.0.0.0:80", nil)
} }

6
view/error_404.templ Normal file
View File

@ -0,0 +1,6 @@
package view
templ Error404() {
<h1>Not found</h1>
}

35
view/error_404_templ.go Normal file
View File

@ -0,0 +1,35 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.648
package view
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
func Error404() templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>Not found</h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

15
view/main.templ Normal file
View File

@ -0,0 +1,15 @@
package view
templ Main(contents templ.Component) {
<!DOCTYPE html>
<html>
<head>
<title>Cool timer app</title>
<link rel="stylesheet" href="/static/style.css" />
<script src="/static/htmx.min.js"></script>
</head>
<body hx-boost="true">
@contents
</body>
</html>
}

43
view/main_templ.go Normal file
View File

@ -0,0 +1,43 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.648
package view
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
func Main(contents templ.Component) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<!doctype html><html><head><title>Cool timer app</title><link rel=\"stylesheet\" href=\"/static/style.css\"><script src=\"/static/htmx.min.js\"></script></head><body hx-boost=\"true\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
templ_7745c5c3_Err = contents.Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</body></html>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

10
view/timer.templ Normal file
View File

@ -0,0 +1,10 @@
package view
import (
"stevenlr.com/timer/model"
)
templ TimerView(timer model.Timer) {
<h1>This is timer { timer.Name }</h1>
<a href="/">Back to list</a>
}

52
view/timer_templ.go Normal file
View File

@ -0,0 +1,52 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.648
package view
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
import (
"stevenlr.com/timer/model"
)
func TimerView(timer model.Timer) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>This is timer ")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 string
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(timer.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timer.templ`, Line: 8, Col: 34}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</h1><a href=\"/\">Back to list</a>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}

17
view/timers_list.templ Normal file
View File

@ -0,0 +1,17 @@
package view
import (
"stevenlr.com/timer/model"
"fmt"
)
templ timer(t model.Timer) {
<p><a href={ templ.URL(fmt.Sprintf("/timer/%s", t.Id)) }>{ t.Name }</a></p>
}
templ TimersList(timers []model.Timer) {
<h1>Timers</h1>
for _, t := range timers {
@timer(t)
}
}

92
view/timers_list_templ.go Normal file
View File

@ -0,0 +1,92 @@
// Code generated by templ - DO NOT EDIT.
// templ: version: v0.2.648
package view
//lint:file-ignore SA4006 This context is only used if a nested component is present.
import "github.com/a-h/templ"
import "context"
import "io"
import "bytes"
import (
"fmt"
"stevenlr.com/timer/model"
)
func timer(t model.Timer) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var1 := templ.GetChildren(ctx)
if templ_7745c5c3_Var1 == nil {
templ_7745c5c3_Var1 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<p><a href=\"")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var2 templ.SafeURL = templ.URL(fmt.Sprintf("/timer/%s", t.Id))
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(string(templ_7745c5c3_Var2)))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("\">")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
var templ_7745c5c3_Var3 string
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(t.Name)
if templ_7745c5c3_Err != nil {
return templ.Error{Err: templ_7745c5c3_Err, FileName: `view\timers_list.templ`, Line: 9, Col: 69}
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("</a></p>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}
func TimersList(timers []model.Timer) templ.Component {
return templ.ComponentFunc(func(ctx context.Context, templ_7745c5c3_W io.Writer) (templ_7745c5c3_Err error) {
templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templ_7745c5c3_W.(*bytes.Buffer)
if !templ_7745c5c3_IsBuffer {
templ_7745c5c3_Buffer = templ.GetBuffer()
defer templ.ReleaseBuffer(templ_7745c5c3_Buffer)
}
ctx = templ.InitializeContext(ctx)
templ_7745c5c3_Var4 := templ.GetChildren(ctx)
if templ_7745c5c3_Var4 == nil {
templ_7745c5c3_Var4 = templ.NopComponent
}
ctx = templ.ClearChildren(ctx)
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("<h1>Timers</h1>")
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
for _, t := range timers {
templ_7745c5c3_Err = timer(t).Render(ctx, templ_7745c5c3_Buffer)
if templ_7745c5c3_Err != nil {
return templ_7745c5c3_Err
}
}
if !templ_7745c5c3_IsBuffer {
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteTo(templ_7745c5c3_W)
}
return templ_7745c5c3_Err
})
}