Skip to content

Commit 1c7d1d7

Browse files
committed
Add auto start jetty option
1 parent e5f5ecf commit 1c7d1d7

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

backends/backend-web/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ dependencies {
2020

2121
implementation("com.badlogicgames.gdx:gdx:${LibExt.gdxVersion}")
2222
implementation("com.github.xpenatan:jMultiplatform:${LibExt.jMultiplatform}")
23+
implementation("org.eclipse.jetty:jetty-server:${LibExt.jettyVersion}")
24+
implementation("org.eclipse.jetty:jetty-webapp:${LibExt.jettyVersion}")
2325

2426
testImplementation("com.google.truth:truth:${LibExt.truthVersion}")
2527
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.github.xpenatan.gdx.teavm.backends.web.config.backend;
2+
3+
import com.github.xpenatan.gdx.teavm.backends.shared.config.TeaLogHelper;
4+
import java.io.File;
5+
import org.eclipse.jetty.server.Server;
6+
import org.eclipse.jetty.webapp.WebAppContext;
7+
8+
class JettyServer {
9+
10+
Server server;
11+
WebAppContext context;
12+
13+
private boolean serverStarted = false;
14+
15+
private String webAppDirectory;
16+
17+
public void runServer() {
18+
int port = 8080;
19+
server = new Server(port);
20+
context = new WebAppContext();
21+
context.setResourceBase(webAppDirectory);
22+
23+
context.setCopyWebDir(false);
24+
context.setContextPath("/");
25+
context.setParentLoaderPriority(false);
26+
27+
// Prevent windows file locking
28+
context.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
29+
30+
context.setPersistTempDirectory(false);
31+
server.setHandler(context);
32+
try {
33+
server.start();
34+
TeaLogHelper.logHeader("Jetty Dev Server started at http://localhost:" + port + "/");
35+
}
36+
catch(Exception e) {
37+
e.printStackTrace();
38+
serverStarted = false;
39+
}
40+
}
41+
42+
public void startServer(String path) {
43+
if(!serverStarted && path != null && !path.isEmpty()) {
44+
this.webAppDirectory = path + "\\webapp";
45+
File file = new File(webAppDirectory);
46+
if(!file.exists()) {
47+
//webapp don't exist
48+
throw new RuntimeException("Webapp directory doesn't exist: " + webAppDirectory);
49+
}
50+
serverStarted = true;
51+
new Thread() {
52+
@Override
53+
public void run() {
54+
runServer();
55+
}
56+
}.start();
57+
}
58+
}
59+
60+
public void stopServer() {
61+
if(this.server != null) {
62+
Server server = this.server;
63+
this.server = null;
64+
65+
try {
66+
context.stop();
67+
context.destroy();
68+
}
69+
catch(Exception e) {
70+
e.printStackTrace();
71+
}
72+
73+
try {
74+
server.stop();
75+
}
76+
catch(Exception e) {
77+
e.printStackTrace();
78+
}
79+
server.setHandler(null);
80+
server.destroy();
81+
serverStarted = false;
82+
}
83+
}
84+
85+
public boolean isServerRunning() {
86+
return serverStarted;
87+
}
88+
89+
public String getWebAppDirectory() {
90+
return webAppDirectory;
91+
}
92+
}

backends/backend-web/src/main/java/com/github/xpenatan/gdx/teavm/backends/web/config/backend/TeaWebBackend.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class TeaWebBackend extends TeaBackend {
2424
public String webappFolderName = "webapp";
2525
public boolean isWebAssembly;
2626
public AssetFilter scriptFilter;
27+
public boolean startJettyAfterBuild = false;
28+
29+
private JettyServer server;
2730

2831
public TeaWebBackend setWebAssembly(boolean isWebAssembly) {
2932
this.isWebAssembly = isWebAssembly;
@@ -70,8 +73,20 @@ public TeaWebBackend setScriptFilter(AssetFilter scriptFilter) {
7073
return this;
7174
}
7275

76+
public TeaWebBackend setStartJettyAfterBuild(boolean startJettyAfterBuild) {
77+
this.startJettyAfterBuild = startJettyAfterBuild;
78+
return this;
79+
}
80+
81+
public void startJetty(String path) {
82+
server.stopServer();
83+
server.startServer(path);
84+
}
85+
7386
@Override
7487
protected void setup(TeaCompilerData data) {
88+
server = new JettyServer();
89+
7590
if(isWebAssembly) {
7691
targetType = TeaVMTargetType.WEBASSEMBLY_GC;
7792
tool.setTargetFileName(data.outputName + ".wasm");
@@ -90,6 +105,14 @@ protected void setup(TeaCompilerData data) {
90105
setupWebapp(data);
91106
}
92107

108+
@Override
109+
protected void build(TeaCompilerData data) {
110+
super.build(data);
111+
if(startJettyAfterBuild) {
112+
startJetty(data.output.getPath());
113+
}
114+
}
115+
93116
protected void setupWebapp(TeaCompilerData data) {
94117
InputStream indexSteam = classLoader.getResourceAsStream("webapp/index.html");
95118
InputStream webXMLStream = classLoader.getResourceAsStream("webapp/WEB-INF/web.xml");

examples/basic/teavm/src/main/java/BuildTeaVMTestDemo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class BuildTeaVMTestDemo {
99

1010
public static void main(String[] args) throws IOException {
1111
AssetFileHandle assetsPath = new AssetFileHandle("../assets");
12-
new TeaCompiler(new TeaWebBackend())
12+
new TeaCompiler(new TeaWebBackend().setStartJettyAfterBuild(false))
1313
.addAssets(assetsPath)
1414
.setOptimizationLevel(TeaVMOptimizationLevel.SIMPLE)
1515
.setMainClass(TestWebLauncher.class.getName())

0 commit comments

Comments
 (0)