allow specifying the lifecycle for middleware (gh-10)

not super happy about the api yet, but the functionality seems to work
added failing test case for prepare_app
move prepare_app call to just after instantiation (fixes gh-13)

previously it was being called as part of to_app, which is called every
time the middleware is applied. now it is only called every time the
middleware is instantiated (which makes a difference for singleton
middleware).