From 20db5cdaadee62e687faea1896dd0bada92b4629 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 16 Jul 2023 12:27:21 -0700 Subject: [PATCH 1/6] Use log.Fatal --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index b75d912..90eb275 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "log" "net/http" "github.com/a-h/templ" @@ -23,5 +24,5 @@ func main() { func() templ.Component { return Click(clicks) }, func() { clicks++ }, )) - http.ListenAndServe(":3333", nil) + log.Fatal(http.ListenAndServe(":3333", nil)) } From 3fc30c0d3abbcd3c190a0a42e27e20e362c8226e Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sun, 16 Jul 2023 13:12:45 -0700 Subject: [PATCH 2/6] Improve demo using html/templates instead of templ --- click.templ | 15 --------- click_templ.go | 80 -------------------------------------------- go.mod | 4 +-- go.sum | 2 -- main.go | 30 +++++++++++------ shell.nix | 1 - static/index.html | 7 +++- templates/click.html | 1 + 8 files changed, 27 insertions(+), 113 deletions(-) delete mode 100644 click.templ delete mode 100644 click_templ.go delete mode 100644 go.sum create mode 100644 templates/click.html diff --git a/click.templ b/click.templ deleted file mode 100644 index 2c5e845..0000000 --- a/click.templ +++ /dev/null @@ -1,15 +0,0 @@ -package main - -import "fmt" - -func plural(singular, plural string, count uint) string { - if count == 1 { - return singular - } else { - return plural - } -} - -templ Click(clicks uint) { -
The button has been clicked { fmt.Sprintf("%d", clicks) }{ " " }{ plural("time", "times", clicks) }.
-} diff --git a/click_templ.go b/click_templ.go deleted file mode 100644 index 8a11cc0..0000000 --- a/click_templ.go +++ /dev/null @@ -1,80 +0,0 @@ -// Code generated by templ@v0.2.304 DO NOT EDIT. - -package main - -//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" - -// GoExpression -import "fmt" - -func plural(singular, plural string, count uint) string { - if count == 1 { - return singular - } else { - return plural - } -} - -func Click(clicks uint) templ.Component { - return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { - templBuffer, templIsBuffer := w.(*bytes.Buffer) - if !templIsBuffer { - templBuffer = templ.GetBuffer() - defer templ.ReleaseBuffer(templBuffer) - } - ctx = templ.InitializeContext(ctx) - var_1 := templ.GetChildren(ctx) - if var_1 == nil { - var_1 = templ.NopComponent - } - ctx = templ.ClearChildren(ctx) - // Element (standard) - _, err = templBuffer.WriteString("
") - if err != nil { - return err - } - // Text - var_2 := `The button has been clicked ` - _, err = templBuffer.WriteString(var_2) - if err != nil { - return err - } - // StringExpression - var var_3 string = fmt.Sprintf("%d", clicks) - _, err = templBuffer.WriteString(templ.EscapeString(var_3)) - if err != nil { - return err - } - // StringExpression - var var_4 string = " " - _, err = templBuffer.WriteString(templ.EscapeString(var_4)) - if err != nil { - return err - } - // StringExpression - var var_5 string = plural("time", "times", clicks) - _, err = templBuffer.WriteString(templ.EscapeString(var_5)) - if err != nil { - return err - } - // Text - var_6 := `.` - _, err = templBuffer.WriteString(var_6) - if err != nil { - return err - } - _, err = templBuffer.WriteString("
") - if err != nil { - return err - } - if !templIsBuffer { - _, err = io.Copy(w, templBuffer) - } - return err - }) -} diff --git a/go.mod b/go.mod index 24acc91..bea319a 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,3 @@ module ElnuDev/shiritori-go -go 1.20 - -require github.com/a-h/templ v0.2.304 // indirect +go 1.20 \ No newline at end of file diff --git a/go.sum b/go.sum deleted file mode 100644 index 5599da4..0000000 --- a/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/a-h/templ v0.2.304 h1:vIgCNazkW6NiYifFIGYNRfBkoBzOMZMO1NibIayzihE= -github.com/a-h/templ v0.2.304/go.mod h1:3oc37WS5rpDvFGi6yeknvTKt50xCu67ywQsM43Wr4PU= diff --git a/main.go b/main.go index 90eb275..66d57fe 100644 --- a/main.go +++ b/main.go @@ -1,28 +1,36 @@ package main import ( - "context" + "fmt" + "html/template" "log" "net/http" - - "github.com/a-h/templ" ) -var clicks uint = 0 +type handler = func(http.ResponseWriter, *http.Request) -func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) { +func generateHandler(file string, handler func(), data func() any) handler { + tmpl := template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file))) return func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - template().Render(context.Background(), w) handler() + w.Header().Set("Content-Type", "text/html; charset=utf-8") + tmpl.Execute(w, data()) } } +func generateClick() handler { + var clicks uint = 0 + return generateHandler( + "click.html", + func() { clicks++ }, + func() any { return clicks }, + ) +} + func main() { http.Handle("/", http.FileServer(http.Dir("static"))) - http.HandleFunc("/api/click", generateHandler( - func() templ.Component { return Click(clicks) }, - func() { clicks++ }, - )) + http.HandleFunc("/api/click1", generateClick()) + http.HandleFunc("/api/click2", generateClick()) + http.HandleFunc("/api/click3", generateClick()) log.Fatal(http.ListenAndServe(":3333", nil)) } diff --git a/shell.nix b/shell.nix index 27c057f..6688eae 100644 --- a/shell.nix +++ b/shell.nix @@ -7,6 +7,5 @@ pkgs.mkShell { ]; shellHook = '' export PATH="$HOME/go/bin:$PATH" - go install github.com/a-h/templ/cmd/templ@latest ''; } diff --git a/static/index.html b/static/index.html index a59f0cc..e223af2 100644 --- a/static/index.html +++ b/static/index.html @@ -7,6 +7,11 @@ - + +
+ +
+ +
\ No newline at end of file diff --git a/templates/click.html b/templates/click.html new file mode 100644 index 0000000..f997858 --- /dev/null +++ b/templates/click.html @@ -0,0 +1 @@ +The button has been clicked {{ . }} time{{ if ne . 1 }}s{{ end }}! \ No newline at end of file From 3a8179a99aaa8c9c7bf31405ff11eb63407ebcfd Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 17 Jul 2023 09:46:32 -0700 Subject: [PATCH 3/6] Implement Server-Sent Events --- main.go | 29 +++++++++++++++++++++++++++++ static/index.html | 3 +++ 2 files changed, 32 insertions(+) diff --git a/main.go b/main.go index 66d57fe..7420502 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "html/template" "log" "net/http" + "time" ) type handler = func(http.ResponseWriter, *http.Request) @@ -27,10 +28,38 @@ func generateClick() handler { ) } +func generateSseHandler(handler func(http.ResponseWriter, *http.Request)) handler { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache") + w.Header().Set("Connection", "keep-alive") + w.WriteHeader(http.StatusOK) + handler(w, r) + } +} + +func generateCounter() handler { + return generateSseHandler(func(w http.ResponseWriter, r *http.Request) { + tick := time.Tick(500 * time.Millisecond) + ctx := r.Context() + outer: + for i := 0; ; i++ { + select { + case <-ctx.Done(): + break outer + case <-tick: + fmt.Fprintf(w, "event: count\ndata: %d\n\n", i) + w.(http.Flusher).Flush() + } + } + }) +} + func main() { http.Handle("/", http.FileServer(http.Dir("static"))) http.HandleFunc("/api/click1", generateClick()) http.HandleFunc("/api/click2", generateClick()) http.HandleFunc("/api/click3", generateClick()) + http.HandleFunc("/api/counter", generateCounter()) log.Fatal(http.ListenAndServe(":3333", nil)) } diff --git a/static/index.html b/static/index.html index e223af2..d9c8890 100644 --- a/static/index.html +++ b/static/index.html @@ -7,6 +7,9 @@ +
+
+

From b5ca22cd7d18fcbfe98d21024b0cccd0c7d9767d Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 17 Jul 2023 10:11:26 -0700 Subject: [PATCH 4/6] Implent SSE feed --- main.go | 2 +- static/index.html | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 7420502..780c178 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,7 @@ func generateCounter() handler { case <-ctx.Done(): break outer case <-tick: - fmt.Fprintf(w, "event: count\ndata: %d\n\n", i) + fmt.Fprintf(w, "event: count\ndata:
%d
\n\n", i) w.(http.Flusher).Flush() } } diff --git a/static/index.html b/static/index.html index d9c8890..5625853 100644 --- a/static/index.html +++ b/static/index.html @@ -7,8 +7,7 @@ -
-
+

From 5027e0251d862cecbf1d61f27cb20050839e0a0a Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 17 Jul 2023 10:13:18 -0700 Subject: [PATCH 5/6] Remove demo button clickers --- main.go | 12 ------------ static/index.html | 9 +-------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/main.go b/main.go index 780c178..3f39f9e 100644 --- a/main.go +++ b/main.go @@ -19,15 +19,6 @@ func generateHandler(file string, handler func(), data func() any) handler { } } -func generateClick() handler { - var clicks uint = 0 - return generateHandler( - "click.html", - func() { clicks++ }, - func() any { return clicks }, - ) -} - func generateSseHandler(handler func(http.ResponseWriter, *http.Request)) handler { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") @@ -57,9 +48,6 @@ func generateCounter() handler { func main() { http.Handle("/", http.FileServer(http.Dir("static"))) - http.HandleFunc("/api/click1", generateClick()) - http.HandleFunc("/api/click2", generateClick()) - http.HandleFunc("/api/click3", generateClick()) http.HandleFunc("/api/counter", generateCounter()) log.Fatal(http.ListenAndServe(":3333", nil)) } diff --git a/static/index.html b/static/index.html index 5625853..d7952c6 100644 --- a/static/index.html +++ b/static/index.html @@ -7,13 +7,6 @@ -
-
- -
- -
- -
+
\ No newline at end of file From fcc9d9f5efbfbe0ae3da7ae37c290b813ee3dd78 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Mon, 17 Jul 2023 11:07:51 -0700 Subject: [PATCH 6/6] Remove old click template --- templates/click.html | 1 - 1 file changed, 1 deletion(-) delete mode 100644 templates/click.html diff --git a/templates/click.html b/templates/click.html deleted file mode 100644 index f997858..0000000 --- a/templates/click.html +++ /dev/null @@ -1 +0,0 @@ -The button has been clicked {{ . }} time{{ if ne . 1 }}s{{ end }}! \ No newline at end of file