-
Notifications
You must be signed in to change notification settings - Fork 223
/
main.go
94 lines (78 loc) · 2.47 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
89
90
91
92
93
94
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"context"
"fmt"
"log"
"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"
cloudevents "github.com/cloudevents/sdk-go/v2"
)
type envConfig struct {
// Port on which to listen for cloudevents
Port int `envconfig:"RCV_PORT" default:"8080"`
Path string `envconfig:"RCV_PATH" default:"/"`
}
type Example struct {
Sequence int `json:"id"`
Message string `json:"message"`
}
func eventReceiver(ctx context.Context, event cloudevents.Event) (*cloudevents.Event, error) {
fmt.Printf("Got Event Context: %+v\n", event.Context)
data := &Example{}
if err := event.DataAs(data); err != nil {
fmt.Printf("Got Data Error: %s\n", err.Error())
}
fmt.Printf("Got Data: %+v\n", data)
fmt.Printf("----------------------------\n")
if data.Sequence%3 == 0 {
responseEvent := cloudevents.NewEvent()
responseEvent.SetID(uuid.New().String())
responseEvent.SetSource("/mod3")
responseEvent.SetType("samples.http.mod3")
responseEvent.SetSubject(fmt.Sprintf("%s#%d", event.Source(), data.Sequence))
_ = responseEvent.SetData(cloudevents.ApplicationJSON, Example{
Sequence: data.Sequence,
Message: "mod 3!",
})
return &responseEvent, nil
} else if data.Sequence%7 == 0 {
responseEvent := cloudevents.NewEvent()
responseEvent.SetID(uuid.New().String())
responseEvent.SetSource("/mod7")
responseEvent.SetType("samples.http.mod7")
responseEvent.SetSubject(fmt.Sprintf("%s#%d", event.Source(), data.Sequence))
_ = responseEvent.SetData(cloudevents.ApplicationJSON, Example{
Sequence: data.Sequence,
Message: "mod 7 has issues!",
})
return &responseEvent, cloudevents.NewHTTPResult(500, "this is a mod 7 server error message")
}
return nil, nil
}
func main() {
var env envConfig
if err := envconfig.Process("", &env); err != nil {
log.Fatalf("Failed to process env var: %s", err)
}
ctx := context.Background()
p, err := cloudevents.NewHTTP(cloudevents.WithPort(env.Port), cloudevents.WithPath(env.Path))
if err != nil {
log.Fatalf("failed to create protocol: %s", err.Error())
}
c, err := cloudevents.NewClient(p,
cloudevents.WithUUIDs(),
cloudevents.WithTimeNow(),
)
if err != nil {
log.Fatalf("failed to create client: %s", err.Error())
}
if err := c.StartReceiver(ctx, eventReceiver); err != nil {
log.Fatalf("failed to start receiver: %s", err.Error())
}
log.Printf("listening on :%d%s\n", env.Port, env.Path)
<-ctx.Done()
}