A small and configurable implementation of the cloudevents spec for Go, with support for fasthttp.
This package exists to replace the heavy, complex, and difficult to use (in my opinion) go-sdk.
See main.go and fastce/examples.go.
package main
import (
fastce "github.com/creativecactus/fast-cloudevents-go/fastce"
jsonce "github.com/creativecactus/fast-cloudevents-go/jsonce"
)
func main(){
// An example of a custom unmarshal function
MyMapToCE := func(cm jsonce.CEMap)(ce jsonce.CloudEvent, err error){
// In this example, we still want to perform the DefaultCEToMap validation
// But we will automatically generate an ID if it is not present
if id, ok := cm["id"].(string); !ok || len(id) < 1 {
cm["id"] = "SomeRandomRuntimeGeneratedID"
}
return jsonce.DefaultMapToCE(cm)
}
handler := func(ces jsonce.CloudEvents)(res jsonce.CloudEvents, err error){
// This is a simple echo server
res = ces
return
}
CEServer{}.ListenAndServeCE(listenAddr,jsonce.DefaultCEToMap,MyMapToCE,handler)
}
- While most functions operate on []jsonce.CloudEvent (AKA jsonce.CloudEvents), structured and binary modes will discard all but the first event.
- In binary mode, all extension headers are treated as strings. This might look strange when sending and receiving in different modes. To support receiving non-strings in binary mode, use a custom unmarshal mapper as in the above example.
- High level server and client types:
CEClient
,CEServer
- Mid level getters and setters for fasthttp request/response:
GetEvents
,SetEvents
,SendEvents
,RecvEvents
- Flexible CloudEvents type can be used standalone
- Lightweight, easy to audit, heavily tested
- Good support for CloudEvents spec
- Easy to use
See JSON, HTTP specifications.
fasthttp support:
- 3.1 Binary Content Mode ☑️ Send and receive.
- 3.2 Structured Content Mode ☑️ Send and receive.
- 3.3. Batched Content Mode ☑️ Send and receive.
- 2.2. Type System Mapping ☑️ Supported on known fields, user must enforce for extensions as needed using type assertion.
- 2.4. JSONSchema Validation ❌ Not tested yet.
- 3. Envelope 🕙 Fully suported, partially complaint.
- 4. JSON Batch Format ☑️ Supported.
cec
means CloudEvent Clientce
andces
mean CloudEvent and CloudEvents respectivelyres
is sometimes used to refer to a second CloudEvents if there are multiplere
andres
mean Response CloudEvent and Response CloudEvents respectivelyGet
/Set
andSend
/Recv
refer to Servers and Clients writing/reading events, respectively