vefaas

package
v0.0.0-...-998a342 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(handler interface{})

Start stats vefaas runtime server with provided handler to handle incoming requests.

Currently, the supported handler signatures are:

- func(context.Context, *events.HTTPRequest) (*events.EventResponse, error) for handling regular http requests, like those from api gateway trigger.

- func(context.Context, *events.CloudEvent) (*events.EventResponse, error) for handling CloudEvent request, like those from timer trigger, tos trigger, kafka trigger, etc.

- func(context.Context, interface{}) (*events.EventResponse, error) for handling requests of any type, especially for those business that handle both regular http requests and CloudEvent requests, and the developer can use type assertion to distinguish and process them.

Example

ExampleStart demonstrated how to start a simple vefaas function serving http request.

// Define your handler.
handler := func(ctx context.Context, r *events.HTTPRequest) (*events.EventResponse, error) {
	log.Printf("request id: %v", vefaascontext.RequestIdFromContext(ctx))
	log.Printf("request headers: %v", r.Headers)

	body, _ := json.Marshal(map[string]string{"message": "Hello veFaaS!"})
	return &events.EventResponse{
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
		Body: body,
	}, nil
}

// Start your vefaas function =D.
Start(handler)
Output:

func StartWithInitializer

func StartWithInitializer(handler interface{}, initializer interface{})

StartWithInitializer starts vefaas runtime server with provided handler and initializer.

See Start for the supported handler signatures.

The functionality of initializer is to do the initialization work before your runtime server can handle any incoming request, like setup the connection to database, setup the http/rpc client for downstream services, etc. Currently the supported initializer signatures are: - func(context.Context) error

Example

ExampleStartWithInitializer shows how to start a simple vefaas function with some initialization logic.

// First define your handler.
handler := func(ctx context.Context, r *events.HTTPRequest) (*events.EventResponse, error) {
	body, _ := json.Marshal(map[string]string{"message": "Hello veFaaS!"})
	return &events.EventResponse{
		Headers: map[string]string{
			"Content-Type": "application/json",
		},
		Body: body,
	}, nil
}

// Then we have a dummy initializer.
initializer := func(ctx context.Context) error {
	fmt.Println("init function...")
	// Some initialization work, like setup http client, create database connections, etc.
	fmt.Println("init function done")
	return nil
}

// Start your vefaas function =D.
StartWithInitializer(handler, initializer)
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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