Skip to content
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
6 changes: 6 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/DJex.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public Jex port(int port) {
return this;
}

@Override
public Jex context(String contextPath) {
config.contextPath(contextPath);
return this;
}

@Override
public Jex register(TemplateRender renderer, String... extensions) {
for (String extension : extensions) {
Expand Down
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 @@ -15,6 +15,7 @@
final class DJexConfig implements JexConfig {

private int port = 8080;
private String contextPath = "/";
private int socketBacklog = 0;
private boolean health = true;
private boolean ignoreTrailingSlashes = true;
Expand All @@ -31,6 +32,12 @@ public JexConfig port(int port) {
return this;
}

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

@Override
public JexConfig socketBacklog(int socketBacklog) {
this.socketBacklog = socketBacklog;
Expand Down Expand Up @@ -82,6 +89,11 @@ public int port() {
return port;
}

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

@Override
public int socketBacklog() {
return socketBacklog;
Expand Down
10 changes: 10 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ default Jex staticResource(Consumer<StaticContentConfig> consumer) {
*/
Jex port(int port);

/**
* Sets the context path for the Jex application.
*
* <p>The context path is the portion of the URL that identifies the application.
*
* @param contextPath The context path to use.
* @return The updated Jex instance.
*/
Jex context(String contextPath);

/**
* Explicitly register a template renderer.
*
Expand Down
11 changes: 11 additions & 0 deletions avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public sealed interface JexConfig permits DJexConfig {
*/
JexConfig port(int port);

/**
* Set the contextPath.
*
* @param contextPath The context path which defaults to "/".
*/
JexConfig contextPath(String contextPath);

/**
* Set the socket backlog. If this value is less than or equal to zero, then a system default
* value is used
Expand Down Expand Up @@ -90,6 +97,9 @@ public sealed interface JexConfig permits DJexConfig {
/** Returns the configured port number. (Defaults to 8080 if not set) */
int port();

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

/** Returns whether the health endpoint is enabled. */
boolean health();

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

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

}
13 changes: 6 additions & 7 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,7 +2,6 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.System.Logger.Level;
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpServer;
Expand All @@ -14,6 +13,8 @@
import io.avaje.jex.core.SpiServiceManager;
import io.avaje.jex.routes.SpiRoutes;

import static java.lang.System.Logger.Level.INFO;

public final class JdkServerStart {

private static final System.Logger log = AppLog.getLogger("io.avaje.jex");
Expand All @@ -22,6 +23,7 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana
try {
final var config = jex.config();
final var port = new InetSocketAddress(config.port());
final var contextPath = config.contextPath();
final var https = config.httpsConfig();
final var backlog = config.socketBacklog();

Expand All @@ -37,18 +39,15 @@ public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceMana
server = HttpServer.create(port, backlog);
}

final var manager = new CtxServiceManager(serviceManager, scheme, "");

final var manager = new CtxServiceManager(serviceManager, scheme, contextPath);
final var handler = new RoutingHandler(routes, manager, config.compression());

server.setExecutor(config.executor());
server.createContext("/", handler);
server.createContext(contextPath, handler);
server.start();

jex.lifecycle().status(AppLifecycle.Status.STARTED);
log.log(
Level.INFO,
"started com.sun.net.httpserver.HttpServer on port %s://%s".formatted(scheme, port));
log.log(INFO, "started com.sun.net.httpserver.HttpServer on port %s://%s", scheme, port);
return new JdkJexServer(server, jex.lifecycle(), handler);
} catch (IOException e) {
throw new UncheckedIOException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void contextPath() {
.GET().asString();

assertThat(res.statusCode()).isEqualTo(200);
assertThat(res.body()).isEqualTo("contextPath:");
assertThat(res.body()).isEqualTo("contextPath:/");
}

@Test
Expand Down