本文整理汇总了Scala中spray.http.StatusCodes类的典型用法代码示例。如果您正苦于以下问题:Scala StatusCodes类的具体用法?Scala StatusCodes怎么用?Scala StatusCodes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StatusCodes类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1:
//设置package包名称以及导入依赖的类
package org.packtpublishing.web
import spray.routing.HttpService
trait ApiDocsUi {
this: HttpService =>
import spray.http.StatusCodes
import spray.routing._
import Directives._
val swaggerUiPath = "META-INF/resources/webjars/swagger-ui/2.1.4"
val apiDocsUiRoutes = get {
pathEndOrSingleSlash {
requestUri { uri =>
parameters('url.?) {
case Some(url) => getFromResource(s"$swaggerUiPath/index.html")
case None => redirect(uri.copy(query = uri.query.+:("url", "/api-docs")), StatusCodes.PermanentRedirect)
}
}
} ~ getFromResourceDirectory(s"$swaggerUiPath/")
}
}
示例2: SbtServePlugin
//设置package包名称以及导入依赖的类
package com.github.michalsenkyr.sbt.serve
import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
import sbt.Keys._
import sbt._
import spray.http.StatusCodes
import spray.routing.SimpleRoutingApp
object SbtServePlugin extends AutoPlugin with SimpleRoutingApp {
private val cl = getClass.getClassLoader
implicit val system = ActorSystem("sbt-serve", ConfigFactory.load(cl), cl)
object autoImport {
val port = settingKey[Int]("Port")
val serve = taskKey[Unit]("Serve static files")
// val packagePath = settingKey[File]("Path inside package")
}
import autoImport._
override lazy val projectSettings = Seq(
port in serve := 8000,
sourceDirectory in serve := file("src/main/web"),
serve := serveTask.value
// packagePath in serve := new File("web")
)
lazy val serveTask =
Def.task {
val srcDir = baseDirectory.value / (sourceDirectory in serve).value.getPath
if (!srcDir.exists) throw new IllegalStateException("Source directory does not exist")
startServer(interface = "localhost", port = (port in serve).value) {
pathSingleSlash {
redirect("/index.html", StatusCodes.PermanentRedirect)
} ~ get {
compressResponse() {
getFromDirectory(srcDir.getPath)
}
}
}
}
}
示例3: handleErrors
//设置package包名称以及导入依赖的类
package com.github.mijicd.waes.api
import com.github.mijicd.waes.representations.{Error, JsonFormats}
import spray.http.StatusCodes
import spray.routing.{ExceptionHandler, HttpService, MalformedRequestContentRejection, RejectionHandler}
import spray.util.LoggingContext
trait FailureHandlers {
this: HttpService =>
import JsonFormats._
implicit def handleErrors(implicit log: LoggingContext) =
ExceptionHandler {
case e: IllegalArgumentException =>
log.warning(s"Failed to complete request. Cause: ${e.getMessage}")
complete(StatusCodes.BadRequest, Error(e.getMessage))
case t: Throwable =>
log.error(s"Failed to complete request. Cause: ${t.getMessage}")
complete(StatusCodes.InternalServerError, Error(t.getMessage))
}
implicit def handleRejections(implicit log: LoggingContext) =
RejectionHandler {
case MalformedRequestContentRejection(msg, cause) :: Nil =>
log.warning(s"Invalid content submitted. Cause: $msg")
complete(StatusCodes.BadRequest, Error(msg))
case _ =>
complete(StatusCodes.InternalServerError, Error("Server-side error occurred."))
}
}
示例4: redirectToHttps
//设置package包名称以及导入依赖的类
package com.biosimilarity.evaluator.spray.directives
import com.biosimilarity.evaluator.distribution.EvalConfigWrapper
import spray.http.{HttpHeader, HttpHeaders, StatusCodes, Uri}
import spray.routing.Directives._
import spray.routing.{Directive0, RequestContext}
trait HttpsDirectives {
import HttpsDirectives._
def redirectToHttps: Directive0 = requestUri.flatMap { (uri: Uri) =>
redirect(uri.withScheme("https").withPort(EvalConfigWrapper.serverSSLPort), StatusCodes.MovedPermanently)
}
def requireHttps: Directive0 =
respondWithHeader(StrictTransportSecurity) &
extract(isHttpsRequest).flatMap { (b: Boolean) =>
if (b) pass
else redirectToHttps
}
def requireHttpsIf(p: => Boolean): Directive0 =
if (p) requireHttps
else pass
}
object HttpsDirectives {
val StrictTransportSecurity = HttpHeaders.RawHeader("Strict-Transport-Security", "max-age=31536000")
def isHttpsRequest(ctx: RequestContext): Boolean =
ctx.request.uri.scheme == "https" || ctx.request.headers.exists { (h: HttpHeader) =>
h.is("x-forwarded-proto") && h.value == "https"
}
}
示例5: ApiActor
//设置package包名称以及导入依赖的类
package org.xiaon
package hashservice
package http
import akka.actor.{ ActorRef, Props, ActorLogging }
import spray.routing.{ RequestContext, Route, HttpService, HttpServiceActor }
import spray.http.StatusCodes
import spray.httpx.SprayJsonSupport._
import domain.SimpleJob
import processor.ProcessingActor
class ApiActor(settings: Settings) extends HttpServiceActor with HttpService with ActorLogging {
import ApiActor._
import ProcessingActor._
private[http] val timeout = settings.Timeout
def receive = runRoute(route)
private[hashservice] def route: Route = {
path(Ping) {
get {
complete(StatusCodes.OK -> "pong")
}
} ~
pathPrefix(Api) {
path(Service) {
post {
entity(as[SimpleJob]) { job =>
requestContext =>
val processor = createProcessor(requestContext)
processor ! job
}
}
}
}
}
private[hashservice] def createProcessor(ctx: RequestContext): ActorRef =
context.actorOf(Props(new ProcessingActor(timeout, resultCallback = completeJobRequest(ctx))))
private[hashservice] def completeJobRequest(ctx: RequestContext)(result: JobResult): Unit = {
result match {
case Right(job) => ctx.complete(StatusCodes.Accepted -> job)
case Left(error) => ctx.complete(StatusCodes.ServiceUnavailable -> error)
}
}
}
object ApiActor {
private[hashservice] val Ping = "ping"
private[hashservice] val Api = "api"
private[hashservice] val Service = "service"
}