Skip to content
This repository was archived by the owner on Dec 11, 2023. It is now read-only.

Commit 525a147

Browse files
authored
Merge pull request #24 from triggermesh/ce-envvars
CE context attributes from envvars
2 parents 46c4c7d + d1452c0 commit 525a147

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ type Specification struct {
6666
InternalAPIport string `envconfig:"internal_api_port" default:"80"`
6767
// Lambda API port to put function requests and get results
6868
ExternalAPIport string `envconfig:"external_api_port" default:"8080"`
69-
// Parent Knative Service name
70-
Service string `envconfig:"k_service"`
7169

7270
// Apply response wrapping before sending it back to the client.
7371
// Common case - AWS Lambda functions usually returns data formatted for API Gateway service.
@@ -240,7 +238,10 @@ func (s *Specification) mapEvent(h http.Handler) http.Handler {
240238
case "API_GATEWAY":
241239
mapper = apigateway.NewMapper()
242240
case "CLOUDEVENTS":
243-
mapper = cloudevents.NewMapper(s.Service)
241+
mapper = cloudevents.NewMapper()
242+
if err := envconfig.Process("CE", mapper); err != nil {
243+
log.Fatalf("Cannot process CloudEvents wrapper env variables: %v", err)
244+
}
244245
default:
245246
mapper = passthrough.NewMapper()
246247
}

pkg/events/cloudevents/mapper.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,28 @@ import (
88
"github.com/google/uuid"
99
)
1010

11-
type CloudEvents struct {
12-
Service string
11+
// CloudEvent is a data structure required to map KLR responses to cloudevents
12+
type CloudEvent struct {
13+
EventType string `envconfig:"type" default:"ce.klr.triggermesh.io"`
14+
Source string `envconfig:"k_service" default:"knative-lambda-runtime"`
15+
Subject string `envconfig:"subject" default:"klr-response"`
1316
}
1417

15-
func NewMapper(service string) *CloudEvents {
16-
return &CloudEvents{
17-
Service: service,
18-
}
18+
// NewMapper reurns an empty instance of CloudEvent structure
19+
func NewMapper() *CloudEvent {
20+
return &CloudEvent{}
1921
}
2022

21-
func (c *CloudEvents) Request(r *http.Request) {}
23+
// Request method can be used to customize incoming requests before passing them
24+
// to the KLR function
25+
func (c *CloudEvent) Request(r *http.Request) {}
2226

23-
func (c *CloudEvents) Response(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
27+
// Response method converts generic KLR response to the Cloudevent format
28+
func (c *CloudEvent) Response(w http.ResponseWriter, statusCode int, data []byte) (int, error) {
2429
response := cloudevents.NewEvent()
25-
response.SetType("ce.klr.triggermesh.io")
26-
response.SetSource(c.Service)
30+
response.SetType(c.EventType)
31+
response.SetSource(c.Source)
32+
response.SetSubject(c.Subject)
2733
response.SetID(uuid.New().String())
2834
if err := response.SetData(cloudevents.ApplicationJSON, data); err != nil {
2935
return 0, fmt.Errorf("failed to set event data: %w", err)

0 commit comments

Comments
 (0)