From 7fb91b80ab65a649135f62eb11af2ab4b0582cb2 Mon Sep 17 00:00:00 2001
From: Andres Morey <andresmarcel@gmail.com>
Date: Wed, 11 Sep 2024 18:46:18 +0300
Subject: [PATCH 1/2] improvements to example and tilt resources

---
 example/cmd/app/main.go      |  6 ++--
 example/internal/app/app.go  |  4 +--
 example/templates/index.html | 67 ++++++++++++++++++++++++++----------
 hack/tilt/chaoskube.yaml     |  4 +--
 hack/tilt/example-app.yaml   |  5 +++
 5 files changed, 60 insertions(+), 26 deletions(-)

diff --git a/example/cmd/app/main.go b/example/cmd/app/main.go
index b78503d..0874a97 100644
--- a/example/cmd/app/main.go
+++ b/example/cmd/app/main.go
@@ -35,7 +35,7 @@ func main() {
 	// create app
 	app, err := app.NewApp()
 	if err != nil {
-		log.Fatal(err)
+		log.Fatalf("Error: %v", err)
 	}
 
 	// create server
@@ -51,7 +51,7 @@ func main() {
 	go func() {
 		log.Println("Starting server on :4000")
 		if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
-			log.Fatal(err)
+			log.Fatalf("Error: %v", err)
 		}
 	}()
 
@@ -68,7 +68,7 @@ func main() {
 	done := make(chan struct{})
 	go func() {
 		if err := server.Shutdown(ctx); err != nil {
-			log.Println(err)
+			log.Printf("Error: %v", err)
 		}
 		close(done)
 	}()
diff --git a/example/internal/app/app.go b/example/internal/app/app.go
index e3d4cc3..8edb37c 100644
--- a/example/internal/app/app.go
+++ b/example/internal/app/app.go
@@ -99,7 +99,7 @@ func NewApp() (*App, error) {
 			// execute grpc request
 			resp, err := client.Echo(ctx, &examplepb.EchoRequest{Message: "hello", DurationMs: 0})
 			if err != nil {
-				fmt.Println(err)
+				log.Printf("Error: %v", err)
 				return
 			}
 
@@ -135,7 +135,7 @@ func NewApp() (*App, error) {
 			// execute grpc request
 			resp, err := client.Echo(ctx, &examplepb.EchoRequest{Message: "hello", DurationMs: 0})
 			if err != nil {
-				fmt.Println(err)
+				log.Printf("Error: %v", err)
 				return
 			}
 
diff --git a/example/templates/index.html b/example/templates/index.html
index b16b41c..5d48348 100644
--- a/example/templates/index.html
+++ b/example/templates/index.html
@@ -5,8 +5,13 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Example App</title>
     <style>
-      #output {
-        margin-top: 20px;
+      h2 {
+        padding: 0;
+        margin: 30px 0 5px;
+      }
+
+      .output {
+        margin-top: 10px;
         height: 200px;
         width: 600px;
         border: 1px solid black;
@@ -15,17 +20,9 @@
       }
     </style>
     <script>
-      let eventSource;
-
-      const stopEvents = () => {
-        if (eventSource && eventSource.readyState !== EventSource.CLOSED) {
-          eventSource.close();
-        }
-      }
-
-      const runExample = (endpoint) => {
-        eventSource = new EventSource(endpoint);
-        const output = document.getElementById('output');
+      const runFanoutExample = () => {
+        const eventSource = new EventSource('/fanout-events');
+        const output = document.getElementById('fanout-example-output');
 
         output.innerHTML = '';
 
@@ -40,16 +37,48 @@
         };
 
         eventSource.onerror = function(error) {
-          console.error('EventSource failed:', error);
+          console.error('Fanout Example EventSource failed:', error);
         };
+      };
+
+      let subscriptionES;
+
+      const startFanoutSubscribeExample = () => {
+        subscriptionES = new EventSource('/fanout-subscribe-events');
+        const output = document.getElementById('fanout-subscribe-output');
+
+        output.innerHTML = '';
+
+        subscriptionES.onmessage = function(event) {
+          const el = document.createElement('div');
+          el.textContent = event.data;
+          output.appendChild(el);
+
+          if (event.data === "finished") {
+            subscriptionES.close();
+          }
+        };
+
+        subscriptionES.onerror = function(error) {
+          console.error('Fanout Subscribe Example EventSource failed:', error);
+        };
+      }
+
+      const stopFanoutSubscribeExample = () => {
+        if (subscriptionES && subscriptionES.readyState !== EventSource.CLOSED) {
+          subscriptionES.close();
+        }
       }
-    </script>
+</script>
   </head>
   <body>
     <h1>Example App</h1>
-    <button type="button" onclick="runExample('/fanout-events')">Start Fanout Example</button>
-    <button type="button" onclick="runExample('/fanout-subscribe-events')">Start Fanout Subscribe Example</button>
-    <button type="button" onclick="stopEvents()">Stop</button>
-    <div id="output"></div>
+    <h2>Fanout Example:</h2>
+    <button type="button" onclick="runFanoutExample()">Run</button>
+    <div id="fanout-example-output" class="output"></div>
+    <h2>Fanout Subscribe Example:</h2>
+    <button type="button" onclick="startFanoutSubscribeExample()">Start</button>
+    <button type="button" onclick="stopFanoutSubscribeExample()">Stop</button>
+    <div id="fanout-subscribe-output" class="output"></div>
   </body>
 </html>
diff --git a/hack/tilt/chaoskube.yaml b/hack/tilt/chaoskube.yaml
index 60a01c8..bedab70 100644
--- a/hack/tilt/chaoskube.yaml
+++ b/hack/tilt/chaoskube.yaml
@@ -83,10 +83,10 @@ spec:
       - name: chaoskube
         image: ghcr.io/linki/chaoskube:v0.31.0
         args:
-        - --interval=2m
+        - --interval=30s
         - --labels=app.kubernetes.io/name=example,app.kubernetes.io/component=server
         - --namespaces=default
-        - --minimum-age=4m
+        - --minimum-age=0s
         - --no-dry-run
         securityContext:
           runAsNonRoot: true
diff --git a/hack/tilt/example-app.yaml b/hack/tilt/example-app.yaml
index 80517b9..a57c07b 100644
--- a/hack/tilt/example-app.yaml
+++ b/hack/tilt/example-app.yaml
@@ -67,4 +67,9 @@ spec:
         - name: http
           protocol: TCP
           containerPort: 4000
+        #env:
+        #- name: GRPC_GO_LOG_SEVERITY_LEVEL
+        #  value: "INFO"
+        #- name: GRPC_GO_LOG_VERBOSITY_LEVEL
+        #  value: "2"
         resources: {}

From 6e2961553fcfc20f6fda9449bcbbbc3675f758d7 Mon Sep 17 00:00:00 2001
From: Andres Morey <andresmarcel@gmail.com>
Date: Wed, 11 Sep 2024 18:49:13 +0300
Subject: [PATCH 2/2] reverted to grpc-dispatcher-go path

---
 README.md                   | 6 +++---
 example/cmd/app/main.go     | 2 +-
 example/cmd/server/main.go  | 2 +-
 example/go.mod              | 6 +++---
 example/internal/app/app.go | 4 ++--
 go.mod                      | 2 +-
 6 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index 60c8941..56876a2 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,7 @@ Try it out and let us know what you think! If you notice any bugs or have any fe
 
 First, add the library to your Go project:
 ```console
-go get github.com/kubetail-org/grpc-dispatcher
+go get github.com/kubetail-org/grpc-dispatcher-go
 ```
 
 Next write your dispatch code:
@@ -26,7 +26,7 @@ Next write your dispatch code:
 import (
   "context"
 
-  "github.com/kubetail-org/grpc-dispatcher"
+  "github.com/kubetail-org/grpc-dispatcher-go"
   "google.golang.org/grpc"
 )
 
@@ -99,7 +99,7 @@ func main() {
 
 ## Docs
 
-See [Go docs](https://pkg.go.dev/github.com/kubetail-org/grpc-dispatcher) for library documentation.
+See [Go docs](https://pkg.go.dev/github.com/kubetail-org/grpc-dispatcher-go) for library documentation.
 
 ## Example
 
diff --git a/example/cmd/app/main.go b/example/cmd/app/main.go
index 0874a97..4168616 100644
--- a/example/cmd/app/main.go
+++ b/example/cmd/app/main.go
@@ -23,7 +23,7 @@ import (
 	"syscall"
 	"time"
 
-	"github.com/kubetail-org/grpc-dispatcher/example/internal/app"
+	"github.com/kubetail-org/grpc-dispatcher-go/example/internal/app"
 )
 
 func main() {
diff --git a/example/cmd/server/main.go b/example/cmd/server/main.go
index 07aa715..ad75a0f 100644
--- a/example/cmd/server/main.go
+++ b/example/cmd/server/main.go
@@ -26,7 +26,7 @@ import (
 
 	"google.golang.org/grpc"
 
-	"github.com/kubetail-org/grpc-dispatcher/example/internal/examplepb"
+	"github.com/kubetail-org/grpc-dispatcher-go/example/internal/examplepb"
 )
 
 type ExampleService struct {
diff --git a/example/go.mod b/example/go.mod
index a1e8baa..957466d 100644
--- a/example/go.mod
+++ b/example/go.mod
@@ -1,15 +1,15 @@
-module github.com/kubetail-org/grpc-dispatcher/example
+module github.com/kubetail-org/grpc-dispatcher-go/example
 
 go 1.22.5
 
 require (
 	github.com/gin-gonic/gin v1.10.0
-	github.com/kubetail-org/grpc-dispatcher v0.0.0
+	github.com/kubetail-org/grpc-dispatcher-go v0.0.0
 	google.golang.org/grpc v1.66.0
 	google.golang.org/protobuf v1.34.2
 )
 
-replace github.com/kubetail-org/grpc-dispatcher => ../
+replace github.com/kubetail-org/grpc-dispatcher-go => ../
 
 require (
 	github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect
diff --git a/example/internal/app/app.go b/example/internal/app/app.go
index 8edb37c..0431956 100644
--- a/example/internal/app/app.go
+++ b/example/internal/app/app.go
@@ -25,8 +25,8 @@ import (
 	"time"
 
 	"github.com/gin-gonic/gin"
-	grpcdispatcher "github.com/kubetail-org/grpc-dispatcher"
-	"github.com/kubetail-org/grpc-dispatcher/example/internal/examplepb"
+	grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
+	"github.com/kubetail-org/grpc-dispatcher-go/example/internal/examplepb"
 	"google.golang.org/grpc"
 	"google.golang.org/grpc/credentials/insecure"
 )
diff --git a/go.mod b/go.mod
index deb98d7..6dfc781 100644
--- a/go.mod
+++ b/go.mod
@@ -1,4 +1,4 @@
-module github.com/kubetail-org/grpc-dispatcher
+module github.com/kubetail-org/grpc-dispatcher-go
 
 go 1.22.5