package controllers import play.api.* import _root_.controllers.authentication.{AuthenticatedAction, AuthenticatedRequest} import _root_.controllers.caching.{CheckFresh, DigestEtagable} import models.User import play.api.libs.json.Json import play.api.mvc.* import java.io.DataOutputStream import java.nio.charset.StandardCharsets.UTF_8 import javax.inject.* /** * This controller creates an `Action` to handle HTTP requests to the * application's home page. */ @Singleton class HomeController @Inject()(val controllerComponents: ControllerComponents, authenticatedAction: AuthenticatedAction) extends BaseController with Logging with CheckFresh { /** * Create an Action to render an HTML page. * * The configuration in the `routes` file means that this method * will be called when the application receives a `GET` request with * a path of `/`. */ def index() = Action { implicit request: Request[AnyContent] => Ok(views.html.index()) } def userData = authenticatedAction { implicit request: AuthenticatedRequest[AnyContent] => val user = request.user checkFresh(user) { // TODO: put the max-age somewhere into config Ok(Json.toJson(user)).withHeaders("Cache-Control" -> "private, max-age=15, must-revalidate") } } private given DigestEtagable[models.User] with { override protected def writeDigestData(a: User, s: DataOutputStream): Unit = { s.writeLong(a.id.getOrElse(0)) s.write(a.gid.getBytes(UTF_8)) s.write(a.username.getBytes(UTF_8)) } } }