Skip to content

Commit

Permalink
add more options for configuring and update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Inei1 committed Apr 5, 2018
1 parent 5cd8f57 commit 4b7b40f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ The web server exposes access to the various resources both with an HTTP REST AP
More information is available in the docs.md file at the root of this repository.
Also, here is a [direct link](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/MovingBlocks/FacadeServer/develop/src/main/resources/web/swagger.json#/) to view online the Swagger/OpenAPI specification of the HTTP API.

### HTTPS connections
The server is able to support https connections.
A self-signed certificate is provided in a keystore file, which will make browsers refuse to connect unless an exception is made.
To avoid this problem, you can obtain a valid certificate, [convert it to jks format](https://blogs.oracle.com/jtc/installing-trusted-certificates-into-a-java-keystore), and replace the keystore.jks file.


### Related repositories
[Here](https://github.com/gianluca-nitti/FacadeServer-frontend) is the code for a web and mobile frontend to FacadeServer.

Expand Down
Binary file modified keystore.jks
Binary file not shown.
45 changes: 30 additions & 15 deletions src/main/java/org/terasology/web/ServerMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,40 @@ public static void main(String[] args) throws Exception {
setupLogging();
ServerAdminsManager.getInstance().loadAdminList();

String portEnv = System.getenv("PORT");
if (portEnv == null) {
portEnv = "8080";
logger.warn("Environment variable 'PORT' not defined - using default {}", portEnv);
String httpPortEnv = System.getenv("HTTP_PORT");
if (httpPortEnv == null) {
httpPortEnv = "8080";
logger.warn("Environment variable 'HTTP_PORT' not defined - using default {}", httpPortEnv);
}
Integer port = Integer.valueOf(portEnv);

Integer httpPort = Integer.valueOf(httpPortEnv);

String httpsPortEnv = System.getenv("HTTPS_PORT");
if (httpsPortEnv == null) {
httpsPortEnv = "8443";
logger.warn("Environment variable 'HTTPS_PORT' not defined - using default {}", httpsPortEnv);
}

String keystorePassword = System.getenv("KEYSTORE_PASSWORD");
if (keystorePassword == null){
keystorePassword = "ServerKeyPassword";
logger.warn("Environment variable 'KEYSTORE_PASSWORD' not defined - using default {}", keystorePassword);
}

Integer httpsPort = Integer.valueOf(httpsPortEnv);

// this is mostly for I18nMap, but can have an influence on other
// string formats. Note that metainfo.ftl explicitly sets the locale to
// define the date format.
Locale.setDefault(Locale.ENGLISH);

Server server = createServer(port,
Server server = createServer(httpPort, httpsPort, keystorePassword,
new LogServlet(),
new AboutServlet(),
new HttpAPIServlet());

server.start();
logger.info("Web server started on port {}!", port);
logger.info("Web server started on port {}!", httpPort);

EngineRunner.getInstance().runEngine(autoStart);

Expand Down Expand Up @@ -137,7 +152,7 @@ private static void printUsage() {
System.out.println(ARG_ENGINE_SERVER_PORT + ": use the specified port for the Terasology server");
System.out.println(ARG_WAIT_MANUAL_START + ": do not generate and start a game with the default settings, but wait for manual setup via the web interface");
System.out.println();
System.out.println("The web server port (default 8080) can be overridden by setting the environment variable PORT.");
System.out.println("The web server port (default 8080) can be overridden by setting the environment variable HTTP_PORT.");
}

private static void setupLogging() {
Expand All @@ -149,8 +164,8 @@ private static void setupLogging() {
LoggingContext.initialize(path);
}

private static Server createServer(int port, Object... annotatedObjects) throws Exception {
Server server = new Server(port);
private static Server createServer(int httpPort, int httpsPort, String keystorePassword, Object... annotatedObjects) throws Exception {
Server server = new Server(httpPort);

ResourceHandler logFileResourceHandler = new ResourceHandler();
logFileResourceHandler.setDirectoriesListed(true);
Expand Down Expand Up @@ -179,21 +194,21 @@ private static Server createServer(int port, Object... annotatedObjects) throws
Path keyStorePath = PathManager.getInstance().getInstallPath().resolve("facades").resolve("Server").resolve("keystore.jks");
File keyStoreFile = new File(keyStorePath.toString());
try {
keyStore.load(new FileInputStream(keyStoreFile), "ServerKeyPassword".toCharArray());
keyStore.load(new FileInputStream(keyStoreFile), keystorePassword.toCharArray());
} catch (FileNotFoundException e) {
logger.error("Keystore file not found");
e.printStackTrace();
}

SslContextFactory sslContextFactory = new SslContextFactory();
sslContextFactory.setKeyStorePath(keyStorePath.toString());
sslContextFactory.setKeyStorePassword("ServerKeyPassword");
sslContextFactory.setKeyStorePassword(keystorePassword);

ServerConnector wssConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,
ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory,
HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory());

wssConnector.setPort(8443);
server.addConnector(wssConnector);
sslConnector.setPort(httpsPort);
server.addConnector(sslConnector);

ServletContextHandler jerseyContext = new ServletContextHandler(ServletContextHandler.GZIP);
jerseyContext.setResourceBase("templates");
Expand Down

0 comments on commit 4b7b40f

Please sign in to comment.