moroncloudevents

package module
v0.0.0-...-c1632aa Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: Apache-2.0 Imports: 9 Imported by: 3

README

Moron CloudEvents

GoDoc

Tired of a complex API surface? Moron CloudEvents makes it easy to set up a CloudEvent receiver side-by-side with HTTP handlers. Check out this simple example:

package main

import (
	"context"
	"log"
	"net/http"

	cloudevents "github.com/cloudevents/sdk-go"
	moron "github.com/spencer-p/moroncloudevents"
)

func index(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello from HTTP!"))
}

func receive(ctx context.Context, event cloudevents.Event, r *cloudevents.EventResponse) error {
	databytes, _ := event.DataBytes()
	log.Printf("Received CloudEvent with data: %q\n", databytes)
	return nil
}

func main() {
	svr, err := moron.NewServer(&moron.ServerConfig{
		Port:                  "8080",
		CloudEventReceivePath: "/apis/receive",
	})
	if err != nil {
		log.Fatal("Could not create server: ", err)
	}

	svr.HandleCloudEvents(receive)

	svr.HandleFunc("/", index)

	log.Fatal(svr.ListenAndServe())
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	*http.ServeMux
	// contains filtered or unexported fields
}

Server allows you to simply serve HTTP handlers and a CloudEvent receiver side-by-side.

func NewServer

func NewServer(conf *ServerConfig) (*Server, error)

func (*Server) CloudEventClient

func (s *Server) CloudEventClient() cloudevents.Client

CloudEventClient returns the Server's client for CloudEvents.

func (*Server) HandleCloudEvents

func (s *Server) HandleCloudEvents(handler cloudeventsclient.ReceiveFull)

HandleCloudEvents sets the handler for CloudEvent receiveing. There can only be one.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() error

ListenAndServe starts serving the HTTP handlers and CloudEvent receiver, blocking until termination.

func (*Server) Shutdown

func (s *Server) Shutdown()

Shutdown will call the cancel function for the server if it is already listening and serving.

type ServerConfig

type ServerConfig struct {
	// Port is the port that serves both HTTP handlers and CloudEvent receiving.
	// Defaults to 80.
	// +optional
	Port string

	// CloudEventReceivePath is the path reserved for CloudEvents.
	// If omitted, defaults to "/".
	// +optional
	CloudEventReceivePath string

	// CloudEventTargets is a slice of targets that the client will send CloudEvents on.
	// +optional
	CloudEventTargets []string

	// ConvertFn is a function to convert non-CloudEvent requests to the CloudEventReceivePath into CloudEvents.
	// +optional
	ConvertFn cloudevents.ConvertFn

	// TransportOptions are forwarded directly to CloudEvent transport construction.
	// +optional
	TransportOptions []cloudeventshttp.Option

	// ClientOptions are forwarded directly to CloudEvent client construction.
	// +optional
	ClientOptions []cloudeventsclient.Option
}

ServerConfig is a struct for options when constructing a new HTTP server.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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