Documentation
¶
Overview ¶
Package bedrock provides a functional, composable framework for building and running applications.
The package is built around three core abstractions:
- Builder[T]: A generic interface for constructing application components with context support
- Runtime: An interface representing a runnable application component
- Runner[T]: An interface for executing application components built from Builders
Functional Composition ¶
Bedrock embraces functional programming patterns to enable flexible composition:
- Map: Transform builder outputs using pure functions
- Bind: Chain builders together, allowing the output of one to inform the construction of the next
These combinators allow you to build complex applications from simple, reusable components.
Basic Usage ¶
Create a builder for your application component:
builder := bedrock.BuilderFunc[MyApp](func(ctx context.Context) (MyApp, error) {
return MyApp{}, nil
})
Create a runtime by transforming the builder:
runtime := bedrock.Map(builder, func(app MyApp) (bedrock.Runtime, error) {
return bedrock.RuntimeFunc(func(ctx context.Context) error {
return app.Start(ctx)
}), nil
})
Run the application with signal handling and panic recovery:
runner := bedrock.RecoverPanics(
bedrock.NotifyOnSignal(
bedrock.DefaultRunner[bedrock.Runtime](),
os.Interrupt,
),
)
if err := runner.Run(context.Background(), runtime); err != nil {
log.Fatal(err)
}
Example ¶
buildRuntime := BuilderFunc[RuntimeFunc](func(ctx context.Context) (RuntimeFunc, error) {
rt := func(ctx context.Context) error {
fmt.Println("hello from runtime")
return nil
}
return rt, nil
})
err := NotifyOnSignal(
RecoverPanics(
DefaultRunner[RuntimeFunc](),
),
os.Interrupt,
os.Kill,
syscall.SIGTERM,
).Run(
context.Background(),
buildRuntime,
)
if err != nil {
fmt.Println(err)
return
}
Output: hello from runtime
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Builder ¶ added in v0.21.0
Builder is a generic interface for building application components.
func Bind ¶ added in v0.21.0
Bind chains two Builders together, where the output of the first is used to create the second.
func BuilderOf ¶ added in v0.21.0
BuilderOf creates a Builder that always returns the provided value.
func Map ¶ added in v0.21.0
Map transforms the output of a Builder using the provided mapper function.
func MemoizeBuilder ¶ added in v0.21.0
MemoizeBuilder wraps a Builder to cache its result after the first build. It is safe for concurrent use.
type BuilderFunc ¶ added in v0.21.0
BuilderFunc is a function type that implements the Builder interface.
type Runner ¶ added in v0.21.0
Runner is a generic interface for running application components.
func DefaultRunner ¶ added in v0.21.0
DefaultRunner returns a Runner that builds and runs the application component.
func NotifyOnSignal ¶ added in v0.21.0
NotifyOnSignal wraps a Runner to listen for specified OS signals and cancel the context when received.
func RecoverPanics ¶ added in v0.21.0
RecoverPanics wraps a Runner to recover from panics during execution and return them as errors.
type RunnerFunc ¶ added in v0.21.0
RunnerFunc is a function type that implements the Runner interface.
Directories
¶
| Path | Synopsis |
|---|---|
|
circuit
module
|
|
|
Package config provides a functional approach to reading and composing configuration values.
|
Package config provides a functional approach to reading and composing configuration values. |
|
example
|
|
|
http/rest/ecommerce
command
|
|
|
fit
module
|
|
|
host
module
|
|
|
internal
|
|
|
retry
module
|
|
|
runtime
|
|
|
http
Package http provides HTTP server runtime functionality for bedrock applications.
|
Package http provides HTTP server runtime functionality for bedrock applications. |
|
http/rest
Package rest provides a REST API builder with automatic OpenAPI v3 spec generation.
|
Package rest provides a REST API builder with automatic OpenAPI v3 spec generation. |
|
otel
Package otel provides OpenTelemetry integration for bedrock applications.
|
Package otel provides OpenTelemetry integration for bedrock applications. |
|
otel/noop
Package noop provides no-operation implementations of OpenTelemetry exporters for traces, metrics, and logs.
|
Package noop provides no-operation implementations of OpenTelemetry exporters for traces, metrics, and logs. |
|
otel/otlp
Package otlp provides bedrock Builders for creating OpenTelemetry Protocol (OTLP) exporters.
|
Package otlp provides bedrock Builders for creating OpenTelemetry Protocol (OTLP) exporters. |
|
otel/stdout
Package stdout provides bedrock builders for OpenTelemetry exporters that write telemetry data to an io.Writer.
|
Package stdout provides bedrock builders for OpenTelemetry exporters that write telemetry data to an io.Writer. |