58 lines
1.7 KiB
Scala
58 lines
1.7 KiB
Scala
package controllers
|
|
|
|
import javax.inject.*
|
|
import play.api.*
|
|
import play.api.mvc.*
|
|
import authentication.{AuthenticatedAction, AuthenticatedRequest}
|
|
import caching.{Etagable, checkFresh}
|
|
import models.User
|
|
import play.api.libs.json.Json
|
|
|
|
import java.nio.ByteBuffer
|
|
import java.nio.charset.StandardCharsets.UTF_8
|
|
import java.security.MessageDigest
|
|
import java.util.Base64
|
|
|
|
/**
|
|
* 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 {
|
|
|
|
/**
|
|
* 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
|
|
request.checkFresh(user) {
|
|
// TODO: put the max-age somewhere into config
|
|
Ok(Json.toJson(user)).withHeaders("Etag" -> user.etag, "Cache-Control" -> "private, max-age=15, must-revalidate")
|
|
}
|
|
}
|
|
|
|
private given Etagable[models.User] with {
|
|
private val base64 = Base64.getUrlEncoder
|
|
|
|
extension (a: User)
|
|
override def etag: String = {
|
|
val md = MessageDigest.getInstance("SHA-1")
|
|
md.update(ByteBuffer.allocate(8).putLong(a.id.getOrElse(0)))
|
|
md.update(a.gid.getBytes(UTF_8))
|
|
md.update(a.username.getBytes(UTF_8))
|
|
|
|
base64.encodeToString(md.digest())
|
|
}
|
|
}
|
|
}
|