casualjim
4/24/2014 - 10:51 PM

BaseResource.scala

class TestResource(implicit swagger: Swagger) extends BaseResource() with Logging {

  val renderTemplate = (apiOperation[String]("getSomething")
      summary "Get random data"
      parameter pathParam[String]("name").description("Name")
      )
  post("/:name", operation(renderTemplate)) {
    new AsyncResult() {
      override val is = Future {
        Ok(Map("a" -> "b", "name" -> params("name")))
      }
    }
  }

}
class ScalatraBootstrap extends LifeCycle with Logging with Imports {

  implicit val  swagger= new Swagger
  override def destroy(context: ServletContext) {
    system.shutdown()
  }

  override def init(context: ServletContext) {

    context.mount(new TestResource, "/api/test")

    context.mount(new MessagingResourcesApp, "/api-docs/*")
  }
}
trait Imports {
  implicit def jsonFormats = GlobalContext.jsonFormats
  implicit def swagger = GlobalContext.swagger
  implicit val system = GlobalContext.system
  implicit val executor = GlobalContext.executor
}

object GlobalContext {
  implicit def jsonFormats: Formats = DefaultFormats + new TemplateTypeSerializer + new EnumNameSerializer(DeliveryStatus)
  implicit def swagger = new MessagingSwagger
  implicit val system = ActorSystem()
  implicit val executor = system.dispatcher
}
abstract class BaseResource(implicit protected val swagger: Swagger)
    extends ScalatraServlet with FutureSupport with JacksonJsonSupport with SwaggerSupport with Imports {

  override protected val applicationName = Some("messaging-api")
  protected def applicationDescription = "Messaging Services API"

  protected implicit val defaultTimeout = Timeout(5 seconds)

  override val jsonpCallbackParameterNames: Iterable[String] = Some("callback")

  protected override def transformRequestBody(body: JValue): JValue = body.camelizeKeys

  protected override def transformResponseBody(body: JValue): JValue = body.underscoreKeys

  before() {
    contentType = formats("json")
  }

}