
config: Fix config error while starting Yobi
Problem: An error occurs while starting Yobi in product mode if there is no conf/application.conf. Reason: Yobi has a feature to initilizate conf/application.conf by copying from conf/application.conf.default. But it does not work for product mode because Application.onLoadConfig, which does the initialization, runs after configuration has been loaded. Solution: Run the config initialization in Build.scala.
@53920e0315f62917961a5b9fa4f8640997ea35ef
--- app/Global.java
+++ app/Global.java
... | ... | @@ -68,40 +68,10 @@ |
68 | 68 |
import static play.mvc.Results.badRequest; |
69 | 69 |
|
70 | 70 |
public class Global extends GlobalSettings { |
71 |
- public static final String APPLICATION_CONF_DEFAULT = "application.conf.default"; |
|
72 |
- public static final String APPLICATION_CONF = "application.conf"; |
|
73 |
- public static final String CONFIG_DIRNAME = "conf"; |
|
74 | 71 |
private final String DEFAULT_SECRET = "VA2v:_I=h9>?FYOH:@ZhW]01P<mWZAKlQ>kk>Bo`mdCiA>pDw64FcBuZdDh<47Ew"; |
75 | 72 |
|
76 | 73 |
private boolean isRestartRequired = false; |
77 | 74 |
private boolean isValidationRequired = false; |
78 |
- |
|
79 |
- @Override |
|
80 |
- public Configuration onLoadConfig(play.Configuration config, File path, ClassLoader classloader) { |
|
81 |
- String basePath = path.getAbsolutePath(); |
|
82 |
- Path pathToDefaultConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF_DEFAULT); |
|
83 |
- Path pathToConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF); |
|
84 |
- File configFile = pathToConfig.toFile(); |
|
85 |
- |
|
86 |
- if (!configFile.exists()) { |
|
87 |
- try { |
|
88 |
- Files.copy(pathToDefaultConfig, pathToConfig); |
|
89 |
- } catch (IOException e) { |
|
90 |
- play.Logger.error("Failed to initialize configuration", e); |
|
91 |
- return null; |
|
92 |
- } |
|
93 |
- Config parsedConfig = ConfigFactory.parseFileAnySyntax(configFile); |
|
94 |
- return new Configuration(ConfigFactory.load(classloader, parsedConfig)); |
|
95 |
- } else { |
|
96 |
- if (!configFile.isFile()) { |
|
97 |
- play.Logger.error( |
|
98 |
- "Failed to initialize configuration: " + pathToConfig + " is a directory."); |
|
99 |
- return null; |
|
100 |
- } |
|
101 |
- } |
|
102 |
- |
|
103 |
- return null; |
|
104 |
- } |
|
105 | 75 |
|
106 | 76 |
public void onStart(Application app) { |
107 | 77 |
isValidationRequired = !validateSecret(); |
--- project/Build.scala
+++ project/Build.scala
... | ... | @@ -5,11 +5,17 @@ |
5 | 5 |
import play.Project.javaEbean |
6 | 6 |
import play.Project.templatesImport |
7 | 7 |
import play.Project.lessEntryPoints |
8 |
+import java.nio.file.Files |
|
9 |
+import java.nio.file.Paths |
|
10 |
+import java.io.IOException; |
|
8 | 11 |
|
9 | 12 |
object ApplicationBuild extends Build { |
10 | 13 |
|
11 | 14 |
val appName = "yobi" |
12 | 15 |
val appVersion = "1.0-SNAPSHOT" |
16 |
+ val APPLICATION_CONF_DEFAULT = "application.conf.default" |
|
17 |
+ val APPLICATION_CONF = "application.conf" |
|
18 |
+ val CONFIG_DIRNAME = "conf" |
|
13 | 19 |
|
14 | 20 |
val appDependencies = Seq( |
15 | 21 |
// Add your project dependencies here, |
... | ... | @@ -59,7 +65,29 @@ |
59 | 65 |
scalacOptions ++= Seq("-feature") |
60 | 66 |
) |
61 | 67 |
|
62 |
- val main = play.Project(appName, appVersion, appDependencies) |
|
68 |
+ val initConfig = { |
|
69 |
+ val basePath = new File(System.getProperty("user.dir")).getAbsolutePath() |
|
70 |
+ val pathToDefaultConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF_DEFAULT) |
|
71 |
+ val pathToConfig = Paths.get(basePath, CONFIG_DIRNAME, APPLICATION_CONF) |
|
72 |
+ val configFile = pathToConfig.toFile() |
|
73 |
+ |
|
74 |
+ if (!configFile.exists()) { |
|
75 |
+ try { |
|
76 |
+ Files.copy(pathToDefaultConfig, pathToConfig) |
|
77 |
+ } catch { |
|
78 |
+ case e: IOException => throw new Exception("Failed to initialize configuration", e) |
|
79 |
+ } |
|
80 |
+ } else { |
|
81 |
+ if (!configFile.isFile()) { |
|
82 |
+ throw new Exception("Failed to initialize configuration: '" + pathToConfig + "' is a directory.") |
|
83 |
+ } |
|
84 |
+ } |
|
85 |
+ } |
|
86 |
+ |
|
87 |
+ val main = { |
|
88 |
+ initConfig |
|
89 |
+ play.Project(appName, appVersion, appDependencies) |
|
63 | 90 |
.settings(projectSettings: _*) |
64 | 91 |
.settings(net.virtualvoid.sbt.graph.Plugin.graphSettings: _*) |
92 |
+ } |
|
65 | 93 |
} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?