• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala get类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了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
} 
开发者ID:hiroinamine,项目名称:akka-labs,代码行数:32,代码来源:WebServer.scala


示例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"""
        }
      }
    }
} 
开发者ID:tshy0931,项目名称:github-mining-datastore,代码行数:42,代码来源:DataStoreServiceRoutes.scala


示例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())
  }
} 
开发者ID:xtwxy,项目名称:akka-tests,代码行数:48,代码来源:WebServer2.scala


示例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
  }
} 
开发者ID:xtwxy,项目名称:akka-tests,代码行数:39,代码来源:WebServer.scala


示例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)
} 
开发者ID:ayubmalik,项目名称:houseprices,代码行数:57,代码来源:HousePriceSearchServer.scala


示例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"
} 
开发者ID:narrativeandplay,项目名称:hypedyn2,代码行数:52,代码来源:Server.scala



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala CsvWriterSettings类代码示例发布时间:2022-05-23
下一篇:
Scala Message类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap