get("/me") { environment match { case"DEVELOPMENT" => println("this is dev env") case"PRODUCTION" => println("this is pro env") case _ => println("unknown env") } }
// port on which embedded servlet container should listen port = 8080 // public url of the application webBase = "http://dev.example.org"
// directory from which public web assets are served assetsDirectory = "target/webapp" // environment this instance runs in; can be one of development, test, staging, or production environment = "development"
scala> val cfg = ConfigFactory.load cfg: com.typesafe.config.Config = Config(...
scala> val webBase = cfg.getString("webBase") webBase: String = http://dev.example.org
scala> println(webBase) http://dev.example.org
之后的话,我们在项目中实例化配置类就能读取相应的配置项了。
下面是一个简单的构造示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
objectAppConfig{ defload: AppConfig = { val cfg = ConfigFactory.load val assetsDirectory = cfg.getString("assetsDirectory") val webBase = cfg.getString("webBase") val port = cfg.getInt("port") val env = AppEnvironment.fromString(cfg.getString("environment")) val mailConfig = MailConfig( cfg.getString("email.user"), cfg.getString("email.password"), cfg.getString("email.host"), cfg.getString("email.sender"))
objectScalatraLauncher{ defmain(args: Array[String]): Unit = { val conf = AppConfig.load val server = newServer server.setStopTimeout(5000) server.setStopAtShutdown(true)
val connector = newServerConnector(server) connector.setHost("127.0.0.1") connector.setPort(conf.port)
server.addConnector(connector)
val webAppContext = newWebAppContext webAppContext.setContextPath("/") webAppContext.setResourceBase(conf.assetsDirectory) webAppContext.setEventListeners(Array(newScalatraListener))
sbt:web> help webappPrepare prepare webapp contents for packaging sbt:web> help dist Build a distribution, assemble the files, create a launcher and make an archive.
sbt:web> webappPrepare [success] Total time: 1 s, completed Apr17, 20204:53:11PM sbt:web> dist [info] Adding /mnt/d/IdeaProjects/scala/scalatra-study/my-scalatra-web-app/target/webapp to dist in /mnt/d/IdeaProjects/scala/scalatra-study/my-scalatra-web-app/target/dist/webapp [success] Total time: 1 s, completed Apr17, 20204:53:14PM
$ docker logs example1 Exception in thread "main" com.typesafe.config.ConfigException$IO: /app/conf/application.conf: java.io.FileNotFoundException: /app/conf/application.conf (No such file or directory) at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:190) at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:174) at com.typesafe.config.impl.Parseable.parse(Parseable.java:301) at com.typesafe.config.ConfigFactory.parseFile(ConfigFactory.java:739) at com.typesafe.config.DefaultConfigLoadingStrategy.parseApplicationConfig(DefaultConfigLoadingStrategy.java:51) at com.typesafe.config.ConfigFactory.defaultApplication(ConfigFactory.java:478) at com.typesafe.config.ConfigFactory$1.call(ConfigFactory.java:260) at com.typesafe.config.ConfigFactory$1.call(ConfigFactory.java:257) at com.typesafe.config.impl.ConfigImpl$LoaderCache.getOrElseUpdate(ConfigImpl.java:66) at com.typesafe.config.impl.ConfigImpl.computeCachedConfig(ConfigImpl.java:93) at com.typesafe.config.ConfigFactory.load(ConfigFactory.java:257) at com.typesafe.config.ConfigFactory.load(ConfigFactory.java:233) at com.example.app.AppConfig$.load(AppConfig.scala:52) at ScalatraLauncher$.main(ScalatraLauncher.scala:10) at ScalatraLauncher.main(ScalatraLauncher.scala) Caused by: java.io.FileNotFoundException: /app/conf/application.conf (No such file or directory) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157) at com.typesafe.config.impl.Parseable$ParseableFile.reader(Parseable.java:629) at com.typesafe.config.impl.Parseable.reader(Parseable.java:99) at com.typesafe.config.impl.Parseable.rawParseValue(Parseable.java:233) at com.typesafe.config.impl.Parseable.parseValue(Parseable.java:180) ... 14 more