本文整理汇总了Scala中akka.http.scaladsl.server.Directives.path类的典型用法代码示例。如果您正苦于以下问题:Scala path类的具体用法?Scala path怎么用?Scala path使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了path类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: WebServer
//设置package包名称以及导入依赖的类
package com.example
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives.{complete, get, path}
import akka.stream.ActorMaterializer
import scala.io.StdIn
object WebServer extends App {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = system.dispatcher
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
示例2: CovarianceInput
//设置package包名称以及导入依赖的类
package org.openalgo.analytics
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes.OK
import akka.http.scaladsl.server.Directives.{as, complete, decodeRequest, entity, path, post}
import akka.stream.ActorMaterializer
import argonaut.Argonaut.casecodec2
import com.typesafe.config.ConfigFactory
import de.heikoseeberger.akkahttpargonaut.ArgonautSupport
import scala.collection.JavaConverters._
import scala.collection.mutable
case class CovarianceInput(series1: mutable.Buffer[java.lang.Double], series2: mutable.Buffer[java.lang.Double])
object AppMain extends App with ArgonautSupport {
val config = ConfigFactory.load()
implicit def PersonCodecJson =
casecodec2(CovarianceInput.apply, CovarianceInput.unapply)("series1", "series2")
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val ec = system.dispatcher
val route =
path("covariance") {
post {
decodeRequest {
entity(as[CovarianceInput]) { json: CovarianceInput =>
complete(OK, CovarianceCall.getCovariance(json.series1.asJava, json.series2.asJava))
}
}
}
}
val bindingFuture = Http().bindAndHandle(route, config.getString("http.interface"), config.getInt("http.port"))
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
Console.readLine() // for the future transformations
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ ? system.terminate()) // and shutdown when done
}
示例3: WebServer2
//设置package包名称以及导入依赖的类
package com.wincom.http
import scala.io.StdIn
import scala.util.Random
import akka.actor.ActorSystem
import akka.http.scaladsl.marshalling.ToResponseMarshallable.apply
import akka.http.scaladsl.model.ContentTypes
import akka.http.scaladsl.model.HttpEntity
import akka.http.scaladsl.server.Directive.addByNameNullaryApply
import akka.http.scaladsl.server.Directives._segmentStringToPathMatcher
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.server.Directives.get
import akka.http.scaladsl.server.Directives.path
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import akka.util.ByteString
import akka.http.scaladsl.Http
object WebServer2 {
def main(args: Array[String]) {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val numbers = Source.fromIterator(() =>
Iterator.continually(Random.nextInt()))
val route =
path("random") {
get {
complete(
HttpEntity(
ContentTypes.`text/plain(UTF-8)`,
numbers.map(n => ByteString(s"$n\n"))))
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine()
bindingFuture
.flatMap(_.unbind())
.onComplete(_ => system.terminate())
}
}
示例4: WebServer
//设置package包名称以及导入依赖的类
package actor
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.ToResponseMarshallable.apply
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directive.addByNameNullaryApply
import akka.http.scaladsl.server.Directives.{_segmentStringToPathMatcher, complete, get, path}
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import akka.stream.ActorMaterializer
import com.typesafe.config.ConfigFactory
import scala.io.StdIn
object WebServer {
def main_(args: Array[String]) {
implicit val seedConfig = ConfigFactory.load("dcim-cluster")
implicit val seedSystem = ActorSystem("dcim", seedConfig)
implicit val materializer = ActorMaterializer()
implicit val executionContext = seedSystem.dispatcher
val route =
path("hello") {
get {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>Say hello to akka-http</h1>"))
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => seedSystem.terminate()) // and shutdown when done
}
}
示例5: searchClient
//设置package包名称以及导入依赖的类
package houseprices.search
import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.Http
import akka.http.scaladsl.marshalling.ToResponseMarshallable.apply
import akka.http.scaladsl.server.Directive.addByNameNullaryApply
import akka.http.scaladsl.server.Directives.complete
import akka.http.scaladsl.server.Directives.get
import akka.http.scaladsl.server.Directives.path
import akka.http.scaladsl.server.Directives.segmentStringToPathMatcher
import akka.http.scaladsl.server.RouteResult.route2HandlerFlow
import akka.stream.ActorMaterializer
import houseprices.search.model._
import houseprices.search.model.SearchResultJsonProtocol._
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import scala.concurrent.ExecutionContext
import spray.json.DefaultJsonProtocol
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
trait SearchJsonProtocol extends SprayJsonSupport with DefaultJsonProtocol {
implicit val format2 = jsonFormat3(PricePaidData)
implicit val format1 = jsonFormat2(SearchResult)
}
trait HousePriceSearchService extends SearchJsonProtocol {
def searchClient: SearchClient
implicit val ec: ExecutionContext
val routes =
path("search" / ".*".r) { text =>
get {
complete {
searchClient.search(Query(text))
}
}
}
}
class HousePriceSearchServer(val searchClient: SearchClient)(implicit val system: ActorSystem, implicit val materializer: ActorMaterializer) extends HousePriceSearchService {
val log = Logging(system, getClass)
implicit val ec = system.dispatcher
def startServer(interface: String, port: Int) = {
Http().bindAndHandle(routes, interface, port)
log.info("Search server ready at {}:{}", interface, port)
this
}
}
object DevHousePriceSearchServer extends App {
implicit val system = ActorSystem("housePriceSearchServer")
implicit val materializer = ActorMaterializer()
val searchClient = HttpSearchClient(system)
new HousePriceSearchServer(searchClient).startServer("localhost", 8080)
}