From ff21a3eb02b9c9baaa07f9537f0027dafdcbdd53 Mon Sep 17 00:00:00 2001 From: rachel-mp4 Date: Mon, 6 Oct 2025 13:48:07 -0400 Subject: [PATCH] upgrade to lrcd v0.1.0, render message count --- go.mod | 7 ++++--- go.sum | 14 ++++++++------ main.go | 18 ++++++++++++++---- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 3d62b76..7aad2da 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,10 @@ module tangled.org/moth11.net/lrcdaemon go 1.24.2 +require github.com/rachel-mp4/lrcd v0.1.0 + require ( github.com/gorilla/websocket v1.5.3 // indirect - github.com/rachel-mp4/lrcd v0.0.1 // indirect - github.com/rachel-mp4/lrcproto v0.0.0-20250905151943-8e3a1989ea5a // indirect - google.golang.org/protobuf v1.36.6 // indirect + github.com/rachel-mp4/lrcproto v1.2.0 // indirect + google.golang.org/protobuf v1.36.10 // indirect ) diff --git a/go.sum b/go.sum index 190bbad..ced7c2b 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,10 @@ +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= -github.com/rachel-mp4/lrcd v0.0.1 h1:9d5if0HJ/+TLKdupd7Zu9dISA5fF3QRtI/yr+gYVqXM= -github.com/rachel-mp4/lrcd v0.0.1/go.mod h1:aWUVglSyrLf2RGpQdqTucX498b434yWBFJkD6Yzr0OE= -github.com/rachel-mp4/lrcproto v0.0.0-20250905151943-8e3a1989ea5a h1:W3zPeGz/jHYyWj8ZfsuA9yigPMNlA/h7Fu+SQKgiBmQ= -github.com/rachel-mp4/lrcproto v0.0.0-20250905151943-8e3a1989ea5a/go.mod h1:hQzO36tQELGbkmRnUtKeM6NMU34t79ZcTlhM+MO7pHw= -google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY= -google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY= +github.com/rachel-mp4/lrcd v0.1.0 h1:iW3ux8otqo+9tYpP2Bsxu5VgbhP324Mjy0vOBf3M39Q= +github.com/rachel-mp4/lrcd v0.1.0/go.mod h1:CyBPWmy94oQ0sTOj24VJjxWoZ9zSnqCKQ/qlR+vhtVE= +github.com/rachel-mp4/lrcproto v1.2.0 h1:nZI80WQKO6yKgX0O5H6OO1cM/tiJqugs0p52KCoIDOw= +github.com/rachel-mp4/lrcproto v1.2.0/go.mod h1:hQzO36tQELGbkmRnUtKeM6NMU34t79ZcTlhM+MO7pHw= +google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE= +google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= diff --git a/main.go b/main.go index 70b8bad..d053b7a 100644 --- a/main.go +++ b/main.go @@ -9,7 +9,10 @@ import ( ) func main() { - server, err := lrcd.NewServer(lrcd.WithLogging(os.Stdout, false), lrcd.WithWelcome("welcome to the beginning of the rest of your life")) + messages := 0 + pubchan := make(chan lrcd.PubEvent) + go incrementMessageCount(pubchan, &messages) + server, err := lrcd.NewServer(lrcd.WithLogging(os.Stdout, false), lrcd.WithWelcome("welcome to the beginning of the rest of your life"), lrcd.WithPubChannel(pubchan)) if err != nil { panic(err) } @@ -19,10 +22,16 @@ func main() { } fmt.Println("serving lrcd on localhost:8080") now := time.Now() - http.ListenAndServe(":8080", guessIfUpgradeFail(server, now)) + http.ListenAndServe(":8080", guessIfUpgradeFail(server, now, &messages)) } -func guessIfUpgradeFail(server *lrcd.Server, now time.Time) http.HandlerFunc { +func incrementMessageCount(pubchan chan lrcd.PubEvent, messages *int) { + for range pubchan { + *messages = *messages + 1 + } +} + +func guessIfUpgradeFail(server *lrcd.Server, now time.Time, messages *int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("Upgrade") != "websocket" { fmt.Fprintf(w, `~ live ~~ hi! i am an lrc server! @@ -33,7 +42,8 @@ i think you should connect to me with your lrc client (e.g. ttyxcvr) it'll be more fun for us both! (tangled.org/moth11.net/ttyxcvr) currently connected: %d - live since: %s`, server.Connected(), now.Format(time.RubyDate)) + live since: %s + message count: %d`, server.Connected(), now.Format(time.RubyDate), *messages) } else { server.WSHandler()(w, r) } -- 2.51.2