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 b75d912..3f39f9e 100644 --- a/main.go +++ b/main.go @@ -1,27 +1,53 @@ package main import ( - "context" + "fmt" + "html/template" + "log" "net/http" - - "github.com/a-h/templ" + "time" ) -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 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/click", generateHandler( - func() templ.Component { return Click(clicks) }, - func() { clicks++ }, - )) - http.ListenAndServe(":3333", nil) + http.HandleFunc("/api/counter", generateCounter()) + 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..d7952c6 100644 --- a/static/index.html +++ b/static/index.html @@ -7,6 +7,6 @@ - +
\ No newline at end of file