I am a newbie in scala world and using http4s for developing the REST layer of my application. I am using blaze server for deploying/publishing the services. I need to mount a servlet filter or interceptor in the flow before it reaches my HttpService methods. How can I do that?
http4s library has so called middleware functionality. Middleware functionality is a wrapper around your service. For example, you have simple endpoint:
val helloWorldService = HttpService {
case GET -> Root / "hello" / name =>
Ok(s"Hello, $name.")
}
To apply middleware you may do the following:
val service: HttpService = middleware(authedService)
val authedService: AuthedService[User] =
AuthedService {
case GET -> Root / "welcome" as user => Ok(s"Welcome, ${user.name}")
}
val middleware = AuthMiddleware(authUser)
and your just need to implement your authUser
:
val authUser: Service[Request, User] = ???
You may want to check out the org.http4s.server.middleware package for already existent middleware.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.