package com.ociweb.oe.greenlightning.api;


import com.ociweb.gl.api.Builder;
import com.ociweb.gl.api.GreenApp;
import com.ociweb.gl.api.GreenRuntime;
import com.ociweb.pronghorn.network.config.HTTPHeaderDefaults;
import com.ociweb.pronghorn.util.AppendableProxy;
import com.ociweb.pronghorn.util.Appendables;

public class HTTPServer implements GreenApp
{
	private byte[] cookieHeader = HTTPHeaderDefaults.COOKIE.rootBytes();
	
	private int emptyResponseRouteId;
	private int smallResponseRouteId;
	private int largeResponseRouteId;
	private int splitResponseRouteId;
	private int shutdownRouteId;
		
	private AppendableProxy console;
	private final String host;
	private final int port;
	
	public HTTPServer(String host, int port, Appendable console) {
		this.host = host;
		this.console = Appendables.proxy(console);
		this.port = port;
	}
	
	public HTTPServer(int port, Appendable console) {
		this.host = null;
		this.console = Appendables.proxy(console);
		this.port = port;
	}
	
    @Override
    public void declareConfiguration(Builder c) {
        
		c.useHTTP1xServer(port).setHost(host);
		
		emptyResponseRouteId = c.defineRoute("/testpageA?arg=#{myarg}", cookieHeader);
		smallResponseRouteId = c.defineRoute("/testpageB");
		largeResponseRouteId = c.defineRoute("/testpageC", cookieHeader);
		splitResponseRouteId = c.defineRoute("/testpageD");
		
		//only do in test mode... 
		//in production it is a bad idea to let clients turn off server.
		shutdownRouteId = c.defineRoute("/shutdown?key=${key}");
				
		c.enableTelemetry();
		
    }


    @Override
    public void declareBehavior(GreenRuntime runtime) {
    	
        runtime.addRestListener(new RestBehaviorEmptyResponse(runtime, "myarg", console))
                 .includeRoutes(emptyResponseRouteId);
        
        runtime.addRestListener(new RestBehaviorSmallResponse(runtime, console))
        		.includeRoutes(smallResponseRouteId);
        
        runtime.addRestListener(new RestBehaviorLargeResponse(runtime, console))
        		 .includeRoutes(largeResponseRouteId);
        
        runtime.addRestListener(new RestBehaviorHandoff(runtime, "responder"))
        		 .includeRoutes(splitResponseRouteId);
        
        runtime.addPubSubListener(new RestBehaviorHandoffResponder(runtime, console))
		         .addSubscription("responder");
        


        
        //splitResponseRouteId
        
        runtime.addRestListener(new ShutdownRestListener(runtime))
                  .includeRoutes(shutdownRouteId);
        
        //NOTE .includeAllRoutes() can be used to write a behavior taking all routes

    }
   
}

@@ -355,80 +355,8 @@
 p%0A%7B%0A
-%09private byte%5B%5D cookieHeader = HTTPHeaderDefaults.COOKIE.rootBytes();%0A%09%0A
 %09pri
@@ -1056,32 +1056,86 @@
 = c.defineRoute(
+HTTPHeaderDefaults.COOKIE)%0A%09%09%09%09                 .path(
 %22/testpageA?arg=
@@ -1143,30 +1143,26 @@
 %7Bmyarg%7D%22
-, cookieHeader
+).routeId(
 );%0A%09%09sma
@@ -1187,32 +1187,39 @@
 = c.defineRoute(
+).path(
 %22/testpageB%22);%0A%09
@@ -1215,16 +1215,26 @@
 tpageB%22)
+.routeId()
 ;%0A%09%09larg
@@ -1270,34 +1270,85 @@
 ute(
-%22/testpageC%22, cookieHeader
+HTTPHeaderDefaults.COOKIE)%0A%09%09%09%09                  .path(%22/testpageC%22).routeId(
 );%0A%09
@@ -1385,16 +1385,23 @@
 neRoute(
+).path(
 %22/testpa
@@ -1405,16 +1405,26 @@
 tpageD%22)
+.routeId()
 ;%0A%09%09%0A%09%09/
@@ -1550,16 +1550,23 @@
 neRoute(
+).path(
 %22/shutdo
@@ -1580,16 +1580,26 @@
 $%7Bkey%7D%22)
+.routeId()
 ;%0A%09%09%09%09%0A%09

update to new API