Skip to content

Commit 6dc3379

Browse files
committed
feat: add main class to run standalone
1 parent e35ac3e commit 6dc3379

4 files changed

Lines changed: 223 additions & 2 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ Add as a dependency to your project:
3131
</dependency>
3232
```
3333

34+
## Running as Standalone Proxy
35+
36+
You can run the proxy as a standalone application without writing any code:
37+
38+
```bash
39+
# Run with defaults (CACHE mode on port 3128, storing to "proxy-cache")
40+
java -cp target/java-webreplay-0.0.1-SNAPSHOT.jar org.codejive.webreplay.Main
41+
```
42+
43+
**Available options:**
44+
- `-p, --port <port>` - Port to run the proxy on (default: 3128)
45+
- `-d, --dir <directory>` - Directory for storing cached requests (default: proxy-cache)
46+
- `-m, --mode <mode>` - Replay mode: RECORD, CACHE, or REPLAY (default: CACHE)
47+
- `-h, --help` - Show help message
48+
49+
Once running, configure your browser or application to use `localhost:<port>` as the HTTP/HTTPS proxy.
50+
3451
## Quick Start
3552

3653
### Basic Recording and Replay

app.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ links:
1414
documentation: https://github.com/codejive/java-webreplay/blob/main/README.md
1515
java: 21
1616
dependencies:
17+
- org.codejive.tproxy:tproxy:0.0.1-SNAPSHOT
18+
- com.google.code.gson:gson:2.11.0
1719
actions:
1820
clean: ./mvnw clean
19-
build: ./mvnw spotless:apply package -DskipTests
21+
build: ./mvnw spotless:apply install -DskipTests
2022
test: ./mvnw test
2123
format: ./mvnw spotless:apply
24+
run: jbang org.codejive.webreplay:webreplay:0.0.1-SNAPSHOT --record --port 8080

pom.xml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77

88
<groupId>org.codejive.webreplay</groupId>
9-
<artifactId>java-webreplay</artifactId>
9+
<artifactId>webreplay</artifactId>
1010
<version>0.0.1-SNAPSHOT</version>
1111
<packaging>jar</packaging>
1212

@@ -184,6 +184,19 @@
184184
</execution>
185185
</executions>
186186
</plugin>
187+
188+
<!-- Configure JAR with Main-Class -->
189+
<plugin>
190+
<groupId>org.apache.maven.plugins</groupId>
191+
<artifactId>maven-jar-plugin</artifactId>
192+
<configuration>
193+
<archive>
194+
<manifest>
195+
<mainClass>org.codejive.webreplay.Main</mainClass>
196+
</manifest>
197+
</archive>
198+
</configuration>
199+
</plugin>
187200
</plugins>
188201
</build>
189202
</project>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package org.codejive.webreplay;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Path;
5+
import java.nio.file.Paths;
6+
7+
/**
8+
* Main entry point for running the WebReplayProxy as a standalone application.
9+
*
10+
* <p>Usage:
11+
*
12+
* <pre>
13+
* java -jar webreplay.jar [options]
14+
*
15+
* Options:
16+
* -p, --port &lt;port&gt; Port to run the proxy on (default: 3128)
17+
* -d, --dir &lt;directory&gt; Directory for storing cached requests (default: proxy-cache)
18+
* -m, --mode &lt;mode&gt; Replay mode: RECORD, CACHE, or REPLAY (default: CACHE)
19+
* -h, --help Show this help message
20+
* </pre>
21+
*/
22+
public class Main {
23+
private static final int DEFAULT_PORT = 3128;
24+
private static final String DEFAULT_DIRECTORY = "proxy-cache";
25+
private static final ReplayMode DEFAULT_MODE = ReplayMode.CACHE;
26+
27+
public static void main(String[] args) {
28+
try {
29+
Config config = parseArgs(args);
30+
31+
if (config.showHelp) {
32+
showHelp();
33+
return;
34+
}
35+
36+
startProxy(config);
37+
} catch (IllegalArgumentException e) {
38+
System.err.println("Error: " + e.getMessage());
39+
System.err.println();
40+
showHelp();
41+
System.exit(1);
42+
} catch (IOException e) {
43+
System.err.println("Failed to start proxy: " + e.getMessage());
44+
e.printStackTrace();
45+
System.exit(1);
46+
}
47+
}
48+
49+
private static void startProxy(Config config) throws IOException {
50+
System.out.println("Starting WebReplayProxy...");
51+
System.out.println(" Mode: " + config.mode);
52+
System.out.println(" Port: " + config.port);
53+
System.out.println(" Storage: " + config.storageDirectory);
54+
System.out.println();
55+
56+
WebReplayProxy proxy =
57+
WebReplayProxy.builder()
58+
.mode(config.mode)
59+
.storageDirectory(config.storageDirectory)
60+
.build();
61+
62+
proxy.start(config.port);
63+
64+
System.out.println("Proxy started successfully!");
65+
System.out.println(
66+
"Configure your browser or application to use proxy: localhost:" + config.port);
67+
System.out.println("Press Ctrl+C to stop");
68+
69+
// Add shutdown hook to gracefully stop the proxy
70+
Runtime.getRuntime()
71+
.addShutdownHook(
72+
new Thread(
73+
() -> {
74+
System.out.println("\nStopping proxy...");
75+
proxy.stop();
76+
}));
77+
78+
// Keep the main thread alive
79+
try {
80+
Thread.currentThread().join();
81+
} catch (InterruptedException e) {
82+
Thread.currentThread().interrupt();
83+
}
84+
}
85+
86+
private static Config parseArgs(String[] args) {
87+
Config config = new Config();
88+
89+
for (int i = 0; i < args.length; i++) {
90+
String arg = args[i];
91+
92+
switch (arg) {
93+
case "-h":
94+
case "--help":
95+
config.showHelp = true;
96+
return config;
97+
98+
case "-p":
99+
case "--port":
100+
if (i + 1 >= args.length) {
101+
throw new IllegalArgumentException("Missing value for " + arg);
102+
}
103+
try {
104+
config.port = Integer.parseInt(args[++i]);
105+
if (config.port < 1 || config.port > 65535) {
106+
throw new IllegalArgumentException("Port must be between 1 and 65535");
107+
}
108+
} catch (NumberFormatException e) {
109+
throw new IllegalArgumentException("Invalid port number: " + args[i]);
110+
}
111+
break;
112+
113+
case "-d":
114+
case "--dir":
115+
if (i + 1 >= args.length) {
116+
throw new IllegalArgumentException("Missing value for " + arg);
117+
}
118+
config.storageDirectory = Paths.get(args[++i]);
119+
break;
120+
121+
case "-m":
122+
case "--mode":
123+
if (i + 1 >= args.length) {
124+
throw new IllegalArgumentException("Missing value for " + arg);
125+
}
126+
try {
127+
config.mode = ReplayMode.valueOf(args[++i].toUpperCase());
128+
} catch (IllegalArgumentException e) {
129+
throw new IllegalArgumentException(
130+
"Invalid mode: " + args[i] + ". Must be RECORD, CACHE, or REPLAY");
131+
}
132+
break;
133+
134+
default:
135+
throw new IllegalArgumentException("Unknown option: " + arg);
136+
}
137+
}
138+
139+
return config;
140+
}
141+
142+
private static void showHelp() {
143+
System.out.println(
144+
"WebReplayProxy - HTTP/HTTPS proxy with recording and replay capabilities");
145+
System.out.println();
146+
System.out.println("Usage: java -jar webreplay.jar [options]");
147+
System.out.println();
148+
System.out.println("Options:");
149+
System.out.println(
150+
" -p, --port <port> Port to run the proxy on (default: "
151+
+ DEFAULT_PORT
152+
+ ")");
153+
System.out.println(
154+
" -d, --dir <directory> Directory for storing cached requests (default: "
155+
+ DEFAULT_DIRECTORY
156+
+ ")");
157+
System.out.println(
158+
" -m, --mode <mode> Replay mode: RECORD, CACHE, or REPLAY (default: "
159+
+ DEFAULT_MODE
160+
+ ")");
161+
System.out.println(" -h, --help Show this help message");
162+
System.out.println();
163+
System.out.println("Modes:");
164+
System.out.println(" RECORD - All requests pass through and are recorded");
165+
System.out.println(
166+
" CACHE - Cached responses returned when available, otherwise pass through and"
167+
+ " record");
168+
System.out.println(
169+
" REPLAY - Only cached responses returned; non-matching requests return 404");
170+
System.out.println();
171+
System.out.println("Examples:");
172+
System.out.println(" # Start with defaults (CACHE mode on port 3128)");
173+
System.out.println(" java -jar webreplay.jar");
174+
System.out.println();
175+
System.out.println(" # Start in RECORD mode on port 8080");
176+
System.out.println(" java -jar webreplay.jar --port 8080 --mode RECORD");
177+
System.out.println();
178+
System.out.println(" # Use custom storage directory");
179+
System.out.println(" java -jar webreplay.jar --dir /path/to/cache");
180+
}
181+
182+
private static class Config {
183+
int port = DEFAULT_PORT;
184+
Path storageDirectory = Paths.get(DEFAULT_DIRECTORY);
185+
ReplayMode mode = DEFAULT_MODE;
186+
boolean showHelp = false;
187+
}
188+
}

0 commit comments

Comments
 (0)