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
2 changes: 1 addition & 1 deletion avaje-jex/src/main/java/io/avaje/jex/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface Context {
Context cookie(String name, String value, int maxAge);

/**
* Sets a cookie using the provided `Cookie` object.
* Sets a cookie using the provided {@link Cookie} object.
*
* @param cookie The cookie object to set.
*/
Expand Down
6 changes: 0 additions & 6 deletions avaje-jex/src/main/java/io/avaje/jex/DJex.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ 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
22 changes: 5 additions & 17 deletions avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
final class DJexConfig implements JexConfig {

private int port = 8080;
private String host;
private String contextPath = "/";
private int socketBacklog = 0;
private boolean health = true;
private boolean ignoreTrailingSlashes = true;
private Executor executor;
Expand All @@ -33,14 +32,8 @@ public JexConfig port(int port) {
}

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

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

Expand Down Expand Up @@ -90,13 +83,8 @@ public int port() {
}

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

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

@Override
Expand Down
15 changes: 2 additions & 13 deletions avaje-jex/src/main/java/io/avaje/jex/Jex.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ default Jex staticResource(Consumer<StaticContentConfig> consumer) {
* Configures the Jex instance using a functional approach.
*
* <p>The provided consumer lambda allows you to customize the Jex configuration, such as setting
* the port, context path, and other options.
* the port, compression, and other options.
*
* @param configure A consumer lambda that accepts a `JexConfig` instance for configuration.
* @param configure A consumer lambda that accepts a {@link JexConfig} instance for configuration.
* @return The configured Jex instance.
*/
Jex configure(Consumer<JexConfig> configure);
Expand All @@ -145,20 +145,9 @@ default Jex staticResource(Consumer<StaticContentConfig> consumer) {
* Sets the port number on which the Jex server will listen for incoming requests.
*
* @param port The port number to use.
* @return The updated Jex instance.
*/
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
114 changes: 75 additions & 39 deletions avaje-jex/src/main/java/io/avaje/jex/JexConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,88 +2,124 @@

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

import javax.net.ssl.SSLContext;

import com.sun.net.httpserver.HttpsConfigurator;

import io.avaje.jex.compression.CompressionConfig;
import io.avaje.jex.spi.JexPlugin;
import io.avaje.jex.spi.JsonService;
import io.avaje.jex.spi.TemplateRender;

/** Jex configuration. */
/**
* Jex configuration interface.
*
* <p>Provides a fluent API for configuring Jex's various settings, including port, host, health
* endpoint, trailing slash handling, JSON service, template renderers, executor service, HTTPS
* configuration, compression, and plugin loading.
*/
public sealed interface JexConfig permits DJexConfig {

/** Set the port to use. Defaults to 7001. */
/**
* Sets the port number on which Jex will listen for incoming requests.
*
* @param port The port number.
*/
JexConfig port(int port);

/** Set the host to bind to. */
JexConfig host(String host);

/** Set the contextPath. */
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
*
* @param backlog the socket backlog. If this value is less than or equal to zero, then a system
* default value is used
*/
JexConfig socketBacklog(int backlog);

/** Set to true to include the health endpoint. Defaults to true. */
/**
* Enables/Disables the default health endpoint.
*
* @param health whether to enable/disable.
*/
JexConfig health(boolean health);

/** Set to true to ignore trailing slashes. Defaults to true. */
/**
* Configures whether trailing slashes in request URIs should be ignored.
*
* @param ignoreTrailingSlashes whether to enable/disable trailing slashes.
*/
JexConfig ignoreTrailingSlashes(boolean ignoreTrailingSlashes);

/** Set the JsonService to use. */
/**
* Sets the JSON service used for (de)serialization.
*
* @param jsonService The json service instance.
*/
JexConfig jsonService(JsonService jsonService);

/**
* Register a template renderer explicitly.
* Registers a template renderer for a specific file extension.
*
* @param extension The extension the renderer applies to.
* @param renderer The template render to use for the given extension.
* @param extension The file extension.
* @param renderer The template renderer implementation.
*/
JexConfig renderer(String extension, TemplateRender renderer);

/** Set executor for serving requests. */
/**
* Sets the executor service used to handle incoming requests.
*
* @param executor The executor service.
*/
JexConfig executor(Executor executor);

/**
* Executor for serving requests. Defaults to a {@link
* Executors#newVirtualThreadPerTaskExecutor()}
* Enable https with the provided {@link HttpsConfigurator}
*
* @param https The HTTPS configuration.
*/
Executor executor();

/** Return the port to use. */
int port();
JexConfig httpsConfig(HttpsConfigurator https);

/** Return the host to bind to. */
String host();
/**
* Configures compression settings using a consumer function.
*
* @param consumer The consumer function to configure compression settings.
* @return The updated configuration.
*/
JexConfig compression(Consumer<CompressionConfig> consumer);

/** Return the contextPath to use. */
String contextPath();
/** Returns the configured port number. (Defaults to 8080 if not set) */
int port();

/** Return true to include the health endpoint. */
/** Returns whether the health endpoint is enabled. */
boolean health();

/** Return true to ignore trailing slashes. */
/** Returns whether trailing slashes in request URIs are ignored. */
boolean ignoreTrailingSlashes();

/** Return the JsonService. */
/** Returns the configured JSON service. */
JsonService jsonService();

/** Return the {@link HttpsConfigurator} if https is enabled. */
HttpsConfigurator httpsConfig();

/** Enable https with the provided {@link HttpsConfigurator} */
JexConfig httpsConfig(HttpsConfigurator https);
/** Returns the configured compression settings. */
CompressionConfig compression();

/** Return the template renderers registered by extension. */
/** Returns a map of registered template renderers, keyed by file extension. */
Map<String, TemplateRender> renderers();

/** configure compression via consumer */
JexConfig compression(Consumer<CompressionConfig> consumer);

/** get compression configuration */
CompressionConfig compression();
/**
* Executor for serving requests. Defaults to a {@link
* Executors#newVirtualThreadPerTaskExecutor()}
*/
Executor executor();

/** whether to disable JexPlugins loaded from ServiceLoader */
/**
* Disables auto-configuring the current instance with {@link JexPlugin} loaded using the
* ServiceLoader.
*/
JexConfig disableSpiPlugins();

/** Return the socket backlog. */
int socketBacklog();
}
15 changes: 8 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 @@ -6,7 +6,6 @@
import java.net.InetSocketAddress;

import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;

import io.avaje.applog.AppLog;
Expand All @@ -21,26 +20,28 @@ public final class JdkServerStart {

public Jex.Server start(Jex jex, SpiRoutes routes, SpiServiceManager serviceManager) {
try {
var port = new InetSocketAddress(jex.config().port());
final HttpsConfigurator https = jex.config().httpsConfig();
final var config = jex.config();
final var port = new InetSocketAddress(config.port());
final var https = config.httpsConfig();
final var backlog = config.socketBacklog();

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

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

final var handler = new RoutingHandler(routes, manager, jex.config().compression());
final var handler = new RoutingHandler(routes, manager, config.compression());

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

Expand Down