Skip to content

Instantly share code, notes, and snippets.

protected case class Encrypted[A](action: Action[A]) extends Action[A] {
def apply(req: Request[A]) = {
EnvUtil.isCloud match {
case true =>
req.headers.get("x-forwarded-proto") match {
case Some("https") => action(req)
case _ => redirectToHttps(req)
}
case _ =>
@stephanos
stephanos / gist:2694905
Created May 14, 2012 16:26
play onRouteRequest
override def onRouteRequest(req: RequestHeader): Option[Action[_]] =
super.onRouteRequest(req).map {
case action: Action[_] =>
currentUserId(req) match {
case None =>
req.path match {
case p if isRestrictedPath(p) => // requires login
Authenticated(action)
case p if isEncryptedWhenLoggedOut(p) => // required HTTPS for user/
Encrypted(action)
@stephanos
stephanos / gist:2694910
Created May 14, 2012 16:27
play assets
//~ INTERFACE =================================================================================
lazy val robots_yes = Assets.at("/public", "robots.txt")
lazy val robots_no = Assets.at("/public", "robots.no.txt")
lazy val robots = if (isProduction) robots_yes else robots_no
def at(typeOf: String, file: String, ver: String = "") = {
val path = typeOf match {
case "js" => ("/public/javascripts", file)
case "css" => ("/public/stylesheets", file)
@stephanos
stephanos / gist:2694917
Created May 14, 2012 16:28
play promise
def login =
Action(parse.urlFormEncoded) {
implicit req =>
val body = req.body
// validate timestamp (not older than 5 minutes)
val tstamp = body("timestamp").head
if (new DateTime((tstamp + "000").toLong).isBefore(new DateTime().minusMinutes(5)))
Forbidden("timestamp invalid")
else {
@stephanos
stephanos / gist:2843716
Created May 31, 2012 14:24
nodejs for play assets
object Compiler4CoffeeScript {
import play.core.coffeescript._
import org.mozilla.javascript.JavaScriptException
import play.core.coffeescript.CompilationException
def compile(files: Array[File]): String =
(files.map{
f =>
try {
def load2(id1: String, id2: String) = Action {
try {
val dir = if (id1 == null) "" else id1.replaceAllLiterally("-", "_") + "."
val file = id2.replaceAllLiterally("-", "_")
val c = Class.forName("views.html.slides." + dir + file)
val m = c.getMethod("render")
Ok(m.invoke(null).asInstanceOf[Html])
} catch {
case e =>
@stephanos
stephanos / r.js
Created September 8, 2012 17:07
r.js profiling
#!/usr/bin/env node
/**
* @license r.js 2.0.6 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
* Available via the MIT or new BSD license.
* see: http://github.com/jrburke/requirejs for details
*/
/*
* This is a bootstrap script to allow running RequireJS in the command line
* in either a Java/Rhino or Node environment. It is modified by the top-level
@stephanos
stephanos / asseto.coffee
Created September 12, 2012 09:04
asset compiler
console.time("total")
S = require 'string'
os = require 'os'
fs = require 'fs'
sys = require 'sys'
mkdirp = require 'mkdirp'
cluster = require 'cluster'
less = require 'less'
wrench = require 'wrench'
scala> val n = 10
n: Int = 10
scala> "Bob is " + n + " years old"
res0: String = Bob is 10 years old
scala> "Bob is $n years old"
res1: String = Bob is $n years old
scala> s"Bob is $n years old"
val usdQuote = future { connection.getCurrentValue(USD) }
val chfQuote = future { connection.getCurrentValue(CHF) }
val purchase = for {
usd <- usdQuote
chf <- chfQuote
if isProfitable(usd, chf)
} yield connection.buy(amount, chf)
purchase onSuccess {