resources

package
v0.9.0-rc.35 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2023 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

Functions

func IsBuildEnvirnonment

func IsBuildEnvirnonment() bool

IsBuildEnvirnonment will return true if the code is running during config discovery.

func NewBucket

func NewBucket(name string, permissions ...BucketPermission) (storage.Bucket, error)

NewBucket register this bucket as a required resource for the calling function/container and register the permissions required by the currently scoped function for this resource.

func NewCollection

func NewCollection(name string, permissions ...CollectionPermission) (documents.CollectionRef, error)

NewCollection register this collection as a required resource for the calling function/container.

func NewQueue

func NewQueue(name string, permissions ...QueuePermission) (queues.Queue, error)

NewQueue registers this queue as a required resource for the calling function/container.

Example

See ExampleNewSchedule() for processing a Queue.

queue, err := NewQueue("work", QueueSending)
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

exampleApi, err := NewApi("example")
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

exampleApi.Get("/hello/:name", func(hc *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {
	params := hc.Request.PathParams()

	if params == nil || len(params["name"]) == 0 {
		hc.Response.Body = []byte("error retrieving path params")
		hc.Response.Status = http.StatusBadRequest
	} else {
		_, err = queue.Send(hc.Request.Context(), []*queues.Task{
			{
				ID:          uuid.NewString(),
				PayloadType: "custom-X",
				Payload:     map[string]interface{}{"fruit": "apples"},
			},
		})
		if err != nil {
			hc.Response.Body = []byte("error sending event")
			hc.Response.Status = http.StatusInternalServerError
		} else {
			hc.Response.Body = []byte("Hello " + params["name"])
			hc.Response.Status = http.StatusOK
		}
	}

	return next(hc)
})

fmt.Println("running example")
if err := Run(); err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

func NewSchedule

func NewSchedule(name, rate string, handlers ...faas.EventMiddleware) error

NewSchedule provides a new schedule, which can be configured with a rate/cron and a callback to run on the schedule. The rate is e.g. '7 days'. All rates accept a number and a frequency. Valid frequencies are 'days', 'hours' or 'minutes'.

Example

This shows how to create a Schedule and subscribe to it. It also shows how to process a queue within this handler.

queue, err := NewQueue("work", QueueReceving)
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

err = NewSchedule("job", "10 minutes", func(ec *faas.EventContext, next faas.EventHandler) (*faas.EventContext, error) {
	fmt.Println("got scheduled event ", string(ec.Request.Data()))
	tasks, err := queue.Receive(ec.Request.Context(), 10)
	if err != nil {
		fmt.Println(err)
		return nil, err
	} else {
		for _, task := range tasks {
			fmt.Printf("processing task %s", task.Task().ID)

			if err = task.Complete(ec.Request.Context()); err != nil {
				fmt.Println(err)
			}
		}
	}

	return next(ec)
})
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

fmt.Println("running example")
if err := Run(); err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

func NewSecret

func NewSecret(name string, permissions ...SecretPermission) (secrets.SecretRef, error)

func Run

func Run() error

Run will run the function and callback the required handlers when these events are received.

Types

type Api

type Api interface {
	Get(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Put(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Patch(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Post(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Delete(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Options(path string, handler faas.HttpMiddleware, opts ...MethodOption)
	Details(ctx context.Context) (*ApiDetails, error)
	URL(ctx context.Context) (string, error)
}

Api Resource represents an HTTP API, capable of routing and securing incoming HTTP requests to handlers. path is the route path matcher e.g. '/home'. Supports path params via colon prefix e.g. '/customers/:customerId' handler the handler to register for callbacks.

Note: to chain middleware use faas.ComposeHttpMiddlware()

func NewApi

func NewApi(name string, opts ...ApiOption) (Api, error)

NewApi Registers a new API Resource.

The returned API object can be used to register Routes and Methods, with Handlers.

Example

This shows how to create an API and use the Get method to register a handler.

exampleApi, err := NewApi("example")
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

exampleApi.Get("/hello/:name", func(ctx *faas.HttpContext, next faas.HttpHandler) (*faas.HttpContext, error) {
	params := ctx.Request.PathParams()

	if params == nil || len(params["name"]) == 0 {
		ctx.Response.Body = []byte("error retrieving path params")
		ctx.Response.Status = http.StatusBadRequest
	} else {
		ctx.Response.Body = []byte("Hello " + params["name"])
		ctx.Response.Status = http.StatusOK
	}

	return next(ctx)
})

fmt.Println("running example API")
if err := Run(); err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

type ApiDetails

type ApiDetails struct {
	Details
	URL string
}

type ApiOption

type ApiOption = func(api *api)

func WithPath

func WithPath(path string) ApiOption

WithPath - Prefixes API with the given path

func WithSecurity

func WithSecurity(name string, scopes []string) ApiOption

func WithSecurityJwtRule

func WithSecurityJwtRule(name string, rule JwtSecurityRule) ApiOption

type BucketPermission

type BucketPermission string
const (
	BucketReading  BucketPermission = "reading"
	BucketWriting  BucketPermission = "writing"
	BucketDeleting BucketPermission = "deleting"
)

type CollectionPermission

type CollectionPermission string
const (
	CollectionReading  CollectionPermission = "reading"
	CollectionWriting  CollectionPermission = "writing"
	CollectionDeleting CollectionPermission = "deleting"
)

type Details

type Details struct {
	// The identifier of the resource
	ID string
	// The provider this resource is deployed with (e.g. aws)
	Provider string
	// The service this resource is deployed on (e.g. ApiGateway)
	Service string
}

type JwtSecurityRule

type JwtSecurityRule struct {
	Issuer    string
	Audiences []string
}

type Manager

type Manager interface {
	Run() error
	NewApi(name string, opts ...ApiOption) (Api, error)
	NewBucket(name string, permissions ...BucketPermission) (storage.Bucket, error)
	NewCollection(name string, permissions ...CollectionPermission) (documents.CollectionRef, error)
	NewSecret(name string, permissions ...SecretPermission) (secrets.SecretRef, error)
	NewQueue(name string, permissions ...QueuePermission) (queues.Queue, error)
	NewSchedule(name, rate string, handlers ...faas.EventMiddleware) error
	NewTopic(name string, permissions ...TopicPermission) (Topic, error)
	NewRoute(apiName, apiPath string) Route
}

Manager is the top level object that resources are created on.

func New

func New() Manager

New is used to create the top level resource manager. Note: this is not required if you are using resources.NewApi() and the like. These use a default manager instance.

type MethodOption

type MethodOption = func(mo *methodOptions)

func WithMethodSecurity

func WithMethodSecurity(name string, scopes []string) MethodOption

func WithNoMethodSecurity

func WithNoMethodSecurity() MethodOption

type QueuePermission

type QueuePermission string
const (
	QueueSending  QueuePermission = "sending"
	QueueReceving QueuePermission = "receiving"
)

type Route

type Route interface {
	Get(handler faas.HttpMiddleware, opts ...MethodOption)
	Patch(handler faas.HttpMiddleware, opts ...MethodOption)
	Put(handler faas.HttpMiddleware, opts ...MethodOption)
	Post(handler faas.HttpMiddleware, opts ...MethodOption)
	Delete(handler faas.HttpMiddleware, opts ...MethodOption)
	Options(handler faas.HttpMiddleware, opts ...MethodOption)
}

Route providers convenience functions to register a handler in a single method.

func NewRoute

func NewRoute(apiName, apiPath string) Route

type SecretPermission

type SecretPermission string
const (
	SecretReading SecretPermission = "reading"
	SecretWriting SecretPermission = "writing"
)

type Starter

type Starter interface {
	Start() error
}

type Topic

type Topic interface {
	events.Topic

	// Subscribe will register and start a subscription handler that will be called for all events from this topic.
	Subscribe(...faas.EventMiddleware)
}

Topic is a resource for pub/sub async messaging.

func NewTopic

func NewTopic(name string, permissions ...TopicPermission) (Topic, error)

NewTopic creates a new Topic with the give permissions.

Example

This shows how to create a Topic and subscribe to it using an event handler.

exampleTopic, err := NewTopic("example")
if err != nil {
	fmt.Println(err)
	os.Exit(1)
}

exampleTopic.Subscribe(func(ec *faas.EventContext, next faas.EventHandler) (*faas.EventContext, error) {
	fmt.Printf("event received %s %v", ec.Request.Topic(), string(ec.Request.Data()))

	return next(ec)
})

fmt.Println("running example")
if err := Run(); err != nil {
	fmt.Println(err)
	os.Exit(1)
}
Output:

type TopicPermission

type TopicPermission string

TopicPermission defines the available permissions on a topic

const (
	// TopicPublishing is required to call Publish on a topic.
	TopicPublishing TopicPermission = "publishing"
)

Jump to

Keyboard shortcuts

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