20
20
21
21
import java .io .*;
22
22
import java .net .InetSocketAddress ;
23
+ import java .nio .file .Path ;
23
24
import java .util .*;
24
25
import java .util .concurrent .CompletableFuture ;
25
26
import java .util .concurrent .Future ;
@@ -40,6 +41,7 @@ public class Server implements HttpHandler {
40
41
private final Map <String , String > csp = Collections .synchronizedMap (new HashMap <>());
41
42
private final Map <String , HttpHandler > routes = Collections .synchronizedMap (new HashMap <>());
42
43
private final Set <String > gzipRoutes = Collections .synchronizedSet (new HashSet <>());
44
+ private java .nio .file .Path staticFilesDirectory ;
43
45
44
46
private static class Auth {
45
47
public final String user ;
@@ -93,6 +95,10 @@ void enableGzip(String path) {
93
95
gzipRoutes .add (path );
94
96
}
95
97
98
+ void setStaticFilesDirectory (Path staticFilesDirectory ) {
99
+ this .staticFilesDirectory = staticFilesDirectory ;
100
+ }
101
+
96
102
static class Request {
97
103
public final String url ;
98
104
public final String method ;
@@ -187,7 +193,19 @@ public void handle(HttpExchange exchange) throws IOException {
187
193
path = "/index.html" ;
188
194
}
189
195
190
- // Resources from "src/test/resources/" are copied to "resources/" directory in the jar.
196
+ // If static files directory is set, serve from filesystem first
197
+ if (staticFilesDirectory != null ) {
198
+ java .nio .file .Path filePath = staticFilesDirectory .resolve (path .substring (1 )); // Remove leading /
199
+ if (java .nio .file .Files .exists (filePath ) && !java .nio .file .Files .isDirectory (filePath )) {
200
+ exchange .getResponseHeaders ().add ("Content-Type" , mimeType (filePath .toFile ()));
201
+ exchange .sendResponseHeaders (200 , java .nio .file .Files .size (filePath ));
202
+ java .nio .file .Files .copy (filePath , exchange .getResponseBody ());
203
+ exchange .getResponseBody ().close ();
204
+ return ;
205
+ }
206
+ }
207
+
208
+ // Fallback: Resources from "src/test/resources/" are copied to "resources/" directory in the jar.
191
209
String resourcePath = "resources" + path ;
192
210
InputStream resource = getClass ().getClassLoader ().getResourceAsStream (resourcePath );
193
211
if (resource == null ) {
0 commit comments