Documentation ¶
Index ¶
- Variables
- func IsBuildEnvirnonment() bool
- func NewBucket(name string, permissions ...BucketPermission) (storage.Bucket, error)
- func NewCollection(name string, permissions ...CollectionPermission) (documents.CollectionRef, error)
- func NewQueue(name string, permissions ...QueuePermission) (queues.Queue, error)
- func NewSchedule(name, rate string, handlers ...faas.EventMiddleware) error
- func NewSecret(name string, permissions ...SecretPermission) (secrets.SecretRef, error)
- func Run() error
- type Api
- type ApiDetails
- type ApiOption
- type BucketPermission
- type CollectionPermission
- type Details
- type JwtSecurityRule
- type Manager
- type MethodOption
- type QueuePermission
- type Route
- type SecretPermission
- type Starter
- type Topic
- type TopicPermission
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var BucketEverything []BucketPermission = []BucketPermission{BucketReading, BucketWriting, BucketDeleting}
var CollectionEverything []CollectionPermission = []CollectionPermission{CollectionReading, CollectionWriting, CollectionDeleting}
var QueueEverything []QueuePermission = []QueuePermission{QueueSending, QueueReceving}
var SecretEverything []SecretPermission = []SecretPermission{SecretReading, SecretWriting}
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:
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 ¶
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 ApiOption ¶
type ApiOption = func(api *api)
func WithSecurity ¶
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 JwtSecurityRule ¶
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.
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.
type SecretPermission ¶
type SecretPermission string
const ( SecretReading SecretPermission = "reading" SecretWriting SecretPermission = "writing" )
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" )