3
3
import dev .latvian .apps .tinyserver .http .HTTPHandler ;
4
4
import dev .latvian .apps .tinyserver .http .HTTPMethod ;
5
5
import dev .latvian .apps .tinyserver .http .HTTPRequest ;
6
- import dev .latvian .apps .tinyserver .http .PathFileHandler ;
6
+ import dev .latvian .apps .tinyserver .http .file .DynamicFileHandler ;
7
+ import dev .latvian .apps .tinyserver .http .file .FileIndexHandler ;
8
+ import dev .latvian .apps .tinyserver .http .file .FileResponseHandler ;
9
+ import dev .latvian .apps .tinyserver .http .file .SingleFileHandler ;
7
10
import dev .latvian .apps .tinyserver .http .response .HTTPResponse ;
11
+ import dev .latvian .apps .tinyserver .ws .WSEndpointHandler ;
8
12
import dev .latvian .apps .tinyserver .ws .WSHandler ;
9
13
import dev .latvian .apps .tinyserver .ws .WSSession ;
10
14
import dev .latvian .apps .tinyserver .ws .WSSessionFactory ;
11
15
16
+ import java .io .IOException ;
17
+ import java .nio .file .Files ;
12
18
import java .nio .file .Path ;
13
- import java .time . Duration ;
19
+ import java .util . concurrent . ConcurrentHashMap ;
14
20
import java .util .function .Consumer ;
15
21
16
22
public interface ServerRegistry <REQ extends HTTPRequest > {
@@ -55,8 +61,12 @@ default void redirect(String path, String redirect) {
55
61
get (path , req -> res );
56
62
}
57
63
58
- default void files (String path , Path directory , Duration cacheDuration , boolean autoIndex ) {
59
- var handler = new PathFileHandler <REQ >(path , directory , cacheDuration , autoIndex );
64
+ default void singleFile (String path , Path file , FileResponseHandler responseHandler ) throws IOException {
65
+ get (path , new SingleFileHandler <>(file , Files .probeContentType (file ), responseHandler ));
66
+ }
67
+
68
+ default void dynamicFiles (String path , Path directory , FileResponseHandler responseHandler , boolean autoIndex ) {
69
+ var handler = new DynamicFileHandler <REQ >(directory , responseHandler , autoIndex );
60
70
61
71
if (autoIndex ) {
62
72
get (path , handler );
@@ -65,7 +75,25 @@ default void files(String path, Path directory, Duration cacheDuration, boolean
65
75
get (path + "/<path>" , handler );
66
76
}
67
77
68
- <WSS extends WSSession <REQ >> WSHandler <REQ , WSS > ws (String path , WSSessionFactory <REQ , WSS > factory );
78
+ default void staticFiles (String path , Path directory , FileResponseHandler responseHandler , boolean autoIndex ) throws IOException {
79
+ path = path .endsWith ("/" ) ? path : (path + "/" );
80
+
81
+ for (var file : Files .walk (directory ).filter (Files ::isReadable ).toList ()) {
82
+ var rpath = directory .relativize (file ).toString ();
83
+
84
+ if (Files .isRegularFile (file )) {
85
+ singleFile (path + rpath , file , responseHandler );
86
+ } else if (autoIndex && Files .isDirectory (file )) {
87
+ get (path + rpath , new FileIndexHandler <>(directory , file , responseHandler ));
88
+ }
89
+ }
90
+ }
91
+
92
+ default <WSS extends WSSession <REQ >> WSHandler <REQ , WSS > ws (String path , WSSessionFactory <REQ , WSS > factory ) {
93
+ var handler = new WSEndpointHandler <>(factory , new ConcurrentHashMap <>());
94
+ get (path , handler );
95
+ return handler ;
96
+ }
69
97
70
98
default <WSS extends WSSession <REQ >> WSHandler <REQ , WSS > ws (String path ) {
71
99
return ws (path , (WSSessionFactory ) WSSessionFactory .DEFAULT );
0 commit comments