diff --git a/sdk/go/README.md b/sdk/go/README.md index 89b51488..7fdbde54 100644 --- a/sdk/go/README.md +++ b/sdk/go/README.md @@ -1,89 +1,87 @@ # Go SDK for Datastar -[![Go -Reference](https://pkg.go.dev/badge/github.com/starfederation/datastar.svg)](https://pkg.go.dev/github.com/starfederation/datastar) +[![Go Reference](https://pkg.go.dev/badge/github.com/starfederation/datastar.svg)](https://pkg.go.dev/github.com/starfederation/datastar) Implements the [SDK spec](../README.md) and exposes an abstract ServerSentEventGenerator struct that can be used to implement runtime specific classes. -Usage is straightforward: - ```go package main import ( -"crypto/rand" -"encoding/hex" -"fmt" -"log/slog" -"net/http" -"os" -"time" + "crypto/rand" + "encoding/hex" + "fmt" + "log/slog" + "net/http" + "os" + "time" -datastar "github.com/starfederation/datastar/sdk/go" + datastar "github.com/starfederation/datastar/sdk/go" ) const port = 9001 func main() { -logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) -mux := http.NewServeMux() + logger := slog.New(slog.NewJSONHandler(os.Stdout, nil)) + mux := http.NewServeMux() -cdn := "https://cdn.jsdelivr.net/gh/starfederation/datastar@develop/bundles/datastar.js" -style := "display:flex;flex-direction:column;background-color:oklch(25.3267% 0.015896 -252.417568);height:100vh;justify-content:center;align-items:center;font-family:ui-sans-serif, system-ui, sans-serif, -'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';" + cdn := "https://cdn.jsdelivr.net/gh/starfederation/datastar@develop/bundles/datastar.js" + style := "display:flex;flex-direction:column;background-color:oklch(25.3267% 0.015896 252.417568);height:100vh;justify-content:center;align-items:center;font-family:ui-sans-serif, system-ui, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji';" -page := []byte(fmt.Sprintf(` - - + page := []byte(fmt.Sprintf(` + + - - - - + + + + - - - + + + - -`, cdn, style, datastar.GetSSE("/stream"))) + + `, cdn, style, datastar.GetSSE("/stream"))) -mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { -w.Write(page) -}) + mux.HandleFunc("GET /", func(w http.ResponseWriter, r *http.Request) { + w.Write(page) + }) -mux.HandleFunc("GET /stream", func(w http.ResponseWriter, r *http.Request) { -ticker := time.NewTicker(100 * time.Millisecond) -defer ticker.Stop() + mux.HandleFunc("GET /stream", func(w http.ResponseWriter, r *http.Request) { + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() -sse := datastar.NewSSE(w, r) + sse := datastar.NewSSE(w, r) + for { + select { + case <-r.Context().Done(): + logger.Debug("Client connection closed") + return + case <-ticker.C: + bytes := make([]byte, 3) -for { -select { + if _, err := rand.Read(bytes); err != nil { + logger.Error("Error generating random bytes: ", slog.String("error", err.Error())) + return + } + hexString := hex.EncodeToString(bytes) + frag := fmt.Sprintf(`%s`, hexString, hexString, hexString) -case <-r.Context().Done(): logger.Debug("Client connection closed") return case <-ticker.C: bytes :=make([]byte, 3) _, - err :=rand.Read(bytes) if err !=nil { logger.Error("Error generating random bytes: ", slog.String(" error", - err.Error())) return } hexString :=hex.EncodeToString(bytes) frag :=fmt.Sprintf(`%s`, hexString, hexString, - hexString) - - sse.MergeFragments(frag) - } - } + sse.MergeFragments(frag) + } + } }) logger.Info(fmt.Sprintf("Server starting at 0.0.0.0:%d", port)) if err := http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", port), mux); err != nil { - logger.Error("Error starting server:", slog.String("error", err.Error())) + logger.Error("Error starting server:", slog.String("error", err.Error())) } +} +``` - } - ``` +## Examples - ## Examples - - The [Datastar website](https://data-star.dev) acts as a [set of - examples](https://github.com/starfederation/datastar/tree/develop/site) for how to use the SDK. \ No newline at end of file +The [Datastar website](https://data-star.dev) acts as a [set of examples](https://github.com/starfederation/datastar/tree/develop/site) for how to use the SDK. diff --git a/sdk/go/execute.go b/sdk/go/execute.go index 163d02e4..0180180a 100644 --- a/sdk/go/execute.go +++ b/sdk/go/execute.go @@ -7,33 +7,48 @@ import ( "time" ) -type ExecuteScriptOptions struct { +// executeScriptOptions hold script options that will be translated to [SSEEventOptions]. +type executeScriptOptions struct { EventID string RetryDuration time.Duration Attributes []string AutoRemove *bool } -type ExecuteScriptOption func(*ExecuteScriptOptions) +// ExecuteScriptOption configures script execution event that will be sent to the client. +type ExecuteScriptOption func(*executeScriptOptions) +// WithExecuteScriptEventID configures an optional event ID for the script execution event. +// The client message field [lastEventId] will be set to this value. +// If the next event does not have an event ID, the last used event ID will remain. +// +// [lastEventId]: https://developer.mozilla.org/en-US/docs/Web/API/MessageEvent/lastEventId func WithExecuteScriptEventID(id string) ExecuteScriptOption { - return func(o *ExecuteScriptOptions) { + return func(o *executeScriptOptions) { o.EventID = id } } +// WithExecuteScriptRetryDuration overrides the [DefaultRetryDuration] for this script +// execution only. func WithExecuteScriptRetryDuration(retryDuration time.Duration) ExecuteScriptOption { - return func(o *ExecuteScriptOptions) { + return func(o *executeScriptOptions) { o.RetryDuration = retryDuration } } +// WithExecuteScriptAttributes overrides the default script attribute +// value `type module`, which renders as `