Skip to content

Commit

Permalink
Route builder demos
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlasov committed Jan 25, 2025
1 parent 7e2e663 commit 3bbb85e
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.nasdanika.launcher.demo.drawio;

import java.util.concurrent.CompletionStage;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.nasdanika.capability.CapabilityFactory.Loader;
import org.nasdanika.common.Invocable;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.drawio.Node;
import org.nasdanika.graph.Element;
import org.nasdanika.graph.processor.ConnectionProcessorConfig;
import org.nasdanika.graph.processor.NodeProcessorConfig;
import org.nasdanika.graph.processor.ProcessorConfig;
import org.nasdanika.graph.processor.ProcessorElement;
import org.nasdanika.graph.processor.ProcessorInfo;
import org.nasdanika.http.HttpServerRouteBuilder;

import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerRoutes;

/**
* Diagram element processor which builds HTTP routes
*/
public class RouteBuilderProcessor implements HttpServerRouteBuilder {

private String amount;

@ProcessorElement
public void setElement(Node element) {
this.amount = element.getProperty("amount");
}

/**
* This is the constructor signature for graph processor classes which are to be instantiated by URIInvocableCapabilityFactory (org.nasdanika.capability.factories.URIInvocableCapabilityFactory).
* Config may be of specific types {@link ProcessorConfig} - {@link NodeProcessorConfig} or {@link ConnectionProcessorConfig}.
* @param loader
* @param loaderProgressMonitor
* @param data
* @param fragment
* @param config
* @param infoProvider
* @param endpointWiringStageConsumer
* @param wiringProgressMonitor
*/
public RouteBuilderProcessor(
Loader loader,
ProgressMonitor loaderProgressMonitor,
Object data,
String fragment,
ProcessorConfig config,
BiConsumer<Element, BiConsumer<ProcessorInfo<Invocable>, ProgressMonitor>> infoProvider,
Consumer<CompletionStage<?>> endpointWiringStageConsumer,
ProgressMonitor wiringProgressMonitor) {

System.out.println("I got constructed " + this);
}

@Override
public void buildRoutes(HttpServerRoutes routes) {
routes.get("/balance", (request, response) -> response.sendString(Mono.just("Account: " + request.param("account") + ", Amount: " + amount)));
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.nasdanika.launcher.demo.http;

import org.nasdanika.http.HttpServerRouteBuilder;
import org.nasdanika.http.ReflectiveHttpServerRouteBuilder.Route;
import org.nasdanika.http.ReflectiveHttpServerRouteBuilder.RouteBuilder;
import org.reactivestreams.Publisher;

import reactor.core.publisher.Mono;
import reactor.netty.http.server.HttpServerRequest;
import reactor.netty.http.server.HttpServerResponse;
import reactor.netty.http.server.HttpServerRoutes;

@Route("/test/")
public class DemoReflectiveHttpRoutes {
Expand Down Expand Up @@ -44,4 +47,21 @@ public Publisher<Void> doSomething(
return response.sendString(Mono.just("do someting"));
}

@RouteBuilder("field-route-builder")
public HttpServerRouteBuilder routeBuilder = routes -> {
routes.get("/hello", (request, response) -> response.sendString(Mono.just("Hello from field route builder!")));
};

@RouteBuilder("getter-route-builder")
public HttpServerRouteBuilder getRouteBuilder() {
return routes -> {
routes.get("/hello", (request, response) -> response.sendString(Mono.just("Hello from getter route builder!")));
};
};

@RouteBuilder("route-builder-method")
public void buildRoutes(HttpServerRoutes routes) {
routes.get("/hello", (request, response) -> response.sendString(Mono.just("Hello from route builder method!")));
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.nasdanika.launcher.demo.http;

import org.nasdanika.common.Reflector;
import org.nasdanika.http.ReflectiveHttpServerRouteBuilder.Route;

/**
* Demo of a hierarchical routing with factory
*/
@Route("/factory")
public class ReflectorFactory {

@Route("/demo")
@Reflector.Factory
public DemoReflectiveHttpRoutes getRoutes() {
return new DemoReflectiveHttpRoutes();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.nasdanika.launcher.demo.http;

import org.nasdanika.common.Reflector;
import org.nasdanika.http.ReflectiveHttpServerRouteBuilder.Route;

/**
* Demo of a hierarchical routing with factory
*/
@Route("/super-factory")
public class ReflectorSuperFactory {

@Route("/super-demo")
@Reflector.Factory
public ReflectorFactory getRoutes() {
return new ReflectorFactory();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package org.nasdanika.launcher.demo.rules.inspectors;

import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import org.nasdanika.capability.CapabilityProvider;
import org.nasdanika.capability.CapabilityFactory.Loader;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.models.rules.Inspector;
import org.nasdanika.models.rules.InspectorCapabilityFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.nasdanika.http.HttpServerRouteBuilder;
import org.nasdanika.http.ReflectiveHttpServerRouteBuilder;
import org.nasdanika.launcher.demo.http.DemoReflectiveHttpRoutes;
import org.nasdanika.launcher.demo.http.ReflectorSuperFactory;

import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;
Expand Down Expand Up @@ -186,7 +187,54 @@ public void testDrawioRoutesSimple() throws Exception {
}

@Test
// @Disabled
@Disabled
public void testDrawioRouteBuilder() throws Exception {
Document document = Document.load(
URI.createFileURI(new File("test-data/drawio-http/route-builder.drawio").getCanonicalPath()),
null,
null);

ElementProcessorFactory<Object> elementProcessorFactory = new ElementProcessorFactory<Object>(
document,
new CapabilityLoader(),
"processor");

ProgressMonitor progressMonitor = new PrintStreamProgressMonitor();

Map<Element, ProcessorInfo<Object>> processors = elementProcessorFactory.createProcessors(
null,
null,
progressMonitor);

DisposableServer server = HttpServer
.create()
.port(8080)
.route(routes -> HttpServerRouteBuilder.buildRoutes(processors.values(), "route", routes))
.bindNow();

try (Terminal terminal = TerminalBuilder.builder().system(true).build()) {
LineReader lineReader = LineReaderBuilder
.builder()
.terminal(terminal)
.build();

String prompt = "http-server>";
while (true) {
String line = null;
line = lineReader.readLine(prompt);
System.out.println("Got: " + line);
if ("exit".equals(line)) {
break;
}
}
}

server.dispose();
server.onDispose().block();
}

@Test
@Disabled
public void testReflectiveRoutesBuilder() throws Exception {
ReflectiveHttpServerRouteBuilder builder = new ReflectiveHttpServerRouteBuilder();
builder.addTargets("/reflective", new DemoReflectiveHttpRoutes());
Expand Down Expand Up @@ -217,5 +265,38 @@ public void testReflectiveRoutesBuilder() throws Exception {
server.dispose();
server.onDispose().block();
}

@Test
// @Disabled
public void testReflectorFactory() throws Exception {
ReflectiveHttpServerRouteBuilder builder = new ReflectiveHttpServerRouteBuilder();
builder.addTargets("/reflective", new ReflectorSuperFactory());

DisposableServer server = HttpServer
.create()
.port(8080)
.route(builder::buildRoutes)
.bindNow();

try (Terminal terminal = TerminalBuilder.builder().system(true).build()) {
LineReader lineReader = LineReaderBuilder
.builder()
.terminal(terminal)
.build();

String prompt = "http-server>";
while (true) {
String line = null;
line = lineReader.readLine(prompt);
System.out.println("Got: " + line);
if ("exit".equals(line)) {
break;
}
}
}

server.dispose();
server.onDispose().block();
}

}
1 change: 1 addition & 0 deletions test-data/drawio-http/route-builder.drawio
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<mxfile host="Electron" agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/26.0.4 Chrome/128.0.6613.186 Electron/32.2.5 Safari/537.36" compressed="true" version="26.0.4"><diagram id="ibs-context" name="System Context Diagram">dZTLlqIwEIafhqUcILTo0ntv+pweXUxvY1KS9ATKE4LoPP0EKBXGmRXJV0Vd/ioI2Kq47iw/qw+UYIIkkteArYMkibM48Y+W3IjEM9aT3GpJ7AkO+jcQjIjWWkI1cnSIxunzGAosSxBuxLi12IzdTmjGWc88hxdwENy80p9aOnVvYzp/Gt5B54pSz5KsNxT87kydVIpLbAaIbQK2soiuP+Hxu20giQw/ehU7PxIPxcTCqUf7zWL9sQkLSfktOnS3M5W7EE5jGbBFkEyDhHkV2TJo00UGBX/YPEiDbHnkFUxqq4NsrUsJ11C5wvRx78N5VlpcV2C8ddtf/YEqHlkj/be+Fko3FGL7/3b3WDvwaFlrI8E+OhRQVWh7H8kd9z188wtvS7B5WPJK8lL/4qHhdSkU2FBCgaG0vNEYdkEp4ucjVh/acAEKW8t4TWxXCI1g290mx3tRW68cFwJr31e2plUrumv3Rvo2DbPZWMb5l7qwH18xm+enPTa72RTVJH7RNqrc7b55PqufSft+7IfYKO3gcPb1etD4j82zblq9+VVs6uQC1sF1gCjfDrAAZ2/ehawpo0WlTzVN3/p789z7GbmowcanxDhJmD8iJ8NloRb/tTudxOgGroO/Cdv8AQ==</diagram></mxfile>

0 comments on commit 3bbb85e

Please sign in to comment.