package examples.routing;

import com.vtence.molecule.WebServer;
import com.vtence.molecule.routing.DynamicRoutes;

import java.io.IOException;

public class RoutingExample {

    public void run(WebServer server) throws IOException {
        server.start(new DynamicRoutes() {{

            map("/").to((request, response) -> response.done("Welcome!"));

            post("/login").to((request, response) -> {
                String username = request.parameter("username");
                response.redirectTo("/users/" + username)
                        .done();
            });

            get("/hello/:username").to((request, response) -> {
                response.contentType("text/html");
                response.done(
                        "<html>" +
                        "<body>" +
                            "<h3>Hello, " + request.parameter("username") + "</h3>" +
                        "</body>" +
                        "</html>"
                );
            });
        }});
    }

    public static void main(String[] args) throws IOException {
        RoutingExample example = new RoutingExample();
        // Run the default web server
        WebServer webServer = WebServer.create();
        example.run(webServer);
        System.out.println("Access at " + webServer.uri());
    }
}
@@ -142,446 +142,1744 @@
 n;%0A%0A
-public class RoutingExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B%0A        server.start(new DynamicRoutes() %7B%7B%0A%0A            map(%22/%22).to((request, response) -%3E response.done(%22Welcome!%22));%0A%0A            post(%22/login%22).to((request, response) -%3E %7B%0A                String username = request.parameter(%22username%22);%0A                response.redirectTo(%22/users/%22 + username)%0A                        .done();%0A            %7D);%0A
+/**%0A * %3Cp%3E%0A *     This is a simple example showing how to define routes and how to use route patterns containing named%0A *     parameters. Named parameters are accessible as normal request parameters.%0A * %3C/p%3E%0A * %3Cp%3E%0A *     We want to route incoming requests based their paths and verbs, so we provide route patterns as strings.%0A *     If your matching logic is more sophisticated, you can also provide custom matchers.%0A * %3C/p%3E%0A */%0Apublic class RoutingExample %7B%0A%0A    public void run(WebServer server) throws IOException %7B%0A        // We start the server by providing a set of routes, which creates a router to dispatch%0A        // requests to different endpoints.%0A        server.start(new DynamicRoutes() %7B%7B%0A            // Routes are matched in the order they are defined. The first one that matches the request will be%0A            // invoked.%0A%0A            // Route all requests to the root path - ignoring the verb - to an endpoint that responds with %22Welcome!%22%0A            map(%22/%22).to((request, response) -%3E response.done(%22Welcome!%22));%0A%0A            // Route POST requests to /login to an endpoint that simulates login. It simply reads the username%0A            // from the request parameters and redirects to /users/username.%0A            post(%22/login%22).to((request, response) -%3E %7B%0A                String username = request.parameter(%22username%22);%0A                response.redirectTo(%22/users/%22 + username)%0A                        .done();%0A            %7D);%0A%0A            // Route GET requests to /hello/username to an endpoint that renders and HTML page with%0A            // a greeting message for our user. We use a route pattern that includes a named segment (:username).%0A            // The bound parameter is available as a request parameter.
 %0A   
@@ -2608,8 +2608,9 @@
 %0A    %7D%0A%7D
+%0A

Add inline documentation to the routing example