title: How do I perform an action on server startup in the Scala Play Framework?
body: <p>I have a config file <code>servers.conf</code> in my <code>conf/</code> directory that is read by my ServerController whenever the route <code>/servers</code> is hit. This isn't performant because it requires a re-read of the configuration file on each successive hit when the file won't change. Further if there are problems with the config file, I can tell the user ASAP rather than throw an exception on a page hit.</p>
  
  <p>Currently I have this in my <code>ServerController.scala</code>:</p>
  
  <pre><code>case class Server(ip: String, port: String)
  
  /**
    * This controller creates an `Action` to handle HTTP requests to the
    * application's server page.
    */
  @Singleton
  class ServerController @Inject() extends Controller {
  
    /**
      * Create an Action to render an HTML page with a the list of servers.
      * The configuration in the `routes` file means that this method
      * will be called when the application receives a `GET` request with
      * a path of `/servers`.
      */
    def index = Action {
  
      val serverList = ConfigFactory.load().getConfigList("servers")
      val servers: List[Server] = serverList match {
        case null =&gt; Nil
        case _ =&gt; serverList map { s =&gt;
          Server(s.getString("ip"), s.getString("port"))
        } filter { s =&gt;
          s.ip != null &amp;&amp; s.port != null
        }.toList
      }
  
      Ok(views.html.servers(servers))
    }
  }
  </code></pre>
  
  <p>My goal is to have the server read the config file at startup and pass the list of servers to the ServerController when the route is hit if there are no problems reading in the config file. If there are problems, I want an exception to be thrown immediately.</p>
  
  <p>I can't seem to find an entry point for my application, though, so I don't know how to perform actions on startup.</p>
  
  <p>Does anyone know how to do this? I'm using Play 2.5.x.</p>