1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
/*
* generated by Xtext 2.25.0
*/
package org.eclipse.viatra.solver.language.web;
import java.io.File;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.log.Slf4jLog;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
public class ServerLauncher {
private static final Slf4jLog LOG = new Slf4jLog(ServerLauncher.class.getName());
private final Server server;
public ServerLauncher(InetSocketAddress bindAddress, Resource baseResource) {
server = new Server(bindAddress);
var ctx = new WebAppContext();
ctx.setBaseResource(baseResource);
ctx.setWelcomeFiles(new String[] { "index.html" });
ctx.setContextPath("/");
ctx.setConfigurations(new Configuration[] { new AnnotationConfiguration(), new WebXmlConfiguration(),
new WebInfConfiguration(), new MetaInfConfiguration() });
ctx.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*/build/classes/.*,.*\\.jar");
ctx.setInitParameter("org.eclipse.jetty.servlet.Default.useFileMappedBuffer", "false");
server.setHandler(ctx);
}
public void start() throws Exception {
server.start();
LOG.info("Server started " + server.getURI() + "...");
LOG.info("Press enter to stop the server...");
int key = System.in.read();
if (key != -1) {
server.stop();
} else {
LOG.warn(
"Console input is not available. In order to stop the server, you need to cancel process manually.");
}
}
private static InetSocketAddress getBindAddress(String listenAddress, int port) {
if (listenAddress == null) {
return new InetSocketAddress(port);
}
return new InetSocketAddress(listenAddress, port);
}
private static Resource getBaseResource(String baseResourceOverride) throws IOException, URISyntaxException {
if (baseResourceOverride != null) {
return Resource.newResource(baseResourceOverride);
}
var indexUrlInJar = ServerLauncher.class.getResource("/webapp/index.html");
if (indexUrlInJar == null) {
var workingPath = new String[] { System.getProperty("user.dir"), "build", "webpack", "development", };
return Resource.newResource(new File(String.join(File.separator, workingPath)));
}
var webRootUri = URI.create(indexUrlInJar.toURI().toASCIIString().replaceFirst("/index.html$", "/"));
return Resource.newResource(webRootUri);
}
public static void main(String[] args) {
var listenAddress = System.getenv("LISTEN_ADDRESS");
if (listenAddress == null) {
listenAddress = "localhost";
}
var port = 1312;
var portStr = System.getenv("LISTEN_PORT");
if (portStr != null) {
try {
port = Integer.parseInt(portStr);
} catch (NumberFormatException e) {
LOG.warn(e);
System.exit(1);
}
}
var baseResourceOverride = System.getenv("BASE_RESOURCE");
try {
var bindAddress = getBindAddress(listenAddress, port);
var baseResource = getBaseResource(baseResourceOverride);
var serverLauncher = new ServerLauncher(bindAddress, baseResource);
serverLauncher.start();
} catch (Exception exception) {
LOG.warn(exception);
System.exit(1);
}
}
}
|