Skip to content

Instantly share code, notes, and snippets.

@casualjim
Forked from isterin/BaseResource.scala
Last active August 29, 2015 14:00
Show Gist options
  • Save casualjim/11272244 to your computer and use it in GitHub Desktop.
Save casualjim/11272244 to your computer and use it in GitHub Desktop.
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")
}
}
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
}
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/*")
}
}
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")))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment