ps0604
ps0604

Reputation: 1071

Invoking common functions in Play

I have the following action in a Play Framework:

  def action = Action { request =>

    common1() // this is common to all the actions

    // some functions specific to the action

    var json = common2()  // this is common to all the actions

    Ok(json)
  }

I have many actions in my application. My problem is that common1 and common2 are called in ALL the actions, and I don't want to repeat the invocation. What would be a good practice to handle this scenario?

Upvotes: 0

Views: 57

Answers (1)

rethab
rethab

Reputation: 8403

Http Filters

If something is invoked with every action, you might want to take a look at filters: https://www.playframework.com/documentation/2.5.x/ScalaHttpFilters

Example from above link:

class LoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter {

  def apply(nextFilter: RequestHeader => Future[Result])
           (requestHeader: RequestHeader): Future[Result] = {

    val startTime = System.currentTimeMillis

    nextFilter(requestHeader).map { result =>

      val endTime = System.currentTimeMillis
      val requestTime = endTime - startTime

      Logger.info(s"${requestHeader.method} ${requestHeader.uri} took ${requestTime}ms and returned ${result.header.status}")

      result.withHeaders("Request-Time" -> requestTime.toString)
    }
  }
}

Action Composition:

If you have some things you want to run certain code for certain actions, create your own ActionFitlers, ActionRefiners etc: https://www.playframework.com/documentation/2.5.x/ScalaActionsComposition

Example from above link:

object LoggingAction extends ActionBuilder[Request] {
  def invokeBlock[A](request: Request[A], block: (Request[A]) => Future[Result]) = {
    Logger.info("Calling action")
    block(request)
  }
}

Usage:

def index = LoggingAction {
  Ok("Hello World")
}

Upvotes: 1

Related Questions