-
-
Notifications
You must be signed in to change notification settings - Fork 101
Added config file support #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
95 changes: 95 additions & 0 deletions
95
application/src/main/java/org/togetherjava/tjbot/config/Config.java
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.togetherjava.tjbot.config; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Configuration of the application, as singleton. | ||
* <p> | ||
* Create instances using {@link #load(Path)} and then access them with {@link #getInstance()}. | ||
*/ | ||
public final class Config { | ||
|
||
private static Config config; | ||
|
||
private final String token; | ||
private final String databasePath; | ||
private final String projectWebsite; | ||
private final String discordGuildInvite; | ||
|
||
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) | ||
private Config(@JsonProperty("token") String token, | ||
@JsonProperty("databasePath") String databasePath, | ||
@JsonProperty("projectWebsite") String projectWebsite, | ||
@JsonProperty("discordGuildInvite") String discordGuildInvite) { | ||
this.token = token; | ||
this.databasePath = databasePath; | ||
this.projectWebsite = projectWebsite; | ||
this.discordGuildInvite = discordGuildInvite; | ||
} | ||
|
||
/** | ||
* Loads the configuration from the given file. Will override any previously loaded data. | ||
* <p> | ||
* Access the instance using {@link #getInstance()}. | ||
* | ||
* @param path the configuration file, as JSON object | ||
* @throws IOException if the file could not be loaded | ||
*/ | ||
public static void load(Path path) throws IOException { | ||
config = new ObjectMapper().readValue(path.toFile(), Config.class); | ||
} | ||
|
||
/** | ||
* Gets the singleton instance of the configuration. | ||
* <p> | ||
* Must be loaded beforehand using {@link #load(Path)}. | ||
* | ||
* @return the previously loaded configuration | ||
*/ | ||
public static Config getInstance() { | ||
return Objects.requireNonNull(config, | ||
"can not get the configuration before it has been loaded"); | ||
} | ||
|
||
/** | ||
* Gets the token of the Discord bot to connect this application to. | ||
* | ||
* @return the Discord bot token | ||
*/ | ||
public String getToken() { | ||
return token; | ||
} | ||
|
||
/** | ||
* Gets the path where the database of the application is located at. | ||
* | ||
* @return the path of the database | ||
*/ | ||
public String getDatabasePath() { | ||
return databasePath; | ||
} | ||
|
||
/** | ||
* Gets a URL of the project's website, for example to tell the user where he can contribute. | ||
* | ||
* @return the website of the project | ||
*/ | ||
public String getProjectWebsite() { | ||
return projectWebsite; | ||
} | ||
|
||
/** | ||
* Gets an invite-URL to join the Discord guild this application is connected to. | ||
* | ||
* @return an invite-URL for this Discord guild | ||
*/ | ||
public String getDiscordGuildInvite() { | ||
return discordGuildInvite; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.