Skip to content

Commit 6bc7da5

Browse files
committed
chore: load secrets from the resources folder
1 parent aa934ca commit 6bc7da5

4 files changed

Lines changed: 17 additions & 11 deletions

File tree

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ gradle-app.setting
144144

145145
# End of https://www.toptal.com/developers/gitignore/api/netbeans,intellij,java,gradle,eclipse
146146
application/db/
147-
config.json
148147
secrets.json
149148
application/config.json
150149
*.db

application/src/main/java/org/togetherjava/tjbot/Application.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.togetherjava.tjbot.secrets.Secrets;
1919

2020
import java.io.IOException;
21+
import java.io.InputStream;
2122
import java.nio.file.Files;
2223
import java.nio.file.Path;
2324
import java.sql.SQLException;
@@ -34,7 +35,7 @@ private Application() {
3435
}
3536

3637
private static final Logger logger = LoggerFactory.getLogger(Application.class);
37-
private static final String DEFAULT_CONFIG_PATH = "config.json";
38+
private static final String DEFAULT_CONFIG_PATH = "/config.json";
3839
private static final String DEFAULT_SECRETS_PATH = "secrets.json";
3940

4041
/**
@@ -50,13 +51,19 @@ public static void main(final String[] args) {
5051
+ DEFAULT_CONFIG_PATH + "' will be assumed.");
5152
}
5253

53-
Path configPath = Path.of(args.length == 1 ? args[0] : DEFAULT_CONFIG_PATH);
54+
String configPath = args.length == 1 ? args[0] : DEFAULT_CONFIG_PATH;
5455
Config config;
55-
try {
56-
config = Config.load(configPath);
56+
57+
try (InputStream stream = Application.class.getResourceAsStream(configPath)) {
58+
if (stream == null) {
59+
throw new IOException("InputStream is null when loading " + configPath);
60+
}
61+
62+
String content = new String(stream.readAllBytes());
63+
config = Config.load(content);
64+
5765
} catch (IOException e) {
58-
logger.error("Unable to load the configuration file from path '{}'",
59-
configPath.toAbsolutePath(), e);
66+
logger.error("Unable to load the configuration file from path '{}'", configPath, e);
6067
return;
6168
}
6269

@@ -66,7 +73,7 @@ public static void main(final String[] args) {
6673
secrets = Secrets.load(secretsPath);
6774
} catch (IOException e) {
6875
logger.error("Unable to load the configuration file from path '{}'",
69-
configPath.toAbsolutePath(), e);
76+
secretsPath.toAbsolutePath(), e);
7077
return;
7178
}
7279

application/src/main/java/org/togetherjava/tjbot/config/Config.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ private Config(@JsonProperty(value = "databasePath", required = true) String dat
127127
/**
128128
* Loads the configuration from the given file.
129129
*
130-
* @param path the configuration file, as JSON object
130+
* @param content the configuration file, as Stringified JSON object
131131
* @return the loaded configuration
132132
* @throws IOException if the file could not be loaded
133133
*/
134-
public static Config load(Path path) throws IOException {
134+
public static Config load(String content) throws IOException {
135135
return new ObjectMapper().registerModule(new JavaTimeModule())
136-
.readValue(path.toFile(), Config.class);
136+
.readValue(content, Config.class);
137137
}
138138

139139
/**
File renamed without changes.

0 commit comments

Comments
 (0)