function

package
v1.6.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 11 Imported by: 0

README

fx/modules/bootstrap/function

fx module that runs a bootstrap/function handler through the adapter commands provided in the boostrap.function.adapters group (BSFunctionAdaptersGroupKey) — cloudevents HTTP, NATS, Pub/Sub, Kafka, AWS Lambda.

fxfact.NewApp(
	fx.Provide(func() function.Handler[*event.Event] { return handle }),
	cehttp.Module[*event.Event](),
	functionfx.Module[*event.Event](middlewares),
).Run()

T must be *event.Event or []*event.Event.

Adapter clients

The nats, cloudevents_nats and Pub/Sub v2 adapter modules (adapter/contrib/nats-io/nats.go/v1, adapter/contrib/cloudevents/sdk-go/v2/contrib/nats-io/nats.go/v1, adapter/contrib/cloud.google.com/pubsub/v2) come in two forms:

Function Client
Module[T]() brings its own: the factory fx module of *nats.Conn or *pubsub.Client
ModuleWithoutClient[T]() takes the one the app provides

Use ModuleWithoutClient when the app already provides that client, such as the default client a publisher driver or an @Inject gets: fx accepts one provider per type, so Module there fails the boot with "already provided". builder generates it that way. Both share one sync.Once: the first call wins.

The function runs the middlewares given to Module, then the []middleware.AnyErrorMiddleware[T] provided under the name MiddlewaresName (bootstrap.function.middlewares), when there is one. That is how a builder app passes the middlewares of its @Function:

fx.Provide(fx.Annotate(
	func() []middleware.AnyErrorMiddleware[*event.Event] { return mws },
	fx.ResultTags(`name:"`+functionfx.MiddlewaresName+`"`),
))

Lifecycle

  • OnStart — Function.Run runs in a goroutine, so fx.New returns and app.Run() listens for SIGINT/SIGTERM.
  • OnStop — the context passed to Run (and to the adapter command) is cancelled; the hook waits for Run to return, bounded by the stop context (fx's stop timeout), and returns the context error if it does not.
  • Run ends on its own (no stop requested) — the app is shut down with exit code 0 on success, or 1 when Run returned an error (logged).

Documentation

Index

Constants

View Source
const (
	BSFunctionAdaptersGroupKey = "boostrap.function.adapters"

	// MiddlewaresName names the optional []middleware.AnyErrorMiddleware[T]
	// the function runs after the middlewares given to Module, in order.
	// builder provides it for @Function(middlewares=[...]).
	MiddlewaresName = "bootstrap.function.middlewares"
)
View Source
const (
	Root = bootstrap.Root + ".function"
)

Variables

This section is empty.

Functions

func Module

func Module[T any](m []middleware.AnyErrorMiddleware[T]) fx.Option

Module fx module for bootstrap function. The function runs from the app's OnStart and its context is cancelled on OnStop, so app.Run handles SIGTERM gracefully; when the function ends on its own the app shuts down, with exit code 1 if it failed. The function runs m, then the middlewares named MiddlewaresName when the app provides them.

Types

This section is empty.

Directories

Path Synopsis
contrib/cloud.google.com/pubsub
Package pubsub declares, once, the config the Pub/Sub v1 and v2 function adapter fx modules share: whether the adapter is on and which version it is.
Package pubsub declares, once, the config the Pub/Sub v1 and v2 function adapter fx modules share: whether the adapter is on and which version it is.

Jump to

Keyboard shortcuts

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