Awesomest handling of 404
This commit is contained in:
parent
162c17b9e8
commit
6e3da19b0a
|
@ -38,13 +38,13 @@ trait LilaController
|
|||
Open(BodyParsers.parse.anyContent)(f)
|
||||
|
||||
def Open[A](p: BodyParser[A])(f: Context ⇒ Result): Action[A] =
|
||||
Action(p)(req ⇒ f(Context(req, restoreUser(req))))
|
||||
Action(p)(req ⇒ f(reqToCtx(req)))
|
||||
|
||||
def OpenBody(f: BodyContext ⇒ Result): Action[AnyContent] =
|
||||
OpenBody(BodyParsers.parse.anyContent)(f)
|
||||
|
||||
def OpenBody[A](p: BodyParser[A])(f: BodyContext ⇒ Result): Action[A] =
|
||||
Action(p)(req ⇒ f(Context(req, restoreUser(req))))
|
||||
Action(p)(req ⇒ f(reqToCtx(req)))
|
||||
|
||||
def JsonOk(map: Map[String, Any]) = Ok(toJson(map)) as JSON
|
||||
|
||||
|
@ -85,8 +85,10 @@ trait LilaController
|
|||
|
||||
def IORedirect(op: IO[Call]) = Redirect(op.unsafePerformIO)
|
||||
|
||||
def IOption[A, B](ioa: IO[Option[A]])(op: A ⇒ B)(implicit writer: Writeable[B], ctype: ContentTypeOf[B]) =
|
||||
ioa.unsafePerformIO.fold(a ⇒ Ok(op(a)), NotFound)
|
||||
def IOption[A, B](ioa: IO[Option[A]])(op: A ⇒ B)(implicit writer: Writeable[B], ctype: ContentTypeOf[B], ctx: Context) =
|
||||
ioa.unsafePerformIO.fold(a ⇒ Ok(op(a)), notFound(ctx))
|
||||
|
||||
def notFound(ctx: Context) = Lobby handleNotFound ctx
|
||||
|
||||
// I like Unit requests.
|
||||
implicit def wUnit: Writeable[Unit] =
|
||||
|
@ -116,8 +118,12 @@ trait LilaController
|
|||
)
|
||||
}
|
||||
|
||||
private def restoreUser[A](request: Request[A]): Option[UserModel] = for {
|
||||
sessionId ← request.session.get("sessionId")
|
||||
protected def reqToCtx(req: Request[_]) = Context(req, restoreUser(req))
|
||||
|
||||
protected def reqToCtx(req: RequestHeader) = Context(req, restoreUser(req))
|
||||
|
||||
private def restoreUser[A](req: RequestHeader): Option[UserModel] = for {
|
||||
sessionId ← req.session.get("sessionId")
|
||||
userId ← Cache.getAs[Id](sessionId + ":sessionId")(current, idManifest)
|
||||
user ← resolveUser(userId)
|
||||
} yield {
|
||||
|
|
|
@ -18,15 +18,23 @@ object Lobby extends LilaController {
|
|||
private val preloader = env.lobby.preloader
|
||||
|
||||
val home = Open { implicit ctx ⇒
|
||||
preloader(
|
||||
renderHome(ctx).fold(identity, Ok(_))
|
||||
}
|
||||
|
||||
def handleNotFound(req: RequestHeader): Result =
|
||||
handleNotFound(reqToCtx(req))
|
||||
|
||||
def handleNotFound(ctx: Context): Result =
|
||||
renderHome(ctx).fold(identity, NotFound(_))
|
||||
|
||||
private def renderHome(implicit ctx: Context) = preloader(
|
||||
auth = ctx.isAuth,
|
||||
chat = ctx.canSeeChat,
|
||||
myHookId = get("hook")
|
||||
).unsafePerformIO.fold(
|
||||
).unsafePerformIO.bimap(
|
||||
url ⇒ Redirect(url),
|
||||
preload ⇒ Ok(html.lobby.home(toJson(preload)))
|
||||
preload ⇒ html.lobby.home(toJson(preload))
|
||||
)
|
||||
}
|
||||
|
||||
def socket = WebSocket.async[JsValue] { implicit req ⇒
|
||||
implicit val ctx = Context(req, None)
|
||||
|
|
|
@ -19,16 +19,16 @@ object Global extends GlobalSettings {
|
|||
//else Cron start env
|
||||
}
|
||||
|
||||
override def onRouteRequest(request: RequestHeader): Option[Handler] = {
|
||||
println(request)
|
||||
env.i18n.requestHandler(request) orElse super.onRouteRequest(request)
|
||||
override def onRouteRequest(req: RequestHeader): Option[Handler] = {
|
||||
println(req)
|
||||
env.i18n.requestHandler(req) orElse super.onRouteRequest(req)
|
||||
}
|
||||
|
||||
override def onHandlerNotFound(request: RequestHeader): Result = {
|
||||
NotFound("Not found " + request)
|
||||
override def onHandlerNotFound(req: RequestHeader): Result = {
|
||||
controllers.Lobby handleNotFound req
|
||||
}
|
||||
|
||||
override def onBadRequest(request: RequestHeader, error: String) = {
|
||||
override def onBadRequest(req: RequestHeader, error: String) = {
|
||||
BadRequest("Bad Request: " + error)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue