cewrap

package module
v0.0.0-...-6573be1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2023 License: MIT Imports: 11 Imported by: 0

README

cewrap

CE Wrapper is a wrapper to generate CloudEvents events for services that do not emit events themselves. It will initially work for RESTful APIs.

It is a completely stateless proxy that passes the request unmodified to the downstream service, and emits an event for requests that are of interest.

Default these are requests that change state, like POST, PUT, PATCH and DELETE, but other methods can be added.

The mandatory cloud event context attributes are set using the information of the incoming http request, or with values set by the user. This way ample information is available for filtering and subscribing.

Context Attributes

  • id, will be set with an autogenerated UUID
  • source, needs to be configured
  • subject, the path of the request
  • type, this value will be a configured prefix follow by the methodname and the suffix _handled
  • time, the time the forwarded request finished
  • datacontenttype, the content type of the response from the downstream service
  • dataschema, can be configured

The data will contain a json struct with the response body the downstream service returned.

Example

Assume the downstream service is on service.example.com and it returns json formatted responses with person information.

A successful post request to http://service.example.com/persons/id12345 resource that contains persons will result in an event with the following attributes.

  • subject: /persons/id12345
  • type: com.example.persons.post_handled
  • source: http://service.example.com/

Command line parameters and env vars

parameter env var description
-sink K_SINK, CWE_SINK The url of the event sink.
-source CEW_SOURCE The source of the event.
-type CEW_TYPE_PREFIX The prefix for the type.
-dataschema CEW_DATASCHEMA The URL for the dataschema of the event data.
-downstream CEW_DOWNSTREAM Downstream service.
-port PORT Listening port of the wrapper, defaults to 8080.
-extra-methods CEW_EXTRA_METHODS Extra methods to add to the standard state changing methods

Test setup

Run go-httpbin on port 9090.

podman run --rm -p 9090:8080 docker.io/mccutchen/go-httpbin:v2.11.1

Run cesourcewrap on port 8080.

podman run --rm \
    -p 8080:8080 \
    -e CEW_SINK=http://192.168.0.202:10000 \
    -e CEW_DOWNSTREAM=http://192.168.0.202:9090 \
    -e CEW_EXTRA_METHODS=GET \
    docker.io/peterzandbergen/cesourcewrap:v0.0.1

Run event display on port 10000

podman run --rm \
    -p 10000:80 \
    gcr.io/knative-releases/knative.dev/eventing/cmd/event_display

Documentation

Overview

Package cewrap implements event source proxies for knative agnostic services.

Index

Constants

This section is empty.

Variables

Functions

This section is empty.

Types

type Source

type Source struct {
	// contains filtered or unexported fields
}

func NewSource

func NewSource(options ...SourceOption) *Source

func (*Source) Handler

func (s *Source) Handler() http.HandlerFunc

Handler returns a HandlerFunc that handles the requests.

It passes the request to the downstream service and generates a cloud event and sends it to the sink.

type SourceOption

type SourceOption interface {
	// contains filtered or unexported methods
}

func WithChangeMethods

func WithChangeMethods(m []string) SourceOption

func WithDataschema

func WithDataschema(v string) SourceOption

func WithDownstream

func WithDownstream(u string) SourceOption

func WithHTTPClient

func WithHTTPClient(c *http.Client) SourceOption

func WithLogger

func WithLogger(l *slog.Logger) SourceOption

func WithPathPrefix

func WithPathPrefix(v string) SourceOption

func WithSink

func WithSink(s cloudevents.Client) SourceOption

func WithSource

func WithSource(v string) SourceOption

func WithTypePrefix

func WithTypePrefix(v string) SourceOption

Directories

Path Synopsis
cmd
source
The source command implements a proxy server that emits cloud events when it successfully handled a method that changes the data in the downstream service.
The source command implements a proxy server that emits cloud events when it successfully handled a method that changes the data in the downstream service.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL