Skip to content

Commit

Permalink
Merge pull request #155 from Suwayomi/open-on-running
Browse files Browse the repository at this point in the history
Open on running feature
  • Loading branch information
aless2003 authored Dec 6, 2024
2 parents 5a44779 + ab6464b commit 99bdae2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ java {
}

group = 'online.hatsunemiku'
version = '1.13.0'
version = '1.13.1'
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

package online.hatsunemiku.tachideskvaadinui;

import java.io.IOException;
import java.net.ServerSocket;
import lombok.extern.slf4j.Slf4j;
import online.hatsunemiku.tachideskvaadinui.utils.BrowserUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
Expand Down Expand Up @@ -34,11 +37,51 @@ public class TachideskVaadinUiApplication {
public static void main(String[] args) {
boolean headless = Boolean.parseBoolean(System.getProperty("vaaui.headless"));

if (isRunningAlready()) {
log.warn("Application is already running.");
runAlreadyRunningTasks(headless);
System.exit(0);
}

log.info("Is headless: {}", headless);

SpringApplication app =
new SpringApplicationBuilder(TachideskVaadinUiApplication.class).headless(headless).build();

app.run(args);
}

/**
* Runs the tasks that should be run when the application is already running.
*
* @param headless Whether the application is running in headless mode
*/
private static void runAlreadyRunningTasks(boolean headless) {
if (!headless) {
log.info("Opening browser...");

try {
BrowserUtils.openBrowser("http://localhost:3901");
} catch (IOException e) {
log.error("Failed to open browser.", e);
}
}
}

/**
* Checks if the application is already running.
*
* @return {@code true} if the application is already running, {@code false} otherwise
*/
private static boolean isRunningAlready() {
int port = 3901;
try (ServerSocket ignored =
new ServerSocket(
port)) { // skipcq: JAVA-S1011 - Not a security risk, because the socket is closed
// immediately.
return false;
} catch (IOException e) {
return true;
}
}
}

0 comments on commit 99bdae2

Please sign in to comment.