Skip to content

Cloudevents flexible implementation in Go

Notifications You must be signed in to change notification settings

elhedran/fast-cloudevents-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fast Cloudevents

A small and configurable implementation of the cloudevents spec for Go, with support for fasthttp.

GoDoc Go Report Sourcegraph

This package exists to replace the heavy, complex, and difficult to use (in my opinion) go-sdk.

Example

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)
}

Notes

  • 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.

Features

V1 spec

See JSON, HTTP specifications.

fasthttp support:

JSON support:

Conventions

  • cec means CloudEvent Client
  • ce and ces mean CloudEvent and CloudEvents respectively
  • res is sometimes used to refer to a second CloudEvents if there are multiple
  • re and res mean Response CloudEvent and Response CloudEvents respectively
  • Get/Set and Send/Recv refer to Servers and Clients writing/reading events, respectively

See also

FastHTTP best practices.

About

Cloudevents flexible implementation in Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages