diff --git a/src/main/java/ch/fritteli/maze/server/MazeServer.java b/src/main/java/ch/fritteli/maze/server/MazeServer.java index b41da69..f1639cb 100644 --- a/src/main/java/ch/fritteli/maze/server/MazeServer.java +++ b/src/main/java/ch/fritteli/maze/server/MazeServer.java @@ -18,8 +18,8 @@ public class MazeServer { private final Undertow undertow; private MazeServer(@NotNull final ServerConfig config) { - final String hostAddress = config.getAddress().getHostAddress(); - final int port = config.getPort(); + final String hostAddress = config.address().getHostAddress(); + final int port = config.port(); log.info("Starting Server at http://{}:{}/", hostAddress, port); final RoutingHandler routingHandler = new RoutingHandler() .get(CreateHandler.PATH_TEMPLATE, new CreateHandler()) diff --git a/src/main/java/ch/fritteli/maze/server/ServerConfig.java b/src/main/java/ch/fritteli/maze/server/ServerConfig.java index ce25a6b..374606f 100644 --- a/src/main/java/ch/fritteli/maze/server/ServerConfig.java +++ b/src/main/java/ch/fritteli/maze/server/ServerConfig.java @@ -1,32 +1,27 @@ package ch.fritteli.maze.server; import io.vavr.control.Try; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Value; import lombok.extern.slf4j.Slf4j; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.wildfly.common.annotation.NotNull; import java.net.InetAddress; -@AllArgsConstructor(access = AccessLevel.PRIVATE) @Slf4j -@Value -public class ServerConfig { +public record ServerConfig(@NotNull InetAddress address, int port) { public static final String SYSPROP_HOST = "fritteli.maze.server.host"; public static final String SYSPROP_PORT = "fritteli.maze.server.port"; - @NotNull - InetAddress address; - int port; - - public ServerConfig(@Nullable final String address, final int port) throws ConfigurationException { - this.address = validateAddress(address); + public ServerConfig(@NotNull final InetAddress address, int port) { + this.address = address; this.port = validatePort(port); log.debug("host={}, port={}", this.address, this.port); } + public ServerConfig(@Nullable final String address, final int port) throws ConfigurationException { + this(validateAddress(address), port); + } + @NotNull public static ServerConfig init() throws ConfigurationException { final String host = System.getProperty(SYSPROP_HOST); @@ -36,7 +31,7 @@ public class ServerConfig { } @NotNull - private static InetAddress validateAddress(@Nullable final String address) { + private static InetAddress validateAddress(@Nullable final String address) throws ConfigurationException { return Try.of(() -> InetAddress.getByName(address)) .getOrElseThrow(cause -> new ConfigurationException( "Invalid hostname/address: %s".formatted(address), @@ -44,14 +39,14 @@ public class ServerConfig { )); } - private static int validatePort(final int port) { + private static int validatePort(final int port) throws ConfigurationException { if (port < 0 || port > 0xFFFF) { throw new ConfigurationException("Port out of range (0..65535): %s".formatted(port)); } return port; } - private static int validatePort(@Nullable final String portString) { + private static int validatePort(@Nullable final String portString) throws ConfigurationException { if (portString == null) { log.info("No port configured; using default."); return 0; diff --git a/src/test/java/ch/fritteli/maze/server/ServerConfigTest.java b/src/test/java/ch/fritteli/maze/server/ServerConfigTest.java index 7760147..999bb0f 100644 --- a/src/test/java/ch/fritteli/maze/server/ServerConfigTest.java +++ b/src/test/java/ch/fritteli/maze/server/ServerConfigTest.java @@ -3,8 +3,6 @@ package ch.fritteli.maze.server; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import java.net.UnknownHostException; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -22,9 +20,9 @@ class ServerConfigTest { final ServerConfig sut = ServerConfig.init(); // assert - assertEquals("127.0.0.1", sut.getAddress().getHostAddress()); - assertEquals("localhost", sut.getAddress().getHostName()); - assertEquals(0, sut.getPort()); + assertEquals("127.0.0.1", sut.address().getHostAddress()); + assertEquals("localhost", sut.address().getHostName()); + assertEquals(0, sut.port()); } @Test @@ -46,7 +44,7 @@ class ServerConfigTest { } @Test - void testInit() throws ConfigurationException, UnknownHostException { + void testInit() throws ConfigurationException { // arrange System.setProperty(ServerConfig.SYSPROP_HOST, "127.0.0.1"); System.setProperty(ServerConfig.SYSPROP_PORT, "12345"); @@ -55,7 +53,7 @@ class ServerConfigTest { final ServerConfig sut = ServerConfig.init(); // assert - assertEquals("127.0.0.1", sut.getAddress().getHostAddress()); - assertEquals(12345, sut.getPort()); + assertEquals("127.0.0.1", sut.address().getHostAddress()); + assertEquals(12345, sut.port()); } }