improved path logic
~5 min work
This commit is contained in:
parent
966373e629
commit
5be9b687b5
@ -8,7 +8,7 @@ import java.io.IOException;
|
|||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
Router router = new Router();
|
Router router = new Router();
|
||||||
router.addRoute(HttpMethod.GET, "/test/{var}/service", new TestService(), 1);
|
router.addRoute(HttpMethod.GET, "/test/{var}/service", new TestService(), 2);
|
||||||
|
|
||||||
Server server = new Server(10001, 10, router);
|
Server server = new Server(10001, 10, router);
|
||||||
server.start();
|
server.start();
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package at.nanopenguin.mtcg.http;
|
|||||||
import at.nanopenguin.mtcg.application.Service;
|
import at.nanopenguin.mtcg.application.Service;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.IntStream;
|
|
||||||
|
|
||||||
public class Router {
|
public class Router {
|
||||||
private Map<HttpMethod, Map<String, Route>> routeMap = new HashMap<>();
|
private Map<HttpMethod, Map<String, Route>> routeMap = new HashMap<>();
|
||||||
@ -15,8 +14,9 @@ public class Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> routeComponents = new ArrayList<String>(Arrays.asList(route.split("/")));
|
List<String> routeComponents = new ArrayList<String>(Arrays.asList(route.split("/")));
|
||||||
routeComponents.remove(0);
|
if (pathVarPos > 0) {
|
||||||
routeComponents.set(pathVarPos, "{var}");
|
routeComponents.set(pathVarPos, "{var}");
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < routeComponents.size(); i++) {
|
for (int i = 0; i < routeComponents.size(); i++) {
|
||||||
Route routeComponent = new Route(i == routeComponents.size() - 1 ? service : null, i == pathVarPos - 1);
|
Route routeComponent = new Route(i == routeComponents.size() - 1 ? service : null, i == pathVarPos - 1);
|
||||||
@ -29,18 +29,13 @@ public class Router {
|
|||||||
public Service resolveRoute(final HttpMethod method, final String route) {
|
public Service resolveRoute(final HttpMethod method, final String route) {
|
||||||
System.out.println("resolving route " + route);
|
System.out.println("resolving route " + route);
|
||||||
String[] routeComponents = route.split("/");
|
String[] routeComponents = route.split("/");
|
||||||
Route component = this.routeMap.get(method).get(routeComponents[1]);
|
|
||||||
|
|
||||||
if (component == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
String pathVariable = null;
|
String pathVariable = null;
|
||||||
|
|
||||||
int i = 1;
|
int i = 0;
|
||||||
|
Route component = this.routeMap.get(method).get(routeComponents[i]);
|
||||||
System.out.println(routeComponents[i]);
|
System.out.println(routeComponents[i]);
|
||||||
for (String search = routeComponents[i]; component != null && component.service == null; component = this.routeMap.get(method).get(search = String.join("/", search, routeComponents[++i]))) {
|
for (String search = routeComponents[i]; component != null && component.service == null; component = this.routeMap.get(method).get(search = String.join("/", search, routeComponents[++i]))) {
|
||||||
System.out.println(component);
|
|
||||||
if (component.hasPathVariable) {
|
if (component.hasPathVariable) {
|
||||||
pathVariable = routeComponents[++i];
|
pathVariable = routeComponents[++i];
|
||||||
search = String.join("/", search, "{var}");
|
search = String.join("/", search, "{var}");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user