ioHandler function
Future<void> Function(HttpRequest request)
ioHandler(
- Action action, {
- ContextProvider? contextProvider,
- bool sendLegacyErrorFrame = false,
Serves a single action as a dart:io request handler, the dart:io
counterpart of Go's genkit.Handler.
final handleHello = ioHandler(helloFlow, contextProvider: bearerAuth);
await for (final request in server) {
if (request.uri.path == '/hello') {
await handleHello(request);
}
}
The handler answers every request it gets (non-POST requests with 405);
routing is up to the caller. See GenkitRouter to serve several actions.
See actionHandler for sendLegacyErrorFrame. It is independent of any
GenkitRouter's setting, so match it if you use both.
Implementation
Future<void> Function(HttpRequest request) ioHandler(
Action action, {
ContextProvider? contextProvider,
bool sendLegacyErrorFrame = false,
}) {
final handler = actionHandler(
action,
contextProvider: contextProvider,
sendLegacyErrorFrame: sendLegacyErrorFrame,
);
return (HttpRequest request) async {
final response = await handler(
// The action doesn't route on the path, so a bad escape is harmless;
// pass the raw path through rather than reject the request.
toGenkitHttpRequest(
request,
path: decodeRequestPath(request.uri.path) ?? request.uri.path,
),
);
await writeHttpResponse(response, request.response);
};
}