Integrating Angel with Existing Infrastructure
Let’s be honest here — migrating existing applications to a new stack sucks. But it shouldn’t have to. The issue here is that most of the time, different frameworks are separated by core differences in philosophy, paradigm or implementation, and are simply incompatible with each other.
Much of Dart’s server-side ecosystem revolves around shelf
, and dozens of shelf
packages are available for download on Pub. It would be crazy to ask people to ditch the work they have already produced by rewriting it based on another framework. So why rewrite it at all? With Angel, we try to alleviate the stress of migration by eliminating it entirely.
By far the easiest way to incorporate an existing shelf
application into Angel is to use the integration middleware. This middleware is best suited for applications mounted on the root level, and to which Angel routes will merely be added. If your application strongly depends on relative paths (for example, mounting an old server at /api/v1
), then this is not what you are looking for.
import 'dart:io';
import 'package:angel_framework/angel_framework.dart';
import 'package:angel_shelf/angel_shelf.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'api/api.dart';
main() async {
final app = new Angel();
// Angel routes on top
await app.configure(new ApiController());
// Re-route all other traffic to an
// existing shelf application.
app.after.add(embedShelf(
new shelf.Pipeline()
.addMiddleware(shelf.logRequests())
.addHandler(_echoRequest)
));
await app.startServer(InternetAddress.LOOPBACK_IP_V4, 3000);
}
But what if your current API assumes it is mounted at the root, even though you want it to function from another virtual directory, such as /api/v1
? Your other option is to shuttle it through a reverse proxy. Angel’s reverse proxy plug-in supports path mappings, so it will work perfectly in this case.
import 'package:angel_proxy/angel_proxy.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'my_old_api.dart';/// Mount old app on `/api/v1`
myPlugin(Angel app) async {
var server = await io.serve(myOldApiHandler);
// The `publicPath` option is the important one here. ;)
await app.configure(
new ProxyLayer(server.address, server.port, publicPath: '/api/v1'));
}
Don’t hesitate to try Angel. And if you like it, you can easily integrate it with your previous application without any hassle!
You’ve probably noticed just by looking at the boilerplate that there is much more to the Angel framework than just shelf
integration and reverse proxies. Go ahead and explore it for yourself. Feedback is greatly appreciated, as the library is far from perfect.