add base class for middlewares
refactor middleware to use before/after
simplifies a bit and also allows for usage with pipeline