当前位置: 首页>>代码示例>>Scala>>正文


Scala Service类代码示例

本文整理汇总了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 ))
} 
开发者ID:getmetorajesh,项目名称:rj_reddit_slick_finch,代码行数:34,代码来源:test.scala

示例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()))
      }
    }
  }

} 
开发者ID:bobxwang,项目名称:ReserveFundService,代码行数:37,代码来源:WhichUserLoginFilter.scala

示例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()
  }
} 
开发者ID:CraigGiles,项目名称:mynab,代码行数:35,代码来源:WebServer.scala

示例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")
    }
  }
} 
开发者ID:willb611,项目名称:algorithms-service,代码行数:22,代码来源:FibonacciSequenceService.scala

示例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")
    }
  }
} 
开发者ID:willb611,项目名称:algorithms-service,代码行数:22,代码来源:BinomialService.scala

示例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
  }
} 
开发者ID:pairi,项目名称:pairi,代码行数:45,代码来源:LoggingFilter.scala

示例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)
} 
开发者ID:massakai,项目名称:finagle-kafka-sample,代码行数:16,代码来源:HelloFinagleServer.scala

示例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)
} 
开发者ID:massakai,项目名称:finagle-kafka-sample,代码行数:16,代码来源:HelloFinagleClient.scala

示例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)
  }

} 
开发者ID:kamkor,项目名称:yaas-wishlist-service,代码行数:36,代码来源:YaasHeadersFilter.scala

示例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
  }
} 
开发者ID:Sergey778,项目名称:finatra_test,代码行数:27,代码来源:UserFilter.scala

示例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)
  }
} 
开发者ID:vtatai,项目名称:finagleThriftPlayground,代码行数:32,代码来源:ThriftServer.scala

示例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()))
  }
} 
开发者ID:vtatai,项目名称:finagleThriftPlayground,代码行数:22,代码来源:ThriftClient.scala

示例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)
  }
} 
开发者ID:vtatai,项目名称:finagleThriftPlayground,代码行数:33,代码来源:ThriftMuxGlobalServer.scala

示例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)
  }
} 
开发者ID:vtatai,项目名称:finagleThriftPlayground,代码行数:32,代码来源:ThriftMuxServer.scala


注:本文中的com.twitter.finagle.Service类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。