From af5ddb516c07f255e2d78a20bbd48037c94047f1 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Wed, 29 Feb 2012 23:46:29 +0100 Subject: [PATCH] Start system implementation --- project/Build.scala | 6 ++- system/src/main/resources/system.conf | 12 +++++ system/src/main/scala/SystemEnv.scala | 44 +++++++++++++++++++ .../src/main/scala/model}/Game.scala | 4 +- .../src/main/scala/model}/Player.scala | 4 +- .../src/main/scala/repo/GameRepo.scala | 6 ++- system/src/test/resources/log4j.properties | 4 ++ 7 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 system/src/main/resources/system.conf create mode 100644 system/src/main/scala/SystemEnv.scala rename {http/src/main/scala/repo => system/src/main/scala/model}/Game.scala (81%) rename {http/src/main/scala/repo => system/src/main/scala/model}/Player.scala (80%) rename {http => system}/src/main/scala/repo/GameRepo.scala (74%) create mode 100644 system/src/test/resources/log4j.properties diff --git a/project/Build.scala b/project/Build.scala index 045ebace5d..2870cefd21 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -8,6 +8,7 @@ trait Resolvers { val iliaz = "iliaz.com" at "http://scala.iliaz.com/" val sonatype = "sonatype" at "http://oss.sonatype.org/content/repositories/releases" val sonatypeS = "sonatype snapshots" at "http://oss.sonatype.org/content/repositories/snapshots" + val novusS = "repo.novus snaps" at "http://repo.novus.com/snapshots/" } trait Dependencies { @@ -22,6 +23,7 @@ trait Dependencies { val gson = "com.google.code.gson" % "gson" % "1.7.1" val scalalib = "com.github.ornicar" %% "scalalib" % "1.15" val hasher = "com.roundeights" % "hasher" % "0.3" from "http://cloud.github.com/downloads/Nycto/Hasher/hasher_2.9.1-0.3.jar" + val config = "com.typesafe.config" % "config" % "0.2.1" } object ApplicationBuild extends Build with Resolvers with Dependencies { @@ -30,7 +32,7 @@ object ApplicationBuild extends Build with Resolvers with Dependencies { organization := "com.github.ornicar", version := "0.1", scalaVersion := "2.9.1", - resolvers := Seq(iliaz, codahale, sonatype), + resolvers := Seq(iliaz, codahale, sonatype, novusS, typesafe), libraryDependencies in test := Seq(specs2), shellPrompt := { (state: State) ⇒ "%s> ".format(Project.extract(state).currentProject.id) @@ -43,7 +45,7 @@ object ApplicationBuild extends Build with Resolvers with Dependencies { ) lazy val system = Project("system", file("system"), settings = buildSettings).settings( - libraryDependencies := Seq(scalalib, scalaz, redis, json, casbah, salat) + libraryDependencies := Seq(scalalib, scalaz, config, redis, json, casbah, salat) ) dependsOn (chess) lazy val http = Project("http", file("http"), settings = buildSettings).settings( diff --git a/system/src/main/resources/system.conf b/system/src/main/resources/system.conf new file mode 100644 index 0000000000..5963885180 --- /dev/null +++ b/system/src/main/resources/system.conf @@ -0,0 +1,12 @@ +mongo { + host = 127.0.0.1 + port = 27017 + dbName = lichess + collection { + game = game2 + } +} +redis { + host = localhost + port = 6379 +} diff --git a/system/src/main/scala/SystemEnv.scala b/system/src/main/scala/SystemEnv.scala new file mode 100644 index 0000000000..0f244b1b05 --- /dev/null +++ b/system/src/main/scala/SystemEnv.scala @@ -0,0 +1,44 @@ +package lila.system + +import com.mongodb.casbah.MongoConnection +import com.mongodb.casbah.commons.conversions.scala._ +import com.redis.RedisClient +import com.typesafe.config._ + +import repo._ + +trait SystemEnv { + + val config: Config + + def gameRepo = new GameRepo( + mongodb(config getString "mongo.collection.game")) + + def mongodb = MongoConnection( + config getString "mongo.host", + config getInt "mongo.port" + )(config getString "mongo.dbname") + + def redis = new RedisClient( + config getString "redis.host", + config getInt "redis.port") +} + +trait EnvBuilder { + + def apply(overrides: String = "") = new SystemEnv { + val config = makeConfig(overrides, "lila.conf", "system") + } + + import java.io.File + + def makeConfig(sources: String*) = sources.foldLeft(ConfigFactory.defaultOverrides) { + case (config, source) if source isEmpty ⇒ config + case (config, source) if source contains '=' ⇒ + config.withFallback(ConfigFactory parseString source) + case (config, source) if source contains '.' ⇒ + config.withFallback(ConfigFactory parseFile (new File(source))) + case (config, source) ⇒ + config.withFallback(ConfigFactory load source) + } +} diff --git a/http/src/main/scala/repo/Game.scala b/system/src/main/scala/model/Game.scala similarity index 81% rename from http/src/main/scala/repo/Game.scala rename to system/src/main/scala/model/Game.scala index 9dabcc746b..ee21020556 100644 --- a/http/src/main/scala/repo/Game.scala +++ b/system/src/main/scala/model/Game.scala @@ -1,5 +1,5 @@ -package lila -package repo +package lila.system +package model import com.novus.salat.annotations._ import com.mongodb.casbah.Imports._ diff --git a/http/src/main/scala/repo/Player.scala b/system/src/main/scala/model/Player.scala similarity index 80% rename from http/src/main/scala/repo/Player.scala rename to system/src/main/scala/model/Player.scala index 5afc308273..bb6785c254 100644 --- a/http/src/main/scala/repo/Player.scala +++ b/system/src/main/scala/model/Player.scala @@ -1,5 +1,5 @@ -package lila -package repo +package lila.system +package model import com.novus.salat.annotations._ import com.mongodb.casbah.Imports._ diff --git a/http/src/main/scala/repo/GameRepo.scala b/system/src/main/scala/repo/GameRepo.scala similarity index 74% rename from http/src/main/scala/repo/GameRepo.scala rename to system/src/main/scala/repo/GameRepo.scala index efc845c277..f13a43b26b 100644 --- a/http/src/main/scala/repo/GameRepo.scala +++ b/system/src/main/scala/repo/GameRepo.scala @@ -1,6 +1,8 @@ -package lila +package lila.system package repo +import model._ + import com.novus.salat._ import com.novus.salat.global._ import com.novus.salat.dao._ @@ -9,5 +11,5 @@ import com.mongodb.casbah.Imports._ class GameRepo(collection: MongoCollection) extends SalatDAO[Game, String](collection) { - def game(id: String) = findOneByID(id) + def game(id: String): Option[Game] = findOneByID(id) } diff --git a/system/src/test/resources/log4j.properties b/system/src/test/resources/log4j.properties new file mode 100644 index 0000000000..5d21c4e205 --- /dev/null +++ b/system/src/test/resources/log4j.properties @@ -0,0 +1,4 @@ +log4j.rootLogger=info, R +log4j.appender.R=org.apache.log4j.ConsoleAppender +log4j.appender.R.layout=org.apache.log4j.PatternLayout +log4j.appender.R.layout.ConversionPattern=%-5p %-30.30c{1} %x - %m%n