2 つのサービスを作成し、 orElseを使用して 2 つのサービスを結合させたいと考えています。つまり、service_one または service_two です。それらはすべて PartialFunction です。
サービス 1 は次のとおりです。
val usersService = HttpService {
case request @ GET -> Root / "users" / IntVar(userId) =>
Ok("test")
}
サービス2は次のとおりです。
val versionService = HttpService{
case req @ GET -> Root / "version" => {
val jsonmap = ("origin" -> req.remoteAddr.getOrElse("unknown ip"))
Ok(compact(render(jsonmap)))
}
}
そして、私は一緒に結合したい.
val service = userService orElse versionService //the error happens here.
エラーは次のとおりです。
[error] F:\workspace\frankcheckAPI\src\main\scala\com\cardaccess\ServiceApp.scala:46: value orElse is not a member of org.http4s.HttpService
[error] val service = usersService orElse versionService
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
ケースが最初のサービスのパスと一致する場合は最初のサービスが機能し、ケースが2番目のサービスのパスと一致する場合は、結合してから作成する方法が機能します。2回目のサービス作業。
ps:関係者として:
HttpService オブジェクトは次のように定義されます。
type HttpService = Service[Request, Response]
object HttpService {
/** Alternative application which lifts a partial function to an `HttpService`,
* answering with a [[Response]] with status [[Status.NotFound]] for any requests
* where the function is undefined.
*/
def apply(pf: PartialFunction[Request, Task[Response]], default: HttpService = empty): HttpService =
Service.lift(req => pf.applyOrElse(req, default))
...
}