-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.go
88 lines (70 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"net/http"
"strings"
"github.com/EventStore/EventStore-Client-Go/v3/esdb"
"github.com/gin-gonic/gin"
)
func main() {
router := gin.Default()
settings, err := esdb.ParseConnectionString("esdb://admin:changeit@esdb-local:2113?tls=false&tlsVerifyCert=false")
if err != nil {
panic(err)
}
eventStore, err := esdb.NewClient(settings)
if err != nil {
panic(err)
}
const visitorsStream string = "visitors-stream"
router.GET("/hello-world", func(c *gin.Context) {
visitor := c.Query("visitor")
if visitor == "" {
visitor = "Visitor"
}
visitorGreetedJson, err := json.Marshal(&VisitorGreeted{Visitor: visitor})
if err != nil {
panic(err)
}
eventData := esdb.EventData{
ContentType: esdb.ContentTypeJson,
EventType: "VisitorGreeted",
Data: visitorGreetedJson,
}
_, err = eventStore.AppendToStream(context.Background(), visitorsStream, esdb.AppendToStreamOptions{ExpectedRevision: esdb.Any{}}, eventData)
if err != nil {
panic(err)
}
eventStream, err := eventStore.ReadStream(context.Background(), visitorsStream, esdb.ReadStreamOptions{Direction: esdb.Forwards}, math.MaxUint64)
if err != nil {
panic(err)
}
defer eventStream.Close()
var visitorsGreeted []string
for {
event, err := eventStream.Recv()
if errors.Is(err, io.EOF) {
break
}
if err != nil {
panic(err)
}
var visitorGreeted VisitorGreeted
err = json.Unmarshal(event.Event.Data, &visitorGreeted)
if err != nil {
panic(err)
}
visitorsGreeted = append(visitorsGreeted, visitorGreeted.Visitor)
}
c.String(http.StatusOK, fmt.Sprintf("%d visitors have been greeted, they are: [%s]", len(visitorsGreeted), strings.Join(visitorsGreeted, ",")))
})
router.Run(fmt.Sprintf(":%d", 8080))
}
type VisitorGreeted struct {
Visitor string `json:"Visitor"`
}