My backend API has public and authenticated methods.
I'm trying to encapsulate everything using Cascade and Pipelines, but the cascade seems to quit immediately after the first pipeline if the route isn't found.
In short, I want all requests to be logged using the logRequests
middleware and the private requests to go through a custom middleware which validate credentials.
Here's the code:
final publicPipeline = Pipeline()
.addMiddleware(logRequests())
.addHandler(Router()
..head('/health', _health)
..post('/login', _login));
final privatePipeline = Pipeline()
.addMiddleware(_validateCredentials)
.addHandler(Router()
..head('/get_data', _getData)
..post('/logout', _logout));
final cascadeHandler = Cascade()
.add(publicPipeline)
.add(privatePipeline)
.handler;
await io.serve(cascadeHandler, '0.0.0.0', port);
Removing the Pipelines and using only Routers works as expected, but then I lose the flexibility of using Middlewares.