diff --git a/go/MIDDLEWARE.md b/go/MIDDLEWARE.md new file mode 100644 index 0000000000..1efbf102c0 --- /dev/null +++ b/go/MIDDLEWARE.md @@ -0,0 +1,34 @@ +# Genkit Middleware (Go) + +Middleware in Genkit allows you to wrap your Flows (or any Action) with custom logic. This is useful for cross-cutting concerns like logging, authentication, validation, or modifying inputs/outputs. + +Middleware is defined as a function that takes a `StreamingFunc` and returns a `StreamingFunc`. + +## Defining Middleware + +A middleware function typically follows this pattern: + +```go +package main + +import ( + "context" + "log" + + "github.com/firebase/genkit/go/core" +) + +func MyMiddleware[In, Out, Stream any](next core.StreamingFunc[In, Out, Stream]) core.StreamingFunc[In, Out, Stream] { + return func(ctx context.Context, input In, cb func(context.Context, Stream) error) (Out, error) { + // 1. Logic BEFORE the Flow runs + // e.g., validate input, start timer, check auth + + // 2. Call the next handler in the chain + output, err := next(ctx, input, cb) + + // 3. Logic AFTER the Flow runs + // e.g., log success/failure, modify output + + return output, err + } +} \ No newline at end of file diff --git a/go/README.md b/go/README.md index 67c96adf86..22d1534f14 100644 --- a/go/README.md +++ b/go/README.md @@ -2,3 +2,5 @@ This package is the Go version of Genkit, a framework for building AI-powered apps. See: https://genkit.dev/go/docs/get-started-go + +- [Middleware Guide](MIDDLEWARE.md): Learn how to add logging, authentication, and custom logic to your flows. \ No newline at end of file