Skip to content

Commit e93219f

Browse files
authored
Improves dev environment and switches to grpc-dispatcher-go library path (#5)
* improvements to example and tilt resources * switched to `grpc-dispatcher-go` library path
1 parent 346f44c commit e93219f

File tree

9 files changed

+71
-37
lines changed

9 files changed

+71
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ Try it out and let us know what you think! If you notice any bugs or have any fe
1818

1919
First, add the library to your Go project:
2020
```console
21-
go get github.com/kubetail-org/grpc-dispatcher
21+
go get github.com/kubetail-org/grpc-dispatcher-go
2222
```
2323

2424
Next write your dispatch code:
2525
```go
2626
import (
2727
"context"
2828

29-
"github.com/kubetail-org/grpc-dispatcher"
29+
"github.com/kubetail-org/grpc-dispatcher-go"
3030
"google.golang.org/grpc"
3131
)
3232

@@ -99,7 +99,7 @@ func main() {
9999
100100
## Docs
101101

102-
See [Go docs](https://pkg.go.dev/github.com/kubetail-org/grpc-dispatcher) for library documentation.
102+
See [Go docs](https://pkg.go.dev/github.com/kubetail-org/grpc-dispatcher-go) for library documentation.
103103

104104
## Example
105105

example/cmd/app/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"syscall"
2424
"time"
2525

26-
"github.com/kubetail-org/grpc-dispatcher/example/internal/app"
26+
"github.com/kubetail-org/grpc-dispatcher-go/example/internal/app"
2727
)
2828

2929
func main() {
@@ -35,7 +35,7 @@ func main() {
3535
// create app
3636
app, err := app.NewApp()
3737
if err != nil {
38-
log.Fatal(err)
38+
log.Fatalf("Error: %v", err)
3939
}
4040

4141
// create server
@@ -51,7 +51,7 @@ func main() {
5151
go func() {
5252
log.Println("Starting server on :4000")
5353
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
54-
log.Fatal(err)
54+
log.Fatalf("Error: %v", err)
5555
}
5656
}()
5757

@@ -68,7 +68,7 @@ func main() {
6868
done := make(chan struct{})
6969
go func() {
7070
if err := server.Shutdown(ctx); err != nil {
71-
log.Println(err)
71+
log.Printf("Error: %v", err)
7272
}
7373
close(done)
7474
}()

example/cmd/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
"google.golang.org/grpc"
2828

29-
"github.com/kubetail-org/grpc-dispatcher/example/internal/examplepb"
29+
"github.com/kubetail-org/grpc-dispatcher-go/example/internal/examplepb"
3030
)
3131

3232
type ExampleService struct {

example/go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
module github.com/kubetail-org/grpc-dispatcher/example
1+
module github.com/kubetail-org/grpc-dispatcher-go/example
22

33
go 1.22.5
44

55
require (
66
github.com/gin-gonic/gin v1.10.0
7-
github.com/kubetail-org/grpc-dispatcher v0.0.0
7+
github.com/kubetail-org/grpc-dispatcher-go v0.0.0
88
google.golang.org/grpc v1.66.0
99
google.golang.org/protobuf v1.34.2
1010
)
1111

12-
replace github.com/kubetail-org/grpc-dispatcher => ../
12+
replace github.com/kubetail-org/grpc-dispatcher-go => ../
1313

1414
require (
1515
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef // indirect

example/internal/app/app.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ import (
2525
"time"
2626

2727
"github.com/gin-gonic/gin"
28-
grpcdispatcher "github.com/kubetail-org/grpc-dispatcher"
29-
"github.com/kubetail-org/grpc-dispatcher/example/internal/examplepb"
28+
grpcdispatcher "github.com/kubetail-org/grpc-dispatcher-go"
29+
"github.com/kubetail-org/grpc-dispatcher-go/example/internal/examplepb"
3030
"google.golang.org/grpc"
3131
"google.golang.org/grpc/credentials/insecure"
3232
)
@@ -99,7 +99,7 @@ func NewApp() (*App, error) {
9999
// execute grpc request
100100
resp, err := client.Echo(ctx, &examplepb.EchoRequest{Message: "hello", DurationMs: 0})
101101
if err != nil {
102-
fmt.Println(err)
102+
log.Printf("Error: %v", err)
103103
return
104104
}
105105

@@ -135,7 +135,7 @@ func NewApp() (*App, error) {
135135
// execute grpc request
136136
resp, err := client.Echo(ctx, &examplepb.EchoRequest{Message: "hello", DurationMs: 0})
137137
if err != nil {
138-
fmt.Println(err)
138+
log.Printf("Error: %v", err)
139139
return
140140
}
141141

example/templates/index.html

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<title>Example App</title>
77
<style>
8-
#output {
9-
margin-top: 20px;
8+
h2 {
9+
padding: 0;
10+
margin: 30px 0 5px;
11+
}
12+
13+
.output {
14+
margin-top: 10px;
1015
height: 200px;
1116
width: 600px;
1217
border: 1px solid black;
@@ -15,17 +20,9 @@
1520
}
1621
</style>
1722
<script>
18-
let eventSource;
19-
20-
const stopEvents = () => {
21-
if (eventSource && eventSource.readyState !== EventSource.CLOSED) {
22-
eventSource.close();
23-
}
24-
}
25-
26-
const runExample = (endpoint) => {
27-
eventSource = new EventSource(endpoint);
28-
const output = document.getElementById('output');
23+
const runFanoutExample = () => {
24+
const eventSource = new EventSource('/fanout-events');
25+
const output = document.getElementById('fanout-example-output');
2926

3027
output.innerHTML = '';
3128

@@ -40,16 +37,48 @@
4037
};
4138

4239
eventSource.onerror = function(error) {
43-
console.error('EventSource failed:', error);
40+
console.error('Fanout Example EventSource failed:', error);
4441
};
42+
};
43+
44+
let subscriptionES;
45+
46+
const startFanoutSubscribeExample = () => {
47+
subscriptionES = new EventSource('/fanout-subscribe-events');
48+
const output = document.getElementById('fanout-subscribe-output');
49+
50+
output.innerHTML = '';
51+
52+
subscriptionES.onmessage = function(event) {
53+
const el = document.createElement('div');
54+
el.textContent = event.data;
55+
output.appendChild(el);
56+
57+
if (event.data === "finished") {
58+
subscriptionES.close();
59+
}
60+
};
61+
62+
subscriptionES.onerror = function(error) {
63+
console.error('Fanout Subscribe Example EventSource failed:', error);
64+
};
65+
}
66+
67+
const stopFanoutSubscribeExample = () => {
68+
if (subscriptionES && subscriptionES.readyState !== EventSource.CLOSED) {
69+
subscriptionES.close();
70+
}
4571
}
46-
</script>
72+
</script>
4773
</head>
4874
<body>
4975
<h1>Example App</h1>
50-
<button type="button" onclick="runExample('/fanout-events')">Start Fanout Example</button>
51-
<button type="button" onclick="runExample('/fanout-subscribe-events')">Start Fanout Subscribe Example</button>
52-
<button type="button" onclick="stopEvents()">Stop</button>
53-
<div id="output"></div>
76+
<h2>Fanout Example:</h2>
77+
<button type="button" onclick="runFanoutExample()">Run</button>
78+
<div id="fanout-example-output" class="output"></div>
79+
<h2>Fanout Subscribe Example:</h2>
80+
<button type="button" onclick="startFanoutSubscribeExample()">Start</button>
81+
<button type="button" onclick="stopFanoutSubscribeExample()">Stop</button>
82+
<div id="fanout-subscribe-output" class="output"></div>
5483
</body>
5584
</html>

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module github.com/kubetail-org/grpc-dispatcher
1+
module github.com/kubetail-org/grpc-dispatcher-go
22

33
go 1.22.5
44

hack/tilt/chaoskube.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ spec:
8383
- name: chaoskube
8484
image: ghcr.io/linki/chaoskube:v0.31.0
8585
args:
86-
- --interval=2m
86+
- --interval=30s
8787
- --labels=app.kubernetes.io/name=example,app.kubernetes.io/component=server
8888
- --namespaces=default
89-
- --minimum-age=4m
89+
- --minimum-age=0s
9090
- --no-dry-run
9191
securityContext:
9292
runAsNonRoot: true

hack/tilt/example-app.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,9 @@ spec:
6767
- name: http
6868
protocol: TCP
6969
containerPort: 4000
70+
#env:
71+
#- name: GRPC_GO_LOG_SEVERITY_LEVEL
72+
# value: "INFO"
73+
#- name: GRPC_GO_LOG_VERBOSITY_LEVEL
74+
# value: "2"
7075
resources: {}

0 commit comments

Comments
 (0)