package higgla.server;

import juglr.*;
import juglr.net.HTTP;
import juglr.net.HTTPServer;

import java.io.IOException;

import static java.lang.Integer.parseInt;

/**
 * Main class for the Higgla application
 *
 * @author Mikkel Kamstrup Erlandsen <mailto:mke@statsbiblioteket.dk>
 * @since Jan 29, 2010
 */
public class Main {

    public static void main (String[] args) {
        int port = 4567;
        if (args.length >= 1) {
            try {
                port = parseInt(args[0]);
            } catch (NumberFormatException e) {
                System.err.println(String.format(
                        "Port not a number '%s': %s. Bailing out.",
                        args[0], e.getMessage()));
                System.exit(1);
            }
        }

        // Set up all central actors
        Actor store = new StoreActor();
        Actor query = new QueryGatewayActor();
        Actor get = new GetActor();

        // Start actors
        MessageBus.getDefault().start(store.getAddress());
        MessageBus.getDefault().start(query.getAddress());
        MessageBus.getDefault().start(get.getAddress());

        // Set up the HTTP server
        HTTPServer server = null;
        try {
             server = new HTTPServer(port);
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("Failed to create HTTP server. Bailing out.");
            System.exit(2);
        }

        server.registerHandler(
                    "^/[^/]+/query/?$", query.getAddress(), HTTP.Method.GET);
                    //".*", query.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/search\\?.+$", search.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/changes\\?.+$", changes.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/count/?$", count.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/changes\\?.+$", changes.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/[^/]+$", get.getAddress(), HTTP.Method.GET);
        //server.registerHandler(
        //            "^/[^/]+/[^/]+$", put.getAddress(), HTTP.Method.PUT);

        server.start();

        // Indefinite non-busy block
        synchronized (store) {
            try {                
                store.wait();
            } catch (InterruptedException e) {
                System.err.println("Interrupted");
                System.exit(1);
            }
        }
    }

}

@@ -816,25 +816,34 @@
 tor 
-store
+writer
  = new 
-Store
+WriterGateway
 Acto
@@ -993,21 +993,22 @@
 ).start(
-store
+writer
 .getAddr
@@ -1507,24 +1507,123 @@
 /%5D+/
-query
+?$%22, query.getAddress(), HTTP.Method.GET);%0A        server.registerHandler(%0A                    %22%5E/%5B%5E/%5D+
 /?$%22, 
-qu
+writ
 er
-y
 .get
@@ -1637,34 +1637,35 @@
 (), HTTP.Method.
-GE
+POS
 T);%0A            
@@ -2110,129 +2110,8 @@
 er(%0A
-        //            %22%5E/%5B%5E/%5D+/changes%5C%5C?.+$%22, changes.getAddress(), HTTP.Method.GET);%0A        //server.registerHandler(%0A
     
@@ -2377,21 +2377,22 @@
 onized (
-store
+writer
 ) %7B%0A    
@@ -2441,13 +2441,14 @@
     
-store
+writer
 .wai

Make GET and POST to http://localhost:$PORT/$BASENAME do queries and updates to $BASE respectively