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 bea319a..24acc91 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,5 @@
module ElnuDev/shiritori-go
-go 1.20
\ No newline at end of file
+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 3f39f9e..b75d912 100644
--- a/main.go
+++ b/main.go
@@ -1,53 +1,27 @@
package main
import (
- "fmt"
- "html/template"
- "log"
+ "context"
"net/http"
- "time"
+
+ "github.com/a-h/templ"
)
-type handler = func(http.ResponseWriter, *http.Request)
+var clicks uint = 0
-func generateHandler(file string, handler func(), data func() any) handler {
- tmpl := template.Must(template.ParseFiles(fmt.Sprintf("templates/%s", file)))
+func generateHandler(template func() templ.Component, handler func()) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
- handler()
w.Header().Set("Content-Type", "text/html; charset=utf-8")
- tmpl.Execute(w, data())
+ template().Render(context.Background(), w)
+ 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/counter", generateCounter())
- log.Fatal(http.ListenAndServe(":3333", nil))
+ http.HandleFunc("/api/click", generateHandler(
+ func() templ.Component { return Click(clicks) },
+ func() { clicks++ },
+ ))
+ http.ListenAndServe(":3333", nil)
}
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
'';
}
diff --git a/static/index.html b/static/index.html
index d7952c6..a59f0cc 100644
--- a/static/index.html
+++ b/static/index.html
@@ -7,6 +7,6 @@
-
+