本文整理汇总了Scala中com.twitter.finagle.Service类的典型用法代码示例。如果您正苦于以下问题:Scala Service类的具体用法?Scala Service怎么用?Scala Service使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Service类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: Foo2
//设置package包名称以及导入依赖的类
package com.bluedit
import com.twitter.finagle.http.{Response, Request}
import com.twitter.finagle.{Service, Http}
import com.twitter.util.Await
import io.finch._
import io.finch.circe._
import io.circe.generic.auto._
import shapeless.{CNil, :+:}
import io.circe._, io.circe.generic.auto._, io.circe.syntax._
case class Foo2( f: String )
case class Bar2( b: String )
case class Person2( firstname: String, lastname: String )
object HelloService4 extends App {
val foo2: Endpoint[Foo2] = get("foo" / string ) {
name: String => Ok( Foo2(name))
}
val bar2: Endpoint[Bar2] = get("bar" / string ) {
name: String => Ok( Bar2(name))
}
val person2: Endpoint[Person2] = get("person" / string ) {
name: String => Ok( Person2( name, "Welkenbach"))
}
val api: Service[Request, Response] = (foo2 :+: bar2 :+: person2).toService
Await.ready(Http.server.serve(":9001", api ))
}
示例2: WhichUserLogin
//设置package包名称以及导入依赖的类
package com.bob.reservefund.scala.Filter
import javax.inject.Inject
import com.twitter.finagle.filter.LogFormatter
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.inject.Logging
import com.twitter.util.{Stopwatch, Future}
case class WhichUserLogin(id: Long)
object WhichUserLoginContext {
private val UserField = Request.Schema.newField[WhichUserLogin]()
class RequestAopFilter[R <: Request] @Inject()(
logFormatter: LogFormatter[R, Response])
extends SimpleFilter[R, Response] with Logging {
override def apply(request: R, service: Service[R, Response]): Future[Response] = {
if (!isInfoEnabled) {
service(request)
}
else {
val elapsed = Stopwatch.start()
service(request) onSuccess { response =>
info(response.contentString)
} onFailure { e =>
// should never get here since this filter is meant to be after the exception barrier
info(logFormatter.formatException(request, e, elapsed()))
}
}
}
}
示例3: WebServer
//设置package包名称以及导入依赖的类
package com.gilesc
package mynab
package finch
import com.gilesc.mynab.finch.endpoint.AccountEndpoints
import com.gilesc.mynab.finch.endpoint.AccountGroupEndpoints
object WebServer {
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Http, Service}
import com.twitter.util.Await
import io.circe.generic.auto._
import io.finch._
import io.finch.circe._
// Endpoints
val api: Service[Request, Response] = (
AccountGroupEndpoints.postGroup :+:
AccountGroupEndpoints.getGroup :+:
AccountEndpoints.getAccount :+:
AccountEndpoints.postAccount
).handle({
case e => NotFound(new Exception(e.toString))
}).toServiceAs[Application.Json]
// Service Stuff
def shutdown(): Unit = {}
def main(args: Array[String]): Unit = {
val server = Http.server.serve(":8080", api)
Await.ready(server)
shutdown()
}
}
示例4: FibonacciSequenceService
//设置package包名称以及导入依赖的类
package services
import algorithms.sequences.{FibonacciFinder, MatricesFibonacciFinder}
import com.twitter.finagle.Service
import com.twitter.finagle.httpx.{Request, Response}
import com.twitter.util.Future
import io.finch.request.param
import io.finch.response._
object FibonacciSequenceService extends Service[Request, Response] {
val fibonacciFinder: FibonacciFinder = new MatricesFibonacciFinder()
override def apply(request: Request): Future[Response] = {
for {
termNumber <- param("termNumber")(request).map(_.toInt)
} yield {
val result = fibonacciFinder.findNthTermInSequence(termNumber)
Ok(s"$result")
}
}
}
示例5: BinomialService
//设置package包名称以及导入依赖的类
package services
import algorithms.BinomialCoefficient
import com.twitter.finagle.Service
import com.twitter.finagle.httpx.{Request, Response}
import com.twitter.util.Future
import io.finch.request.param
import io.finch.response._
object BinomialService extends Service[Request, Response] {
override def apply(request: Request): Future[Response] = {
for {
n <- param("n")(request).map(_.toInt)
k <- param("k")(request).map(_.toInt)
} yield {
val result = BinomialCoefficient.nChooseK(n, k)
Ok(s"$result")
}
}
}
示例6: LoggingFilter
//设置package包名称以及导入依赖的类
package io.peregrine
import com.twitter.finagle.http.{Request => FinagleRequest, Response => FinagleResponse}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.logging.Logger
import com.twitter.util._
import com.twitter.conversions.time._
class LoggingFilter extends SimpleFilter[FinagleRequest, FinagleResponse] with LoggingFilterHelper {
def apply(request: FinagleRequest, service: Service[FinagleRequest, FinagleResponse]): Future[FinagleResponse] = {
applyLogging(request, req => service(req))
}
}
trait LoggingFilterHelper extends LoggerColors {
private val logger: Logger = PeregrineLogger.logger()
private[peregrine] def applyLogging(request: FinagleRequest,
func: FinagleRequest => Future[FinagleResponse]): Future[FinagleResponse] = {
val elapsed = Stopwatch.start()
func(request) map { response =>
val duration = elapsed().inMicroseconds/1000.0
val mColor = methodColor(response.statusCode)
logger.info("[%s%s] %s%s %s\"%s\" %s%d %sin %s%.3fms%s",
ANSI_RESET, request.remoteHost,
ANSI_PURPLE, request.method,
ANSI_BLUE, request.uri,
mColor, response.statusCode,
ANSI_RESET,
ANSI_GREEN, duration,
ANSI_RESET
)
response
}
}
private[this] def methodColor(statusCode: Int) = statusCode match {
case code if code >= 200 && code < 300 => ANSI_GREEN
case code if code >= 400 && code < 500 => ANSI_YELLOW
case code if code >= 500 && code < 600 => ANSI_RED
case _ => ANSI_RESET
}
}
示例7: HelloFinagleServer
//设置package包名称以及导入依赖的类
package jp.gr.java_conf.massakai.application
import com.twitter.finagle.{Http, Service, http}
import com.twitter.util.{Await, Future}
object HelloFinagleServer extends App {
val service = new Service[http.Request, http.Response] {
def apply(req: http.Request): Future[http.Response] =
Future.value(
http.Response(req.version, http.Status.Ok)
)
}
val server = Http.serve(":8080", service)
Await.ready(server)
}
示例8: HelloFinagleClient
//设置package包名称以及导入依赖的类
package jp.gr.java_conf.massakai.application
import com.twitter.finagle.{Http, Service, http}
import com.twitter.util.{Await, Future}
object HelloFinagleClient extends App {
val client: Service[http.Request, http.Response] = Http.newService("www.scala-lang.org:80")
val request = http.Request(http.Method.Get, "/")
request.host = "www.scala-lang.org"
val response: Future[http.Response] = client(request)
response.onSuccess { resp: http.Response =>
println("GET success: " + resp)
}
Await.ready(response)
}
示例9: YaasHeadersTenantConsistentWithRouteFilter
//设置package包名称以及导入依赖的类
package me.kamkor.yaas.http.filters
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future
import me.kamkor.yaas.http.exceptions.ValidationException
import me.kamkor.yaas.http.filters.YaasHeadersContext._
class YaasHeadersTenantConsistentWithRouteFilter(routeParamName: String) extends SimpleFilter[Request, Response] {
def this() = this("tenant")
override def apply(request: Request, service: Service[Request, Response]): Future[Response] = {
checkIfHeaderTenantConsistentWithRoute(request)
service(request)
}
private def checkIfHeaderTenantConsistentWithRoute(request: Request): Unit =
Option(request.getParam(routeParamName)) foreach { tenantRouteParam =>
if (request.yaasHeaders.hybrisTenant != tenantRouteParam) {
throw new ValidationException(s"'hybris-tenant' header must match route param '$routeParamName'")
}
}
}
开发者ID:kamkor,项目名称:yaas-wishlist-service,代码行数:26,代码来源:YaasHeadersTenantConsistentWithRouteFilter.scala
示例10: YaasHeaders
//设置package包名称以及导入依赖的类
package me.kamkor.yaas.http.filters
import com.twitter.finagle.http.{Request, Response}
import com.twitter.finagle.{Service, SimpleFilter}
import com.twitter.util.Future
object YaasHeaders {
val HybrisTenant = "hybris-tenant"
}
case class YaasHeaders(hybrisTenant: String)
class YaasHeadersFilter extends SimpleFilter[Request, Response] {
override def apply(request: Request, service: Service[Request, Response]): Future[Response] = {
YaasHeadersContext.setYaasHeaders(request)
service(request)
}
}
// create a context, see https://twitter.github.io/finatra/user-guide/build-new-http-server/filter.html
object YaasHeadersContext {
private val YaasHeadersField = Request.Schema.newField[YaasHeaders]
implicit class YaasAwareContextSyntax(val request: Request) extends AnyVal {
def yaasHeaders: YaasHeaders = request.ctx(YaasHeadersField)
}
private[filters] def setYaasHeaders(request: Request): Unit = {
val hybrisTenant = request.headerMap(YaasHeaders.HybrisTenant)
val yaasAware = YaasHeaders(hybrisTenant)
request.ctx.update(YaasHeadersField, yaasAware)
}
}
示例11: UserFilter
//设置package包名称以及导入依赖的类
package com.example
import com.example.db.DB
import com.google.inject.Inject
import com.twitter.finagle.http.{Request, Response, Status}
import com.twitter.finagle.{Filter, Service, SimpleFilter}
import com.twitter.util.Future
class UserFilter @Inject() extends SimpleFilter[Request, Response] {
override def apply(request: Request, service: Service[Request, Response]): Future[Response] = {
val token = request.cookies.get("auth_token")
DB.isValidToken(token.map(x => x.value).getOrElse("")) flatMap {
case true => service(request)
case false => redirectToLoginPage
}
}
def redirectToLoginPage = Future {
val response = Response(Status.TemporaryRedirect)
response.location = "/loginform"
response
}
}
示例12: ThriftServer
//设置package包名称以及导入依赖的类
package com.example
import com.twitter.finagle.example.thriftscala.Hello
import com.twitter.finagle.example.thriftscala.Hello.Hi.{Args, Result}
import com.twitter.finagle.{Service, SimpleFilter, Thrift}
import com.twitter.util.{Await, Future}
object ThriftServer {
def main(args: Array[String]) {
val filter = new SimpleFilter[Hello.Hi.Args, Hello.Hi.Result] {
override def apply(request: Args, service: Service[Args, Result]): Future[Result] = {
println("hahaha")
service(request)
}
}
val hiService = new Service[Hello.Hi.Args, Hello.Hi.Result] {
override def apply(request: Hello.Hi.Args): Future[Hello.Hi.Result] = {
Future(Hello.Hi.Result(Some("hi!")))
}
}
val helloService = new Service[Hello.Hello.Args, Hello.Hello.Result] {
override def apply(request: Hello.Hello.Args): Future[Hello.Hello.Result] = {
Future(Hello.Hello.Result(Some("hello!")))
}
}
val serviceImpl = Hello.ServiceIface(hi = filter andThen hiService, hello = helloService)
val server = Thrift.serveIface("localhost:8081", Hello.MethodIfaceBuilder.newMethodIface(serviceImpl))
Await.ready(server)
}
}
示例13: ThriftClient
//设置package包名称以及导入依赖的类
package com.example
import com.twitter.finagle.{Service, SimpleFilter, Thrift}
import com.twitter.finagle.example.thriftscala.Hello
import com.twitter.finagle.thrift.{ClientId, ThriftClientRequest}
import com.twitter.util.{Await, Future}
object ThriftClient {
def main(args: Array[String]) {
val client = Thrift.client
.withClientId(ClientId("CLIENT ID HARD TO MISS"))
.filtered(new SimpleFilter[ThriftClientRequest, Array[Byte]] {
override def apply(request: ThriftClientRequest, service: Service[ThriftClientRequest, Array[Byte]]): Future[Array[Byte]] = {
println("Filtered")
service(request)
}
})
.newIface[Hello.FutureIface]("localhost:8081", "SPECIAL LABEL HARD TO MISS")
println(Await.result(client.hi()))
}
}
示例14: ThriftMuxGlobalServer
//设置package包名称以及导入依赖的类
package com.example
import com.twitter.finagle.example.thriftscala.Hello
import com.twitter.finagle.example.thriftscala.Hello.Hi.{Args, Result}
import com.twitter.finagle.thrift.ClientId
import com.twitter.finagle.{Service, SimpleFilter, Thrift, ThriftMux}
import com.twitter.util.{Await, Future}
object ThriftMuxGlobalServer {
def main(args: Array[String]) {
val iface = new Hello.FutureIface {
def hi() = {
println("received request")
println(s"Client id: ${ClientId.current}")
Future("hi there")
}
override def hello(): Future[String] = {
Future("hello there")
}
}
val filter = new SimpleFilter[Array[Byte], Array[Byte]] {
override def apply(request: Array[Byte], service: Service[Array[Byte], Array[Byte]]): Future[Array[Byte]] = {
println("filtered")
service(request)
}
}
val service = new Hello.FinagledService(iface, Thrift.protocolFactory)
val server = ThriftMux.serve("localhost:8081", filter andThen service)
Await.ready(server)
}
}
示例15: ThriftMuxServer
//设置package包名称以及导入依赖的类
package com.example
import com.twitter.finagle.example.thriftscala.Hello
import com.twitter.finagle.example.thriftscala.Hello.Hi.{Args, Result}
import com.twitter.finagle.{Service, SimpleFilter, Thrift, ThriftMux}
import com.twitter.util.{Await, Future}
object ThriftMuxServer {
def main(args: Array[String]) {
val filter = new SimpleFilter[Hello.Hi.Args, Hello.Hi.Result] {
override def apply(request: Args, service: Service[Args, Result]): Future[Result] = {
println("hahaha")
service(request)
}
}
val hiService = new Service[Hello.Hi.Args, Hello.Hi.Result] {
override def apply(request: Hello.Hi.Args): Future[Hello.Hi.Result] = {
Future(Hello.Hi.Result(Some("hi!")))
}
}
val helloService = new Service[Hello.Hello.Args, Hello.Hello.Result] {
override def apply(request: Hello.Hello.Args): Future[Hello.Hello.Result] = {
Future(Hello.Hello.Result(Some("hello!")))
}
}
val serviceImpl = Hello.ServiceIface(hi = filter andThen hiService, hello = helloService)
val server = ThriftMux.serveIface("localhost:8081", Hello.MethodIfaceBuilder.newMethodIface(serviceImpl))
Await.ready(server)
}
}