Add very rudimentary middleware implementation.

This is the starting point for #1.