Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Enable Host Config #103

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ final class DJexConfig implements JexConfig {

private int port = 8080;
private String contextPath = "/";
private String host;
private int socketBacklog = 0;
private boolean health = true;
private boolean ignoreTrailingSlashes = true;
Expand All @@ -26,6 +27,12 @@ final class DJexConfig implements JexConfig {
private boolean useJexSpi = true;
private final CompressionConfig compression = new CompressionConfig();

@Override
public JexConfig host(String host) {
this.host = host;
return this;
}

@Override
public JexConfig port(int port) {
this.port = port;
Expand Down Expand Up @@ -84,6 +91,11 @@ public JexConfig executor(Executor factory) {
return this;
}

@Override
public String host() {
return host;
}

@Override
public int port() {
return port;
Expand Down
13 changes: 11 additions & 2 deletions avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
public sealed interface JexConfig permits DJexConfig {

/**
* Sets the port number on which Jex will listen for incoming requests.
* Set the host on which the HttpServer will bind to.
*
* @param host The host.
*/
JexConfig host(String host);

/**
* Sets the port number on which the HttpServer will listen for incoming requests.
*
* @param port The port number.
*/
Expand Down Expand Up @@ -97,6 +104,9 @@ public sealed interface JexConfig permits DJexConfig {
/** Returns the configured port number. (Defaults to 8080 if not set) */
int port();

/** Returns the configured host. (Defaults to localhost if not set) */
String host();

/** Return the contextPath. (Defaults to "/") */
String contextPath();

Expand Down Expand Up @@ -132,5 +142,4 @@ public sealed interface JexConfig permits DJexConfig {

/** Return the socket backlog. */
int socketBacklog();

}
14 changes: 10 additions & 4 deletions avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpServer;
Expand All @@ -22,21 +23,23 @@ public final class JdkServerStart {
public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {
try {
final var config = jex.config();
final var port = new InetSocketAddress(config.port());
final var port = config.port();
final var host = InetAddress.getByName(config.host());
final var socket = new InetSocketAddress(host, config.port());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't the same. Any local address vs the loopback address ... I think this isn't right?

final var contextPath = config.contextPath();
final var https = config.httpsConfig();
final var backlog = config.socketBacklog();

final HttpServer server;
final String scheme;
if (https != null) {
var httpsServer = HttpsServer.create(port, backlog);
var httpsServer = HttpsServer.create(socket, backlog);
httpsServer.setHttpsConfigurator(https);
server = httpsServer;
scheme = "https";
} else {
scheme = "http";
server = HttpServer.create(port, backlog);
server = HttpServer.create(socket, backlog);
}

final var manager = new CtxServiceManager(serviceManager, scheme, contextPath);
Expand All @@ -47,7 +50,10 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana
server.start();

jex.lifecycle().status(AppLifecycle.Status.STARTED);
log.log(INFO, "started com.sun.net.httpserver.HttpServer on port %s://%s", scheme, port);
log.log(
INFO,
"started com.sun.net.httpserver.HttpServer on port %s://%s:%s"
.formatted(scheme, host.getHostName(), port));
return new JdkJexServer(server, jex.lifecycle(), handler);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down