Skip to content

Commit 78f7e21

Browse files
Resolve port: CLI > config > default (#29)
* Resolve port: CLI > config > default * Wire port resolution to AppConfig/ConfigLoader and update docs/tests * Update PortConfigurationGuide.md * Update PortConfigurationGuide.md * Remove ServerPortResolver; use ConfigLoader for port * Update PortConfigurationGuide.md * Update PortConfigurationGuide.md * may be done
1 parent 6950c14 commit 78f7e21

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

PortConfigurationGuide.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Konfiguration: port (CLI → config-fil → default)
2+
3+
Det här projektet väljer vilken port servern ska starta på enligt följande prioritet:
4+
5+
1. **CLI-argument** (`--port <port>`) – högst prioritet
6+
2. **Config-fil** (`application.yml`: `server.port`)
7+
3. **Default** (`8080`) – används om port saknas i config eller om config-filen saknas
8+
9+
---
10+
11+
## 1) Default-värde
12+
13+
Om varken CLI eller config anger port används:
14+
15+
- **8080** (default för `server.port` i `AppConfig`)
16+
17+
---
18+
19+
## 2) Config-fil: `application.yml`
20+
21+
### Var ska filen ligga?
22+
Standard:
23+
- `src/main/resources/application.yml`
24+
25+
### Exempel
26+
```yaml
27+
server:
28+
port: 9090
29+
```
30+
31+
---
32+
33+
## 3) CLI-argument
34+
35+
CLI kan användas för att override:a config:
36+
37+
```bash
38+
java -cp target/classes org.example.App --port 8000
39+
```
40+
41+
---
42+
43+
## 4) Sammanfattning
44+
45+
Prioritet:
46+
47+
1. CLI (`--port`)
48+
2. `application.yml` (`server.port`)
49+
3. Default (`8080`)

src/main/java/org/example/App.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,50 @@
66
import java.nio.file.Path;
77

88
public class App {
9+
10+
private static final String PORT_FLAG = "--port";
11+
912
public static void main(String[] args) {
1013
Path configPath = Path.of("src/main/resources/application.yml");
1114

1215
AppConfig appConfig = ConfigLoader.loadOnce(configPath);
13-
int port = appConfig.server().port();
16+
17+
int port = resolvePort(args, appConfig.server().port());
18+
1419
new TcpServer(port).start();
1520
}
21+
22+
static int resolvePort(String[] args, int configPort) {
23+
Integer cliPort = parsePortFromCli(args);
24+
if (cliPort != null) {
25+
return validatePort(cliPort, "CLI argument " + PORT_FLAG);
26+
}
27+
return validatePort(configPort, "configuration server.port");
28+
}
29+
30+
static Integer parsePortFromCli(String[] args) {
31+
if (args == null) return null;
32+
33+
for (int i = 0; i < args.length; i++) {
34+
if (PORT_FLAG.equals(args[i])) {
35+
int valueIndex = i + 1;
36+
if (valueIndex >= args.length) {
37+
throw new IllegalArgumentException("Missing value after " + PORT_FLAG);
38+
}
39+
try {
40+
return Integer.parseInt(args[valueIndex]);
41+
} catch (NumberFormatException e) {
42+
throw new IllegalArgumentException("Invalid port value after " + PORT_FLAG + ": " + args[valueIndex], e);
43+
}
44+
}
45+
}
46+
return null;
47+
}
48+
49+
static int validatePort(int port, String source) {
50+
if (port < 1 || port > 65535) {
51+
throw new IllegalArgumentException("Port out of range (1-65535) from " + source + ": " + port);
52+
}
53+
return port;
54+
}
1655
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example;
2+
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.assertj.core.api.Assertions.assertThat;
7+
8+
class AppPortResolutionTest {
9+
10+
@Test
11+
void cli_port_wins_over_config() {
12+
int port = App.resolvePort(new String[]{"--port", "8000"}, 9090);
13+
assertThat(port).isEqualTo(8000);
14+
}
15+
16+
@Test
17+
void config_port_used_when_no_cli_arg() {
18+
int port = App.resolvePort(new String[]{}, 9090);
19+
assertThat(port).isEqualTo(9090);
20+
}
21+
}

0 commit comments

Comments
 (0)