[api] [refactor] Move http handler into middleware. Added basic tests.