From 94188084e1d4bfa2eb8e194b62eba9c140470260 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Jul 2023 11:12:49 -0700 Subject: [PATCH 1/5] Rename project --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index be80b31..0eb2ffd 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module ElnuDev/go-project +module ElnuDev/shiritori-go go 1.20 From a43e440022c6b6dff1b1aa85da57029eff2cafc7 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Jul 2023 11:19:58 -0700 Subject: [PATCH 2/5] Add Go language server --- shell.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/shell.nix b/shell.nix index 3bb54a7..d2a7dc7 100644 --- a/shell.nix +++ b/shell.nix @@ -3,5 +3,6 @@ pkgs.mkShell { buildInputs = with pkgs; [ go + gopls ]; } From 643f56f3c82d981130e8dbcbd17a401e395eb022 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Jul 2023 18:14:21 -0700 Subject: [PATCH 3/5] Add Go binary directory to PATH --- shell.nix | 3 +++ 1 file changed, 3 insertions(+) diff --git a/shell.nix b/shell.nix index d2a7dc7..6688eae 100644 --- a/shell.nix +++ b/shell.nix @@ -5,4 +5,7 @@ pkgs.mkShell { go gopls ]; + shellHook = '' + export PATH="$HOME/go/bin:$PATH" + ''; } From a48a97a7f702dedd4e365b515a5e39b8b7456529 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Jul 2023 18:16:15 -0700 Subject: [PATCH 4/5] Automatically install templ utilities --- shell.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/shell.nix b/shell.nix index 6688eae..27c057f 100644 --- a/shell.nix +++ b/shell.nix @@ -7,5 +7,6 @@ pkgs.mkShell { ]; shellHook = '' export PATH="$HOME/go/bin:$PATH" + go install github.com/a-h/templ/cmd/templ@latest ''; } From ed11a84dca08ec3b0b114142e66811545e9c2257 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 15 Jul 2023 20:00:52 -0700 Subject: [PATCH 5/5] Create button demo --- click.templ | 15 +++++++++ click_templ.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ main.go | 24 ++++++++++++-- static/index.html | 12 +++++++ 6 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 click.templ create mode 100644 click_templ.go create mode 100644 go.sum create mode 100644 static/index.html diff --git a/click.templ b/click.templ new file mode 100644 index 0000000..2c5e845 --- /dev/null +++ b/click.templ @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..8a11cc0 --- /dev/null +++ b/click_templ.go @@ -0,0 +1,80 @@ +// 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 0eb2ffd..24acc91 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module ElnuDev/shiritori-go go 1.20 + +require github.com/a-h/templ v0.2.304 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5599da4 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +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 a3dd973..b75d912 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,27 @@ package main -import "fmt" +import ( + "context" + "net/http" + + "github.com/a-h/templ" +) + +var clicks uint = 0 + +func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + template().Render(context.Background(), w) + handler() + } +} func main() { - fmt.Println("Hello, World!") + http.Handle("/", http.FileServer(http.Dir("static"))) + http.HandleFunc("/api/click", generateHandler( + func() templ.Component { return Click(clicks) }, + func() { clicks++ }, + )) + http.ListenAndServe(":3333", nil) } diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..a59f0cc --- /dev/null +++ b/static/index.html @@ -0,0 +1,12 @@ + + + + + + Shiritori + + + + + + \ No newline at end of file