From f9a153c5fd0d659361c8c7244d7767385ab0a3c2 Mon Sep 17 00:00:00 2001 From: Antony Leons Date: Sun, 20 Mar 2022 15:49:08 +0000 Subject: [PATCH] Env setup creation (#5) * add env check * Create Ward.run.xml * enable one port and theme fixes * fix edge case * env setup * remove unnecessary check * update docs 2.1.0 --- .gitignore | 3 +- .run/Ward.run.xml | 10 ++++ README.md | 19 +++---- pom.xml | 2 +- setup.ini | 5 -- src/main/java/dev/leons/ward/Ward.java | 17 +++--- .../dev/leons/ward/services/SetupService.java | 54 +++++++++++++------ src/main/resources/static/css/themes.css | 6 +-- src/main/resources/static/js/setup.js | 12 ----- 9 files changed, 70 insertions(+), 58 deletions(-) create mode 100644 .run/Ward.run.xml delete mode 100644 setup.ini diff --git a/.gitignore b/.gitignore index 309b874..c3eff11 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea/ target/ *.iml -.vscode/ \ No newline at end of file +.vscode/ +setup.ini diff --git a/.run/Ward.run.xml b/.run/Ward.run.xml new file mode 100644 index 0000000..6a8b095 --- /dev/null +++ b/.run/Ward.run.xml @@ -0,0 +1,10 @@ + + + + + \ No newline at end of file diff --git a/README.md b/README.md index 3cb22a5..38d6491 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ ## Docker -* `docker run --restart unless-stopped -it -d --name ward -p 4000:4000 -p : --privileged antonyleons/ward` +* `docker run --restart unless-stopped -it -d --name ward -p 4000:4000 -e WARD_PORT=4000 -e WARD_THEME=dark --privileged antonyleons/ward` * Go to localhost:4000 in web browser, input the same application port * If you get error after being redirected to application port try hitting refresh @@ -153,14 +153,18 @@ Ward works nice on all popular operating systems, because it uses [OSHI](https:/ ### Config -If you want to change Ward's configuration, you can edit `setup.ini`. When using Docker, this file is located within the container at `/`. `setup.ini` is generated once you navigate to Ward's webpage and complete the initial setup. You can also make this file yourself before starting Ward and it will use your configuration. +If you want to change Ward's configuration, you can edit `setup.ini`. When using Docker, use the environment variables `WARD_NAME`,`WARD_THEME`, `WARD_PORT` to automatically regenerate this file at startup. Using any environment variable listed will enable the defaults below and immediately start Ward without the GUI setup. + + | Setting | Description | Default | |------------|------------------------------|---------| -| serverName | Name shown in the interface. | | -| theme | Either `light` or `dark`. | | +| serverName | Name shown in the interface. | Ward | +| theme | Either `light` or `dark`. | light | | port | Port to listen on. | 4000 | +Environment variables take priority and will regenerate this file with your variables. If no environment variables are set, `setup.ini` is generated once you navigate to Ward's webpage and complete the initial setup. You can also make this file yourself before starting Ward, and place it in the same directory. + For example: ```ini [setup] @@ -168,10 +172,3 @@ serverName = my-server theme = dark port = 8200 ``` - -If you're using Docker and you want to avoid the initial setup or have Ward listen on a different port right away, you could create a `setup.ini` on the host and copy it to the container: -```bash -docker cp setup.ini ward:/setup.ini -``` - -Then, just make sure you `docker restart ward`. diff --git a/pom.xml b/pom.xml index b90d989..82ee5c0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 dev.leons ward - 2.0.2 + 2.1.0 jar diff --git a/setup.ini b/setup.ini deleted file mode 100644 index 4d54f86..0000000 --- a/setup.ini +++ /dev/null @@ -1,5 +0,0 @@ -[setup] -serverName = Ward -theme = light -port = 5000 - diff --git a/src/main/java/dev/leons/ward/Ward.java b/src/main/java/dev/leons/ward/Ward.java index 6f5b24e..bcd5e2a 100644 --- a/src/main/java/dev/leons/ward/Ward.java +++ b/src/main/java/dev/leons/ward/Ward.java @@ -1,5 +1,6 @@ package dev.leons.ward; +import dev.leons.ward.services.SetupService; import lombok.Getter; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.SpringApplication; @@ -16,8 +17,7 @@ import java.io.File; * @version 1.0.4 */ @SpringBootApplication -public class Ward extends SpringBootServletInitializer -{ +public class Ward extends SpringBootServletInitializer { /** * Constant for determine settings file name */ @@ -44,14 +44,16 @@ public class Ward extends SpringBootServletInitializer * * @param args Spring Boot application arguments */ - public static void main(final String[] args) - { + public static void main(final String[] args) { + isFirstLaunch = true; configurableApplicationContext = SpringApplication.run(Ward.class, args); File setupFile = new File(Ward.SETUP_FILE_PATH); - if (setupFile.exists()) - { + + if (System.getenv("WARD_NAME") != null || (System.getenv("WARD_THEME") != null) || (System.getenv("WARD_PORT") != null)) { + SetupService.envSetup(); + } else if (setupFile.exists()) { restart(); } } @@ -59,8 +61,7 @@ public class Ward extends SpringBootServletInitializer /** * Restarts application */ - public static void restart() - { + public static void restart() { isFirstLaunch = false; ApplicationArguments args = configurableApplicationContext.getBean(ApplicationArguments.class); diff --git a/src/main/java/dev/leons/ward/services/SetupService.java b/src/main/java/dev/leons/ward/services/SetupService.java index 9494441..e865f4e 100644 --- a/src/main/java/dev/leons/ward/services/SetupService.java +++ b/src/main/java/dev/leons/ward/services/SetupService.java @@ -17,8 +17,7 @@ import java.io.IOException; * @version 1.0.2 */ @Service -public class SetupService -{ +public class SetupService { /** * Constant, that providing setup section name */ @@ -27,12 +26,11 @@ public class SetupService /** * Puts new data in ini file * - * @param file ini file + * @param file ini file * @param optionName option in section * @throws IOException if file does not exists */ - private void putInIniFile(final File file, final String optionName, final String value) throws IOException - { + private static void putInIniFile(final File file, final String optionName, final String value) throws IOException { Ini ini = new Ini(file); ini.put(SECTION_NAME, optionName, value); ini.store(); @@ -45,30 +43,52 @@ public class SetupService * @return ResponseEntityWrapperAsset filled with ResponseDto * @throws IOException IoException if file is fot found, and cant be created */ - public ResponseDto postSetup(final SetupDto setupDto) throws IOException, ApplicationAlreadyConfiguredException - { - if (Ward.isFirstLaunch()) - { + public ResponseDto postSetup(final SetupDto setupDto) throws IOException, ApplicationAlreadyConfiguredException { + if (Ward.isFirstLaunch()) { File file = new File(Ward.SETUP_FILE_PATH); - if (file.createNewFile()) - { + if (file.createNewFile()) { putInIniFile(file, "serverName", setupDto.getServerName()); putInIniFile(file, "theme", setupDto.getTheme()); putInIniFile(file, "port", setupDto.getPort()); Ward.restart(); - } - else - { + } else { throw new IOException(); } - } - else - { + } else { throw new ApplicationAlreadyConfiguredException(); } return new ResponseDto("Settings saved correctly"); } + + public static ResponseDto envSetup() { + if (Ward.isFirstLaunch()) { + try { + File file = new File(Ward.SETUP_FILE_PATH); + if (file.exists()) { + file.delete(); + } + if (file.createNewFile()) { + String servername = (System.getenv("WARD_NAME") != null) ? System.getenv("WARD_NAME") : "Ward"; + String theme = (System.getenv("WARD_THEME") != null) ? System.getenv("WARD_THEME").toLowerCase() : "light"; + String port = (System.getenv("WARD_PORT") != null) ? System.getenv("WARD_PORT") : "4000"; + + putInIniFile(file, "serverName", servername); + putInIniFile(file, "theme", theme); + putInIniFile(file, "port", port); + + Ward.restart(); + } else { + throw new IOException(); + } + + } catch (IOException e) { + e.printStackTrace(); + } + } + + return new ResponseDto("Settings saved correctly"); + } } \ No newline at end of file diff --git a/src/main/resources/static/css/themes.css b/src/main/resources/static/css/themes.css index 513f6e9..783b924 100644 --- a/src/main/resources/static/css/themes.css +++ b/src/main/resources/static/css/themes.css @@ -107,12 +107,12 @@ html[theme = "dark"] --color-logo: var(--white); --color-logo-description: var(--white); --color-label-main-settings: var(--white); - --color-main-settings-input: var(--grey); + --color-main-settings-input: var(--white); --color-main-settings-input-placeholder: var(--grey-light); - --color-label-additional-settings: var(--grey-light); + --color-label-additional-settings: var(--white); --color-theme-buttons-input: var(--grey); --color-port: var(--white); - --color-port-placeholder: var(--white); + --color-port-placeholder: var(--grey-light); --color-submit: var(--white); --color-hw-type: var(--white); --color-hw-name: var(--white); diff --git a/src/main/resources/static/js/setup.js b/src/main/resources/static/js/setup.js index 0ebf7b1..4b2eae2 100644 --- a/src/main/resources/static/js/setup.js +++ b/src/main/resources/static/js/setup.js @@ -120,18 +120,6 @@ function sendSetupRequest() "port": port.value } - if (port.value != 4000) - { setupXHR.send(JSON.stringify(data)); - } - else - { - let message = - { - text: "Choose other port", - type: ("") - } - dhtmlx.message(message); - } } \ No newline at end of file