package examples.multiapps;

import com.vtence.molecule.Application;
import com.vtence.molecule.WebServer;
import com.vtence.molecule.middlewares.URLMap.MountPoint;

import java.io.IOException;

public class MultiAppsExample {

    public void run(WebServer server) throws IOException {

        // This middleware simply prints information about the mount point and request
        Application describe = (request, response) -> {
            // The mount point is available as a request attribute
            MountPoint mount = MountPoint.get(request);
            // The modified request path, stripped of the mounted application prefix
            String pathInfo = request.path();
            // The mounted application path prefix
            String mountPoint = mount.app();
            // To reconstruct the full uri, which is useful when creating links
            String uri = mount.uri(pathInfo);

            response.done(String.format("%s at %s (%s)", mountPoint, pathInfo, uri));
        };

        server.mount("/foo", describe) // matches urls starting with /foo but not /foo/bar
              .mount("/foo/bar", describe) // matches urls starting with /foo/bar
              .mount("/baz", describe) // matches urls starting with /baz
              // Start the server without a default app, which means all other requests will get a 404
              .start();
    }

    public static void main(String[] args) throws IOException {
        MultiAppsExample example = new MultiAppsExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -188,16 +188,459 @@
 ption;%0A%0A
+%0A/**%0A * %3Cp%3E%0A *     In this example we demonstrate attaching several applications to different mount points. Mount points are%0A *     matched as words in definition order. More specific paths have precedence over less specific ones.%0A * %3C/p%3E%0A * %3Cp%3E%0A *     This is useful when you want multiple applications to coexist on the server. For instance, you could run both an%0A *     API application under /api and a web application under /.%0A * %3C/p%3E%0A */%0A
 public c
@@ -723,17 +723,16 @@
 ption %7B%0A
-%0A
         
@@ -738,30 +738,47 @@
  // 
-This middleware simply
+We will mount a simple application that
  pri
@@ -819,16 +819,23 @@
 int and 
+client 
 request%0A
@@ -1090,32 +1090,102 @@
 lication prefix%0A
+            // (e.g. this will be /baz for a request to /foo/bar/baz)%0A
             Stri
@@ -1260,24 +1260,76 @@
  path prefix
+ (i.e. either /foo, /foo/bar or /baz in our example)
 %0A           
@@ -1587,16 +1587,102 @@
     %7D;%0A%0A
+        // Mount points are matched in definition order, the most specific match wins%0A
         
@@ -1837,32 +1837,69 @@
 ng with /foo/bar
+ - wins over /foo since more specific
 %0A              .
@@ -2401,8 +2401,9 @@
 %0A    %7D%0A%7D
+%0A

Add javadoc to the multi-apps example