Skip to content

Instantly share code, notes, and snippets.

View leon's full-sized avatar

Leon Radley leon

View GitHub Profile
@ilkka
ilkka / tag_cloud_tag.rb
Created November 22, 2010 20:07
Jekyll tag cloud / tag pages plugin
module Jekyll
class TagCloudTag < Liquid::Tag
safe = true
def initialize(tag_name, text, tokens)
super
end
def render(context)
html = ""
@domdorn
domdorn / SGBeanPersistController.java
Created January 1, 2012 12:46
JPA2 EntityListener Annotations for EBean
package models.sgcore;
import com.avaje.ebean.event.BeanPersistAdapter;
import com.avaje.ebean.event.BeanPersistRequest;
import javax.annotation.PreDestroy;
import javax.persistence.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
@etorreborre
etorreborre / gist:2033254
Created March 14, 2012 01:35
Implicit FakeApplication context for a Play 2.0 Specification
package examples
import org.specs2._
import specification._
import FakeApplication._
/**
* This Specification shows how to declare only once the FakeApplication context in which all the examples
* must be executed
*/
@crockpotveggies
crockpotveggies / Mailloy.scala
Created March 26, 2012 23:51
A mailer trait for sending HTML emails for Play! Framework 2.0
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@guillaumebort
guillaumebort / Secured.scala
Created April 7, 2012 12:05
HTTP Basic Authorization for Play 2.0
def Secured[A](username: String, password: String)(action: Action[A]) = Action(action.parser) { request =>
request.headers.get("Authorization").flatMap { authorization =>
authorization.split(" ").drop(1).headOption.filter { encoded =>
new String(org.apache.commons.codec.binary.Base64.decodeBase64(encoded.getBytes)).split(":").toList match {
case u :: p :: Nil if u == username && password == p => true
case _ => false
}
}.map(_ => action(request))
}.getOrElse {
Unauthorized.withHeaders("WWW-Authenticate" -> """Basic realm="Secured"""")
@guillaumebort
guillaumebort / Global.scala
Created June 9, 2012 11:50
Track response time of simple Action
import play.api._
import play.api.mvc._
object Global extends GlobalSettings {
def ResponseTime[A](action: Action[A]): Action[A] = Action(action.parser) { request =>
val start = System.currentTimeMillis
val result = action(request)
println( request + " -> " + (System.currentTimeMillis - start) + " ms.")
result
@teamon
teamon / sbt.md
Created September 13, 2012 21:49

Multiproject sbt configuration with separate build files

root
 |- sub1
 |- sub2
  • root aggregates sub1 and sub2
  • sub1 depends on sub2
@raulraja
raulraja / EmailService.scala
Created January 13, 2013 00:18
scala EmailService Actor
package services
import akka.actor.Actor._
import com.typesafe.plugin._
import akka.actor.{Props, OneForOneStrategy, Actor}
import play.api.libs.concurrent.Akka
import play.api.Play.current
case class EmailMessage(subject: String, recipient: String, from: String, text: String, html: String)
@sadache
sadache / gist:4714280
Last active July 14, 2022 15:09
Playframework: Async, Reactive, Threads, Futures, ExecutionContexts

Asynchronicity is the price to pay, you better know what you're paying for...

Let's share some vocabulary first:

Thread: The primitive responsible of executing code on the processor, you can give an existing (or a new) Thread some code, and it will execute it. Normally you can have a few hundreds on a JVM, arguments that you can tweak your way out to thousands. Worth noting that multitasking is achieved when using multiple Threads. Multiple Threads can exist for a single processor in which case multitasking happens when this processor switches between threads, called context switching, which will give the impression of things happenning in parallel. An example of a direct, and probably naive, use of a new Thread in Java:

public class MyRunnable implements Runnable {
  public void run(){
 System.out.println("MyRunnable running");
@mariussoutier
mariussoutier / JsonFormats.scala
Last active December 6, 2016 07:19
ReactiveMongo Play Plugin Extensions
package json
import reactivemongo.bson._
import reactivemongo.bson.handlers.DefaultBSONHandlers._
import play.api.libs.json._
import play.api.libs.json.Json._
import play.api.libs.json.util._
import play.api.libs.json.Writes._
import play.api.libs.functional.syntax._