diff --git a/examples/plugins/example/main.go b/examples/plugins/example/main.go index f791f0b02..afb358b2f 100644 --- a/examples/plugins/example/main.go +++ b/examples/plugins/example/main.go @@ -92,7 +92,19 @@ func handleStatus(c *echo.Context) error { }) } -func NewPlugin() plugins.Plugin { return &ExamplePlugin{} } +// singleton ensures all factory functions return the same instance so that state +// initialized in Init() (e.g. event listeners, DB connections) is available to +// route handlers and other capabilities. +var singleton = &ExamplePlugin{} + +func NewPlugin() plugins.Plugin { return singleton } + +// Typed factory functions for Yaegi compatibility. +// Yaegi wraps return values per the declared return type, so sub-interface type +// assertions (Plugin -> AuthenticatedRouterPlugin) don't work. These typed +// factories ensure Yaegi wraps the value with the correct interface wrapper. +func NewAuthenticatedRouterPlugin() plugins.AuthenticatedRouterPlugin { return singleton } +func NewUnauthenticatedRouterPlugin() plugins.UnauthenticatedRouterPlugin { return singleton } type TestListener struct{}