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

Scala StatusCodes类代码示例

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

本文整理汇总了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/")
  }
} 
开发者ID:allansene,项目名称:spray-scala-akka-book-catalog,代码行数:25,代码来源:ApiDocsUi.scala


示例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)
          }
        }
      }
    }
} 
开发者ID:michalsenkyr,项目名称:sbt-serve,代码行数:47,代码来源:SbtServePlugin.scala


示例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."))
    }
} 
开发者ID:mijicd,项目名称:spray-akka-demo,代码行数:32,代码来源:FailureHandlers.scala


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


示例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"
} 
开发者ID:adilakhter,项目名称:hashing-as-a-service,代码行数:58,代码来源:ApiActor.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala Authorization类代码示例发布时间:2022-05-23
下一篇:
Scala Routes类代码示例发布时间: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