From accc2ad05fa1d62f0737c593e53bc7a8dccb9a65 Mon Sep 17 00:00:00 2001 From: devmanishofficial Date: Mon, 29 Dec 2025 23:06:26 +0530 Subject: [PATCH 1/2] docs(go): add middleware documentation and link in README Signed-off-by: devmanishofficial --- go/MIDDLEWARE.md | 36 ++++++++++++++++++++++++++++++++++++ go/README.md | 2 ++ 2 files changed, 38 insertions(+) create mode 100644 go/MIDDLEWARE.md diff --git a/go/MIDDLEWARE.md b/go/MIDDLEWARE.md new file mode 100644 index 0000000000..fe055b6687 --- /dev/null +++ b/go/MIDDLEWARE.md @@ -0,0 +1,36 @@ +# 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" + "fmt" + "log" + + "github.com/firebase/genkit/go/core" + "github.com/firebase/genkit/go/genkit" +) + +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..bed50e0f7f 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.c \ No newline at end of file From eee8bc34631efad34a62e1641cc566825d261dbf Mon Sep 17 00:00:00 2001 From: devmanishofficial Date: Tue, 30 Dec 2025 00:21:27 +0530 Subject: [PATCH 2/2] docs(go): add middleware documentation and examples Signed-off-by: devmanishofficial --- go/MIDDLEWARE.md | 2 -- go/README.md | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/go/MIDDLEWARE.md b/go/MIDDLEWARE.md index fe055b6687..1efbf102c0 100644 --- a/go/MIDDLEWARE.md +++ b/go/MIDDLEWARE.md @@ -13,11 +13,9 @@ package main import ( "context" - "fmt" "log" "github.com/firebase/genkit/go/core" - "github.com/firebase/genkit/go/genkit" ) func MyMiddleware[In, Out, Stream any](next core.StreamingFunc[In, Out, Stream]) core.StreamingFunc[In, Out, Stream] { diff --git a/go/README.md b/go/README.md index bed50e0f7f..22d1534f14 100644 --- a/go/README.md +++ b/go/README.md @@ -3,4 +3,4 @@ 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.c \ No newline at end of file +- [Middleware Guide](MIDDLEWARE.md): Learn how to add logging, authentication, and custom logic to your flows. \ No newline at end of file