2323 * @author Simon Reinisch
2424 * @implNote Core modul, don't change anything.
2525 * <p>
26- * An NodeJS like clone written in Java.
26+ * Core class of java-express
2727 */
28- public class Express extends ExpressMiddleware {
28+ public class Express extends ExpressMiddleware implements Router {
2929
3030 private final ConcurrentHashMap <String , HttpRequest > PARAMETER_LISTENER ;
3131 private final ConcurrentHashMap <Object , Object > LOCALS ;
@@ -44,7 +44,7 @@ public class Express extends ExpressMiddleware {
4444 LOCALS = new ConcurrentHashMap <>();
4545
4646 WORKER = new ArrayList <>();
47- HANDLER = new FilterLayerHandler ();
47+ HANDLER = new FilterLayerHandler (2 );
4848
4949 executor = Executors .newCachedThreadPool ();
5050 hostname = "localhost" ;
@@ -65,7 +65,7 @@ public Express(@NotNull String hostname) {
6565 *
6666 * @param httpsConfigurator The HttpsConfigurator for https
6767 */
68- public Express (HttpsConfigurator httpsConfigurator ) {
68+ public Express (@ NotNull HttpsConfigurator httpsConfigurator ) {
6969 this .httpsConfigurator = httpsConfigurator ;
7070 }
7171
@@ -146,31 +146,23 @@ public void setExecutor(@NotNull Executor executor) throws IOException {
146146 }
147147
148148 /**
149- * Add an middleware which will be firea BEFORE EACH request-type listener will be fired .
149+ * Add an routing object .
150150 *
151- * @param middleware An middleware which will be fired on every equestmethod- and path .
151+ * @param router The router .
152152 */
153+ public void use (@ NotNull ExpressRouter router ) {
154+ this .HANDLER .combine (router .getHandler ());
155+ this .WORKER .addAll (router .getWorker ());
156+ }
157+
153158 public void use (@ NotNull HttpRequest middleware ) {
154159 addMiddleware ("*" , "*" , middleware );
155160 }
156161
157- /**
158- * Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
159- *
160- * @param context The context where the middleware should listen.
161- * @param middleware An middleware which will be fired if the context matches the requestpath.
162- */
163162 public void use (@ NotNull String context , @ NotNull HttpRequest middleware ) {
164163 addMiddleware ("*" , context , middleware );
165164 }
166165
167- /**
168- * Add an middleware which will be firea BEFORE EACH request-type listener will be fired.
169- *
170- * @param context The context where the middleware should listen for the request handler..
171- * @param requestMethod And type of request-method eg. GET, POST etc.
172- * @param middleware An middleware which will be fired if the context matches the requestmethod- and path.
173- */
174166 public void use (@ NotNull String context , @ NotNull String requestMethod , @ NotNull HttpRequest middleware ) {
175167 addMiddleware (requestMethod .toUpperCase (), context , middleware );
176168 }
@@ -181,100 +173,43 @@ private void addMiddleware(@NotNull String requestMethod, @NotNull String contex
181173 WORKER .add (new ExpressFilterWorker ((ExpressFilterTask ) middleware ));
182174 }
183175
184- HANDLER .add (0 , new ExpressFilterImpl (this , requestMethod , context , middleware ));
176+ HANDLER .add (0 , new ExpressFilterImpl (requestMethod , context , middleware ));
185177 }
186178
187- /**
188- * Add an listener for all request methods and contexts.
189- *
190- * @param request Will be fired on all requests.
191- */
192179 public void all (@ NotNull HttpRequest request ) {
193- HANDLER .add (1 , new ExpressFilterImpl (this , "*" , "*" , request ));
180+ HANDLER .add (1 , new ExpressFilterImpl ("*" , "*" , request ));
194181 }
195182
196- /**
197- * Adds an handler for a specific context.
198- *
199- * @param context The context.
200- * @param request An listener which will be fired if the context matches the requestpath.
201- */
202183 public void all (@ NotNull String context , @ NotNull HttpRequest request ) {
203- HANDLER .add (1 , new ExpressFilterImpl (this , "*" , context , request ));
184+ HANDLER .add (1 , new ExpressFilterImpl ("*" , context , request ));
204185 }
205186
206- /**
207- * Adds an handler for a specific context and method.
208- * You can use a star '*' to match every context / request-method.
209- *
210- * @param context The context.
211- * @param requestMethod The request method.
212- * @param request An listener which will be fired if the context matches the requestpath.
213- */
214187 public void all (@ NotNull String context , @ NotNull String requestMethod , @ NotNull HttpRequest request ) {
215- HANDLER .add (1 , new ExpressFilterImpl (this , requestMethod , context , request ));
188+ HANDLER .add (1 , new ExpressFilterImpl (requestMethod , context , request ));
216189 }
217190
218- /**
219- * Add an listener for GET request's.
220- *
221- * @param context The context.
222- * @param request An listener which will be fired if the context matches the requestpath.
223- */
224191 public void get (@ NotNull String context , @ NotNull HttpRequest request ) {
225- HANDLER .add (1 , new ExpressFilterImpl (this , "GET" , context , request ));
192+ HANDLER .add (1 , new ExpressFilterImpl ("GET" , context , request ));
226193 }
227194
228- /**
229- * Add an listener for POST request's.
230- *
231- * @param context The context.
232- * @param request An listener which will be fired if the context matches the requestpath.
233- */
234195 public void post (@ NotNull String context , @ NotNull HttpRequest request ) {
235- HANDLER .add (1 , new ExpressFilterImpl (this , "POST" , context , request ));
196+ HANDLER .add (1 , new ExpressFilterImpl ("POST" , context , request ));
236197 }
237198
238- /**
239- * Add an listener for PUT request's.
240- *
241- * @param context The context for the request handler..
242- * @param request An listener which will be fired if the context matches the requestpath.
243- */
244199 public void put (@ NotNull String context , @ NotNull HttpRequest request ) {
245- HANDLER .add (1 , new ExpressFilterImpl (this , "PUT" , context , request ));
200+ HANDLER .add (1 , new ExpressFilterImpl ("PUT" , context , request ));
246201 }
247202
248- /**
249- * Add an listener for DELETE request's.
250- *
251- * @param context The context.
252- * @param request An listener which will be fired if the context matches the requestpath.
253- */
254203 public void delete (@ NotNull String context , @ NotNull HttpRequest request ) {
255- HANDLER .add (1 , new ExpressFilterImpl (this , "DELETE" , context , request ));
204+ HANDLER .add (1 , new ExpressFilterImpl ("DELETE" , context , request ));
256205 }
257206
258- /**
259- * Add an listener for PATCH request's.
260- *
261- * @param context The context.
262- * @param request An listener which will be fired if the context matches the requestpath.
263- */
264207 public void patch (@ NotNull String context , @ NotNull HttpRequest request ) {
265- HANDLER .add (1 , new ExpressFilterImpl (this , "PATCH" , context , request ));
208+ HANDLER .add (1 , new ExpressFilterImpl ("PATCH" , context , request ));
266209 }
267210
268- /**
269- * Adds an handler for a specific context and method.
270- * You can use a star '*' to match every context / request-method.
271- *
272- * @param context The context.
273- * @param requestMethod The request method.
274- * @param request An listener which will be fired if the context matches the requestpath.
275- */
276211 public void on (@ NotNull String context , @ NotNull String requestMethod , @ NotNull HttpRequest request ) {
277- HANDLER .add (1 , new ExpressFilterImpl (this , requestMethod , context , request ));
212+ HANDLER .add (1 , new ExpressFilterImpl (requestMethod , context , request ));
278213 }
279214
280215 /**
@@ -336,9 +271,16 @@ public void listen(Action onStart, int port) throws IOException {
336271 httpServer = HttpServer .create (socketAddress , 0 );
337272 }
338273
339- httpServer .setExecutor (executor ); // Set thread executor
340- httpServer .createContext ("/" , HANDLER ); // Set handler for all contexts
341- httpServer .start (); // Start server
274+ // Set thread executor
275+ httpServer .setExecutor (executor );
276+
277+ // Create handler for all contexts
278+ httpServer .createContext ("/" ,httpExchange -> {
279+ HANDLER .handle (httpExchange , this );
280+ });
281+
282+ // Start server
283+ httpServer .start ();
342284
343285 // Fire listener
344286 if (onStart != null )
0 commit comments