Last active
November 16, 2015 23:10
-
-
Save joshlemer/206db95787de9cc53d2d to your computer and use it in GitHub Desktop.
Configuration code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
database { | |
username = "josh" | |
password = "password" | |
} | |
api { | |
url = "www.josh.com" | |
port = "8080" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.typesafe.config.{ConfigFactory, Config} | |
trait Configuration { | |
val config: Config = ConfigFactory.load() | |
} | |
trait Configured[+C <: Configuration] { | |
def configuration: C | |
} | |
trait DatabaseConfiguration extends Configuration { | |
object databaseObject { | |
private val databaseConfig = config.getConfig("database") | |
val username = config.getString("username") | |
val password = config.getString("password") | |
} | |
} | |
trait ApiConfiguration extends Configuration { | |
object apiObject { | |
private val apiConfig = config.getConfig("api") | |
val url = config.getString("url") | |
val port = config.getString("port") | |
} | |
} | |
trait MainConfiguration extends ApiConfiguration with DatabaseConfiguration { | |
val version = "3.1.2" | |
} | |
class DatabaseClass extends Configured[DatabaseConfiguration] { | |
override val configuration = new DatabaseConfiguration {} | |
val theDatabaseUsername = configuration.database.username | |
} | |
class ApiClass extends Configured[ApiConfiguration] { | |
override val configuration = new ApiConfiguration {} | |
val apiPort = configuration.api.port | |
} | |
class HasDatabaseAndApi extends Configured[MainConfiguration] { | |
override val configuration = new MainApplicationConfiguration {} | |
val config = configuration | |
val apiPort = configuration.api.port | |
val theDatabaseUsername = configuration.database.username | |
val version = configuration.version | |
val databaseClass = new DatabaseClass { | |
override val configuration = config | |
} | |
val apiClass = new ApiClass { | |
override val configuration = config | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment