diff --git a/avaje-jex/src/main/java/io/avaje/jex/Context.java b/avaje-jex/src/main/java/io/avaje/jex/Context.java index 11f52c32..63ba7ced 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Context.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Context.java @@ -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. */ diff --git a/avaje-jex/src/main/java/io/avaje/jex/DJex.java b/avaje-jex/src/main/java/io/avaje/jex/DJex.java index a5bf11dc..d83eaadf 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/DJex.java +++ b/avaje-jex/src/main/java/io/avaje/jex/DJex.java @@ -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) { diff --git a/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java b/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java index 10da8cfb..028ff6af 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java +++ b/avaje-jex/src/main/java/io/avaje/jex/DJexConfig.java @@ -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; @@ -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; } @@ -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 diff --git a/avaje-jex/src/main/java/io/avaje/jex/Jex.java b/avaje-jex/src/main/java/io/avaje/jex/Jex.java index 98a1f343..4782d9f5 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/Jex.java +++ b/avaje-jex/src/main/java/io/avaje/jex/Jex.java @@ -134,9 +134,9 @@ default Jex staticResource(Consumer consumer) { * Configures the Jex instance using a functional approach. * *

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 configure); @@ -145,20 +145,9 @@ default Jex staticResource(Consumer 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. - * - *

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. * diff --git a/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java b/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java index 09fe2418..cff502b3 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java +++ b/avaje-jex/src/main/java/io/avaje/jex/JexConfig.java @@ -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. + * + *

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 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 renderers(); - /** configure compression via consumer */ - JexConfig compression(Consumer 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(); } diff --git a/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java b/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java index 115c5bf8..de307994 100644 --- a/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java +++ b/avaje-jex/src/main/java/io/avaje/jex/jdk/JdkServerStart.java @@ -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; @@ -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();