Skip to content

Instantly share code, notes, and snippets.

@JosephMoniz
JosephMoniz / JsonConversions.scala
Last active August 29, 2015 14:20
An example on how to convert json to objects.
val nativeList = JsArray(JsInt(1), JsInt(2), JsInt(3)).as[List[Int]]
val failedConversion = JsArray(JsString("not"), JsString("ints")).as[List[Int]]
@JosephMoniz
JosephMoniz / BuildJson.scala
Created May 3, 2015 05:21
An example on how to construct JSON objects
import com.plasmaconduit.json._
val verbose = JsObject(
"key1" -> JsString("value"),
"key2" -> JsInt(42),
"key3" -> JsBoolean(true),
"key4" -> JsArray(JsInt(3), JsBoolean(true))
)
val sugar = JsObject(
@JosephMoniz
JosephMoniz / JsonParser.scala
Created May 3, 2015 05:20
Json parsing example
import com.plasmaconduit.json._
val result = JsonParser.parse("""{"key1": "value", "key2": true}""")
@JosephMoniz
JosephMoniz / JsonController.scala
Created May 3, 2015 05:15
An example of returning a JSON response
final case class JsonController() extends Controller {
def action(implicit request: HttpRequest): Box[Throwable, HttpResponse] = {
Ok(JsObject(
"status" -> "ok",
"message" -> JsObject(
"email" -> "[email protected]",
"admin" -> false
)
))
@JosephMoniz
JosephMoniz / SessionApplicaiton.scala
Created May 3, 2015 04:43
An example on how to configure JWT sessions.
val server = PlasmaConduit(
port = 3000,
middleware = List(SessionMiddleware(JWTSessionHandler(JWTSha256, "topsykretz"))),
defaultRoute = MissingController(),
routes = HttpRoutes(
HttpGetMethodRoutes(
HttpPathRoute("/", RootController()),
)
)
)
@JosephMoniz
JosephMoniz / SessionVisitsController.scala
Created May 3, 2015 04:25
An example of how to use sessions
final case class SessionVisitsController() extends Controller {
def action(implicit request: HttpRequest) = {
val visits = request.session.get("visits").getOrElse("0").toLong
Ok(s"This is so cool, you've viewed it $visits times!")
.withSession(request.session.set("visits", (visits + 1).toString))
}
}
final case class AuthenticatedOnlyMiddleware() extends Middleware {
def intercept(next: Controller) = AdhocController { request =>
request
.session
.get("user")
.map(_ => next.action(request))
.getOrElse(Found("").withHeader(Location("/login")))
}
@JosephMoniz
JosephMoniz / IdentityMiddleware.scala
Created May 2, 2015 19:31
An example identity middleware
final case class IdentityMiddleware() extends Middleware {
def intercept(next: Controller) = AdhocController { request =>
next.action(request)
}
}
@JosephMoniz
JosephMoniz / MiddlewareExampleController.scala
Last active August 29, 2015 14:20
Example use of middleware
final case class ExampleController() extends Controller {
def middleware = Seq(LoggingMiddleware(), CacheMiddleware())
def action(implicit request: HttpRequest) = Ok("Hello")
}
@JosephMoniz
JosephMoniz / RedirectController.scala
Created May 2, 2015 19:20
A simple redirection controller
final case class SimpleController() extends Controller {
def action(implicit request: HttpRequest) = {
Found("").withHeader(Location("/login"))
}
}