Extract http response writers

This commit is contained in:
Thibault Duplessis 2012-05-26 14:31:05 +02:00
parent 907a18c3e3
commit 642e87f5ad
3 changed files with 38 additions and 32 deletions

View file

@ -6,13 +6,9 @@ import security.{ AuthImpl, Permission }
import http.{ Context, BodyContext, HttpEnvironment }
import core.Global
import jp.t2v.lab.play20.auth._
import play.api._
import play.api.mvc._
import play.api.data._
import play.api.data.Form
import play.api.http._
import scala.io.Codec
import com.codahale.jerkson.Json
import scalaz.effects._
@ -21,6 +17,7 @@ trait LilaController
with HttpEnvironment
with ContentTypes
with RequestGetter
with ResponseWriter
with AuthImpl {
lazy val env = Global.env
@ -139,32 +136,6 @@ trait LilaController
def notFound(ctx: Context) = Lobby handleNotFound ctx
// I like Unit requests.
implicit def wUnit: Writeable[Unit] =
Writeable[Unit](_ Codec toUTF8 "ok")
implicit def ctoUnit: ContentTypeOf[Unit] =
ContentTypeOf[Unit](Some(ContentTypes.TEXT))
implicit def wFloat: Writeable[Float] =
Writeable[Float](f Codec toUTF8 f.toString)
implicit def ctoFloat: ContentTypeOf[Float] =
ContentTypeOf[Float](Some(ContentTypes.TEXT))
implicit def wLong: Writeable[Long] =
Writeable[Long](a Codec toUTF8 a.toString)
implicit def ctoLong: ContentTypeOf[Long] =
ContentTypeOf[Long](Some(ContentTypes.TEXT))
implicit def wInt: Writeable[Int] =
Writeable[Int](i Codec toUTF8 i.toString)
implicit def ctoInt: ContentTypeOf[Int] =
ContentTypeOf[Int](Some(ContentTypes.TEXT))
implicit def wOptionString: Writeable[Option[String]] =
Writeable[Option[String]](i Codec toUTF8 i.getOrElse(""))
implicit def ctoOptionString: ContentTypeOf[Option[String]] =
ContentTypeOf[Option[String]](Some(ContentTypes.TEXT))
protected def reqToCtx(req: Request[_]) = Context(req, restoreUser(req))
protected def reqToCtx(req: RequestHeader) = Context(req, restoreUser(req))

View file

@ -0,0 +1,35 @@
package controllers
import lila._
import play.api.http._
import scala.io.Codec
trait ResponseWriter {
// I like Unit requests.
implicit def wUnit: Writeable[Unit] =
Writeable[Unit](_ Codec toUTF8 "ok")
implicit def ctoUnit: ContentTypeOf[Unit] =
ContentTypeOf[Unit](Some(ContentTypes.TEXT))
implicit def wFloat: Writeable[Float] =
Writeable[Float](f Codec toUTF8 f.toString)
implicit def ctoFloat: ContentTypeOf[Float] =
ContentTypeOf[Float](Some(ContentTypes.TEXT))
implicit def wLong: Writeable[Long] =
Writeable[Long](a Codec toUTF8 a.toString)
implicit def ctoLong: ContentTypeOf[Long] =
ContentTypeOf[Long](Some(ContentTypes.TEXT))
implicit def wInt: Writeable[Int] =
Writeable[Int](i Codec toUTF8 i.toString)
implicit def ctoInt: ContentTypeOf[Int] =
ContentTypeOf[Int](Some(ContentTypes.TEXT))
implicit def wOptionString: Writeable[Option[String]] =
Writeable[Option[String]](i Codec toUTF8 i.getOrElse(""))
implicit def ctoOptionString: ContentTypeOf[Option[String]] =
ContentTypeOf[Option[String]](Some(ContentTypes.TEXT))
}

2
todo
View file

@ -22,7 +22,6 @@ http://codetunes.com/2012/05/09/scala-dsl-tutorial-writing-web-framework-router
use POST instead of GET where it makes sense
next deploy:
upgrade jre (jdk?)
db.user.update({},{$unset:{lastLogin: true}}, false, true)
db.user.update({},{$unset:{isOnline: true}}, false, true)
db.user.update({},{$unset:{gameConfigs: true}}, false, true)
@ -30,3 +29,4 @@ db.user.update({},{$unset:{email: true}}, false, true)
db.user.dropIndex("emailCanonical_1")
db.user.update({},{$unset:{emailCanonical: true}}, false, true)
db.user.update({},{$unset:{updatedAt: true}}, false, true)
db.user.update({elo: {$lt: 800}}, {$set: {elo: 800}}, false, true)