Use record for ServerConfig.
This commit is contained in:
		
							parent
							
								
									dbdfb0c8a4
								
							
						
					
					
						commit
						f370037289
					
				
					 3 changed files with 19 additions and 26 deletions
				
			
		|  | @ -18,8 +18,8 @@ public class MazeServer { | ||||||
|     private final Undertow undertow; |     private final Undertow undertow; | ||||||
| 
 | 
 | ||||||
|     private MazeServer(@NotNull final ServerConfig config) { |     private MazeServer(@NotNull final ServerConfig config) { | ||||||
|         final String hostAddress = config.getAddress().getHostAddress(); |         final String hostAddress = config.address().getHostAddress(); | ||||||
|         final int port = config.getPort(); |         final int port = config.port(); | ||||||
|         log.info("Starting Server at http://{}:{}/", hostAddress, port); |         log.info("Starting Server at http://{}:{}/", hostAddress, port); | ||||||
|         final RoutingHandler routingHandler = new RoutingHandler() |         final RoutingHandler routingHandler = new RoutingHandler() | ||||||
|                 .get(CreateHandler.PATH_TEMPLATE, new CreateHandler()) |                 .get(CreateHandler.PATH_TEMPLATE, new CreateHandler()) | ||||||
|  |  | ||||||
|  | @ -1,32 +1,27 @@ | ||||||
| package ch.fritteli.maze.server; | package ch.fritteli.maze.server; | ||||||
| 
 | 
 | ||||||
| import io.vavr.control.Try; | import io.vavr.control.Try; | ||||||
| import lombok.AccessLevel; |  | ||||||
| import lombok.AllArgsConstructor; |  | ||||||
| import lombok.Value; |  | ||||||
| import lombok.extern.slf4j.Slf4j; | import lombok.extern.slf4j.Slf4j; | ||||||
|  | import org.jetbrains.annotations.NotNull; | ||||||
| import org.jetbrains.annotations.Nullable; | import org.jetbrains.annotations.Nullable; | ||||||
| import org.wildfly.common.annotation.NotNull; |  | ||||||
| 
 | 
 | ||||||
| import java.net.InetAddress; | import java.net.InetAddress; | ||||||
| 
 | 
 | ||||||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) |  | ||||||
| @Slf4j | @Slf4j | ||||||
| @Value | public record ServerConfig(@NotNull InetAddress address, int port) { | ||||||
| public class ServerConfig { |  | ||||||
|     public static final String SYSPROP_HOST = "fritteli.maze.server.host"; |     public static final String SYSPROP_HOST = "fritteli.maze.server.host"; | ||||||
|     public static final String SYSPROP_PORT = "fritteli.maze.server.port"; |     public static final String SYSPROP_PORT = "fritteli.maze.server.port"; | ||||||
| 
 | 
 | ||||||
|     @NotNull |     public ServerConfig(@NotNull final InetAddress address, int port) { | ||||||
|     InetAddress address; |         this.address = address; | ||||||
|     int port; |  | ||||||
| 
 |  | ||||||
|     public ServerConfig(@Nullable final String address, final int port) throws ConfigurationException { |  | ||||||
|         this.address = validateAddress(address); |  | ||||||
|         this.port = validatePort(port); |         this.port = validatePort(port); | ||||||
|         log.debug("host={}, port={}", this.address, this.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 |     @NotNull | ||||||
|     public static ServerConfig init() throws ConfigurationException { |     public static ServerConfig init() throws ConfigurationException { | ||||||
|         final String host = System.getProperty(SYSPROP_HOST); |         final String host = System.getProperty(SYSPROP_HOST); | ||||||
|  | @ -36,7 +31,7 @@ public class ServerConfig { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @NotNull |     @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)) |         return Try.of(() -> InetAddress.getByName(address)) | ||||||
|                 .getOrElseThrow(cause -> new ConfigurationException( |                 .getOrElseThrow(cause -> new ConfigurationException( | ||||||
|                         "Invalid hostname/address: %s".formatted(address), |                         "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) { |         if (port < 0 || port > 0xFFFF) { | ||||||
|             throw new ConfigurationException("Port out of range (0..65535): %s".formatted(port)); |             throw new ConfigurationException("Port out of range (0..65535): %s".formatted(port)); | ||||||
|         } |         } | ||||||
|         return port; |         return port; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     private static int validatePort(@Nullable final String portString) { |     private static int validatePort(@Nullable final String portString) throws ConfigurationException { | ||||||
|         if (portString == null) { |         if (portString == null) { | ||||||
|             log.info("No port configured; using default."); |             log.info("No port configured; using default."); | ||||||
|             return 0; |             return 0; | ||||||
|  |  | ||||||
|  | @ -3,8 +3,6 @@ package ch.fritteli.maze.server; | ||||||
| import org.junit.jupiter.api.BeforeEach; | import org.junit.jupiter.api.BeforeEach; | ||||||
| import org.junit.jupiter.api.Test; | 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.assertEquals; | ||||||
| import static org.junit.jupiter.api.Assertions.assertThrows; | import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||
| 
 | 
 | ||||||
|  | @ -22,9 +20,9 @@ class ServerConfigTest { | ||||||
|         final ServerConfig sut = ServerConfig.init(); |         final ServerConfig sut = ServerConfig.init(); | ||||||
| 
 | 
 | ||||||
|         // assert |         // assert | ||||||
|         assertEquals("127.0.0.1", sut.getAddress().getHostAddress()); |         assertEquals("127.0.0.1", sut.address().getHostAddress()); | ||||||
|         assertEquals("localhost", sut.getAddress().getHostName()); |         assertEquals("localhost", sut.address().getHostName()); | ||||||
|         assertEquals(0, sut.getPort()); |         assertEquals(0, sut.port()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|  | @ -46,7 +44,7 @@ class ServerConfigTest { | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Test |     @Test | ||||||
|     void testInit() throws ConfigurationException, UnknownHostException { |     void testInit() throws ConfigurationException { | ||||||
|         // arrange |         // arrange | ||||||
|         System.setProperty(ServerConfig.SYSPROP_HOST, "127.0.0.1"); |         System.setProperty(ServerConfig.SYSPROP_HOST, "127.0.0.1"); | ||||||
|         System.setProperty(ServerConfig.SYSPROP_PORT, "12345"); |         System.setProperty(ServerConfig.SYSPROP_PORT, "12345"); | ||||||
|  | @ -55,7 +53,7 @@ class ServerConfigTest { | ||||||
|         final ServerConfig sut = ServerConfig.init(); |         final ServerConfig sut = ServerConfig.init(); | ||||||
| 
 | 
 | ||||||
|         // assert |         // assert | ||||||
|         assertEquals("127.0.0.1", sut.getAddress().getHostAddress()); |         assertEquals("127.0.0.1", sut.address().getHostAddress()); | ||||||
|         assertEquals(12345, sut.getPort()); |         assertEquals(12345, sut.port()); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue