aboutsummaryrefslogtreecommitdiff
path: root/server/src/Main.scala
blob: 91649a2b46d79ed88b8ca50a23651dea032252db (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package triad

import java.nio.file.{Files, Paths}

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.stream.ActorMaterializer

import scala.concurrent._
import scala.concurrent.duration._

object Main extends App {

  def log(message: String) = System.err.println(message)

  log("Initializing contexts...")
  implicit val system = ActorSystem("triad")
  implicit val materializer = ActorMaterializer()
  system.registerOnTermination {
    log("Bye!")
  }

  try {
    log("Initializing database...")
    val repository = {
      Files.deleteIfExists(Paths.get("database.sqlite"))
      Repository.sqlite("database.sqlite")
    }

    log("Preparing live message relay...")
    val liveMessages = new LiveMessages

    log("Setting up routes...")
    val routes = new Routes(repository, liveMessages)

    log("Populating database tables...")
    Await.result(repository.database.run(repository.initAction), 10.seconds)

    log("Binding to network...")
    Await.result(Http().bindAndHandle(routes.all, "0.0.0.0", 9090), 10.seconds)

    log("Ready")
  } catch {
    case ex: Exception =>
      log("Error in initialization. Shutting down...")
      system.terminate()
  }

}