route to record class, better actions

This commit is contained in:
Daniel
2024-10-23 21:58:28 +03:00
parent ae0d34711c
commit a9d7494e1a
3 changed files with 7 additions and 14 deletions

View File

@@ -29,11 +29,11 @@ public class HttpServer {
}
public void GET(String path, HttpHandler handler) {
router.addRoute("GET", path, handler);
router.addRoute(HttpMethod.GET, path, handler);
}
public void POST(String path, HttpHandler handler) {
router.addRoute("POST", path, handler);
router.addRoute(HttpMethod.POST, path, handler);
}
public void start() {

View File

@@ -1,15 +1,7 @@
package org.lumijiez.core.routing;
public class Route {
private final HttpMethod method;
private final String path;
private final HttpHandler handler;
import org.lumijiez.core.http.HttpHandler;
import org.lumijiez.core.http.HttpMethod;
public Route(HttpMethod method, String path, HttpHandler handler) {
this.method = method;
this.path = path;
this.handler = handler;
}
// Add getters...
public record Route(HttpMethod method, String path, HttpHandler handler) {
}

View File

@@ -1,6 +1,7 @@
package org.lumijiez.core.routing;
import org.lumijiez.core.http.HttpHandler;
import org.lumijiez.core.http.HttpMethod;
import org.lumijiez.core.http.HttpRequest;
import org.lumijiez.core.http.HttpResponse;
import org.lumijiez.core.middleware.Chain;
@@ -49,7 +50,7 @@ public class Router {
Route route = routes.get(key);
if (route != null) {
route.getHandler().handle(request, response);
route.handler().handle(request, response);
} else {
response.sendResponse(HttpStatus.NOT_FOUND, "Not Found");
}