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) { 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) { public void POST(String path, HttpHandler handler) {
router.addRoute("POST", path, handler); router.addRoute(HttpMethod.POST, path, handler);
} }
public void start() { public void start() {

View File

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

View File

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