summerNight
summerNight

Reputation: 1496

get ServerStatus using Scala/Play Framework

I am trying to add a REST endpoint to my application where users can query for getStatus. The only purpose of this endpoint would be check if the server is active and ready to accept requests.

But rather than displaying a static text or date, I was hoping to return how long the server (play server) has been active since.

Is this possible? I would like to do it the most Scala/Play way possible.

Upvotes: 0

Views: 246

Answers (2)

marcospereira
marcospereira

Reputation: 12212

It could be as simple as the following controller:

package controllers

import play.api.mvc._

class UptimeController extends Controller {

  val startTime = System.currentTimeMillis()

  def uptime = Action {
    val uptimeInMillis = System.currentTimeMillis() - startTime
    Ok(s"$uptimeInMillis ms")
  }
}

And then declare the following route:

GET     /uptime          controllers.UptimeController.uptime

Upvotes: 2

CassOnMars
CassOnMars

Reputation: 6181

You can use play.api.libs.concurrent.Akka.system(implicit app: Application)'s method uptime to get the number of seconds the Play app has been alive.

Upvotes: 2

Related Questions