http-server

Version: org.nasdanika.http
Usage: nsd drawio http-server [-hV] [--[no-]console] [--progress-console] [--progress-data] [--progress-json] [--connection-base=<connectionBase>] [--http-host=<httpHost>] [--http-port=<httpPort>] [--http-server-shutdown-timeout=<timeout>] [--progress-output=<progressOutput>] <processorProperty> <routeProperty> Routes HTTP requests to a diagram element processor <processorProperty> Processor property <routeProperty> Route property --connection-base=<connectionBase> Connection base Valid values: PARENT, SOURCE, TARGET --[no-]console If true starts a console and waits for exit command, otherwise creates a shutdown hook disposing the server -h, --help Show this help message and exit. --http-host=<httpHost> HTTP host (network interface) to bind to --http-port=<httpPort> HTTP port. If a port is not specified, an ephemeral port is used --http-server-shutdown-timeout=<timeout> Timeout in seconds, defaults to 3 seconds --progress-console Output progress to console --progress-data Output progress data --progress-json Output progress in JSON --progress-output=<progressOutput> Output file for progress monitor -V, --version Print version information and exit.

Example

drawio diagram.drawio http-server --http-port=8080 processor route

  • Parent drawio command loads diagram.drawio file
  • This command:
    • Loads processor invocable URIs from the processor property
    • Loads route definitions from the route property
    • Serves diagram element routes on port 8080

Resources

Parameters

Route serving processors shall implement BiFunction<? super HttpServerRequest,? super HttpServerResponse,? extends Publisher<Void>> to serve a single route.

Processors may also implement HttpServerRouteBuilder or be adaptable to it. In this case the route property is optional and is used as a route prefix.

Examples

Java
Single route

SystemHttpHandler.java

import java.util.concurrent.CompletionStage;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
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.reactivestreams.Publisher;

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

/**
 * Diagram element processor which processes HTTP requests 
 */
public class SystemHttpHandler implements BiFunction<HttpServerRequest, HttpServerResponse, Publisher<Void>> {
    
    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 SystemHttpHandler(
            Loader loader,
            ProgressMonitor loaderProgressMonitor,
            Object data,
            String fragment,
            ProcessorConfig config,
            BiConsumer<Element, BiConsumer<ProcessorInfo<Invocable>, ProgressMonitor>> infoProvider,
            Consumer<CompletionStage<?>> endpointWiringStageConsumer,
            ProgressMonitor wiringProgressMonitor) {

    }

    @Override
    public Publisher<Void> apply(HttpServerRequest request, HttpServerResponse response) {
        return response.sendString(Mono.just("Account: " + request.param("account") + ", Amount: " + amount));
    }

}
Route builder

RouteBuilderProcessor

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) {
        
    }

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

}
Groovy

person.groovy

import reactor.core.publisher.Mono 
import org.nasdanika.drawio.Node
import org.nasdanika.graph.processor.ProcessorElement

//drawio test-data/drawio-http/diagram.drawio http-server --http-port=8080 processor route
new java.util.function.BiFunction() {

    @ProcessorElement
    public Node element;
    
    def apply(request, response) {
        response.sendString(Mono.just(element.getLabel()))
    }
    
}

Route property value is a YAML String or Map.

String

If the value is a String, then the value is the route path and the route method is GET.

Example: /person

Map

If the value is a String, then the map shall contain two keys:

  • method - one of HTTPMethod literals.
  • path - route path

Example:

method: GET
path: /person