本文整理汇总了Scala中akka.http.scaladsl.server.Directives.get类的典型用法代码示例。如果您正苦于以下问题:Scala get类的具体用法?Scala get怎么用?Scala get使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了get类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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:
//设置package包名称以及导入依赖的类
package com.gitmining.datastore.service.routes
import akka.http.scaladsl.model.{ContentTypes, HttpEntity}
import akka.http.scaladsl.server.Directives.{as, complete, completeWith, delete, entity, get, instanceOf, path, pathPrefix, post}
import com.gitmining.datastore.dao.{JsonFormats, Repo, User}
import akka.http.scaladsl.server.Directives._
import com.gitmining.datastore.database.cassandra.CassandraDatabaseProvider
trait DataStoreServiceRoutes extends Routes with JsonFormats with CassandraDatabaseProvider {
override val routes =
pathPrefix("users") {
(get & path(LongNumber)) { userId =>
complete(db.Users.byId(userId))
} ~
(post & entity(as[List[User]])) { users =>
//TODO: save users
database.save(users.head)
complete {
"""Users saved"""
}
} ~
(delete & path(LongNumber)) { userId =>
complete {
//TODO: delete user with given id
"Delete user not implemented yet"
}
}
} ~
pathPrefix("repos") {
(get & path(LongNumber)) { repoId =>
complete(db.Repos.byId(repoId))
} ~
(post & entity(as[List[Repo]])) { repos =>
//TODO: save repos
complete {
"""Repos saved"""
}
}
}
}
示例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)
}
示例6: Server
//设置package包名称以及导入依赖的类
package org.narrativeandplay.hypedyn.server
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Directives.{extractUnmatchedPath, get, getFromFile}
import akka.stream.ActorMaterializer
import org.narrativeandplay.hypedyn.logging.Logger
object Server {
private var _storyPath = ""
private val hostname = "localhost"
private var port = -1 // -1 represents an uninitialised port
implicit val webserver = ActorSystem("hypedyn")
implicit val materializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext = webserver.dispatcher
// not sure if this is a security risk
val route =
extractUnmatchedPath { p =>
get {
getFromFile(_storyPath + p.toString)
}
}
// We bind to port 0 to let Akka randomly pick an available port to bind to
private val bindingFuture = Http().bindAndHandle(route, hostname, 0)
bindingFuture.onFailure {
case ex: Exception =>
Logger.error("Server failed to start: ", ex)
}
bindingFuture.onSuccess {
case binding =>
port = binding.localAddress.getPort
Logger.info(s"Server online at $address")
}
def shutdown(): Unit = {
bindingFuture flatMap (_.unbind()) onComplete { _ => webserver.terminate() }
}
def storyPath = _storyPath
def storyPath_=(s: String) = _storyPath = s
def address = s"http://$hostname:$port"
}