<commit_before>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.json.JSONType;
import com.ociweb.json.decode.JSONExtractor;
import com.ociweb.pronghorn.network.HTTPServerConfig;
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 AppendableProxy console;
	private final String host;
	private final int port;
	private final int telemetryPort;
	private final boolean isTLS;
	
	public HTTPServer(String host, int port, Appendable console, int telemetryPort, boolean isTLS) {
		this.host = host;
		this.console = Appendables.proxy(console);
		this.port = port;
		this.telemetryPort = telemetryPort;
		this.isTLS = isTLS;
	}

	public HTTPServer(int port, Appendable console, int telemetryPort, boolean isTLS) {
		this.host = null;
		this.console = Appendables.proxy(console);
		this.port = port;
		this.telemetryPort = telemetryPort;
		this.isTLS = isTLS;
	}
	
    @Override
    public void declareConfiguration(Builder c) {
        
		HTTPServerConfig server = c.useHTTP1xServer(port)
		 .setHost(host)
		 .setTracks(2)
		 .setMaxResponseSize(1<<18);
		
		if (!isTLS) {
			server.useInsecureServer();
		}
		
		if (telemetryPort>0) {
			c.enableTelemetry(telemetryPort);
		}

		c.defineRoute(HTTPHeaderDefaults.COOKIE)
				                  .path("/testpageC").routeId(Struct.LARGE_EXAMPLE);
		c.defineRoute().path("/testpageD").routeId(Struct.SPLIT_EXAMPLE);

		c.defineRoute()
		    .parseJSON()
  		    	.stringField( "person.name", Field.PERSON_NAME)
		    	.integerField("person.age",  Field.PERSON_AGE)
		    .path("/testJSON")
			.routeId(Struct.JSON_EXAMPLE);
		
		c.defineRoute()
		     .path("/resources/${path}")
		     .routeId(Struct.RESOURCES_EXAMPLE);

		c.defineRoute()
	     	.path("/files/${path}")
	     	.routeId(Struct.FILES_EXAMPLE);
		
		c.defineRoute(HTTPHeaderDefaults.COOKIE)
				                 .path("/testpageA?arg=#{myarg}")
		
				                ///TODO: urgent fix.. allso add lambda here for validation..
				        //         .path("/testpageA")////TODO: this should be possible  but not working why?
		
				                 .refineInteger("myarg", Field.MYARG, 111)
				                 
				                 .routeId(Struct.EMPTY_EXAMPLE);
		
		c.defineRoute().path("/testpageB").routeId(Struct.SMALL_EXAMPLE);
		
    }
    
    @Override
    public void declareBehavior(GreenRuntime runtime) {

        runtime.registerListener(new RestBehaviorEmptyResponse(runtime, console))
                 .includeRoutesByAssoc(Struct.EMPTY_EXAMPLE);
        
        runtime.registerListener(new RestBehaviorSmallResponse(runtime, console))
        		.includeRoutesByAssoc(Struct.SMALL_EXAMPLE);
        
        runtime.registerListener(new RestBehaviorLargeResponse(runtime, console))
        		 .includeRoutesByAssoc(Struct.LARGE_EXAMPLE);
                
        
        int threadsCount = 20;        
		long timeoutNS = 1_000_000_000;
		
	//	runtime.registerBlockingListener(()->new BlockingExample(), Field.CONNECTION_ID, threadsCount, timeoutNS);
        
        
        String topic = "httpData";

        runtime.registerListener(new RestBehaviorHandoff(runtime, topic))
        		 .includeRoutesByAssoc(Struct.SPLIT_EXAMPLE);
		<commit_msg>cleanup<commit_after>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.json.JSONType;
import com.ociweb.json.decode.JSONExtractor;
import com.ociweb.pronghorn.network.HTTPServerConfig;
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 AppendableProxy console;
	private final String host;
	private final int port;
	private final int telemetryPort;
	private final boolean isTLS;
	
	public HTTPServer(String host, int port, Appendable console, int telemetryPort, boolean isTLS) {
		this.host = host;
		this.console = Appendables.proxy(console);
		this.port = port;
		this.telemetryPort = telemetryPort;
		this.isTLS = isTLS;
	}

	public HTTPServer(int port, Appendable console, int telemetryPort, boolean isTLS) {
		this.host = null;
		this.console = Appendables.proxy(console);
		this.port = port;
		this.telemetryPort = telemetryPort;
		this.isTLS = isTLS;
	}
	
    @Override
    public void declareConfiguration(Builder c) {
        
		HTTPServerConfig server = c.useHTTP1xServer(port)
		 .setHost(host)
		 .setTracks(2)
		 .setMaxResponseSize(1<<18);
		
		if (!isTLS) {
			server.useInsecureServer();
		}
		
		if (telemetryPort>0) {
			c.enableTelemetry(telemetryPort);
		}

		c.defineRoute(HTTPHeaderDefaults.COOKIE)
				                  .path("/testpageC").routeId(Struct.LARGE_EXAMPLE);
		c.defineRoute().path("/testpageD").routeId(Struct.SPLIT_EXAMPLE);

		c.defineRoute()
		    .parseJSON()
  		    	.stringField( "person.name", Field.PERSON_NAME)
		    	.integerField("person.age",  Field.PERSON_AGE)
		    .path("/testJSON")
			.routeId(Struct.JSON_EXAMPLE);
		
		c.defineRoute()
		     .path("/resources/${path}")
		     .routeId(Struct.RESOURCES_EXAMPLE);

		c.defineRoute()
	     	.path("/files/${path}")
	     	.routeId(Struct.FILES_EXAMPLE);
		
		c.defineRoute(HTTPHeaderDefaults.COOKIE)
				                 .path("/testpageA?arg=#{myarg}")
		
				                ///TODO: urgent fix.. allso add lambda here for validation..
				        //         .path("/testpageA")////TODO: this should be possible  but not working why?
		
				                 .refineInteger("myarg", Field.MYARG, 111)
				                 
				                 .routeId(Struct.EMPTY_EXAMPLE);
		
		c.defineRoute().path("/testpageB").routeId(Struct.SMALL_EXAMPLE);
		
    }
    
    @Override
    public void declareBehavior(GreenRuntime runtime) {

        runtime.registerListener(new RestBehaviorEmptyResponse(runtime, console))
                 .includeRoutesByAssoc(Struct.EMPTY_EXAMPLE);
        
        runtime.registerListener(new RestBehaviorSmallResponse(runtime, console))
        		.includeRoutesByAssoc(Struct.SMALL_EXAMPLE);
        
        runtime.registerListener(new RestBehaviorLargeResponse(runtime, console))
        		 .includeRoutesByAssoc(Struct.LARGE_EXAMPLE);
                
        
		//TODO: add blocking exmple here
		//runtime.registerBlockingListener(()->new BlockingExample(), Field.CONNECTION_ID, threadsCount, timeoutNS);
        
        
        String topic = "httpData";

        runtime.registerListener(new RestBehaviorHandoff(runtime, topic))
        		 .includeRoutesByAssoc(Struct.SPLIT_EXAMPLE);
		
{'fraction_non_alphanumeric': 0.11114339677466221, 'fraction_numerical': 0.004358564579398518, 'mean_word_length': 3.0, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6347933', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2108, 'n_words': 357}