<commit_before>package com.shaneutt.joxy;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;

public class Config {
  // server config
  public String  serverAddress;
  public Integer serverPort;
  public ArrayList<Service> services = new ArrayList<Service>();

  // constructor
  public Config(String fileName) throws FileNotFoundException, IOException {
    Yaml yaml = new Yaml();

    // deal with the configuration file
    InputStream configFile = new FileInputStream(fileName);
    Map config = (Map) yaml.load(configFile);
    configFile.close();

    // get the server and route configurations
    Map serverConfig = (Map) config.get("server");
    ArrayList<Map> routeConfig = (ArrayList<Map>) config.get("routes");

    // store the server configuration and set defaults where needed
    serverAddress = (String) serverConfig.get("address");
    if (serverAddress == null)
      serverAddress = "127.0.0.1";

    serverPort = (Integer) serverConfig.get("port");
    if (serverPort == null)
      serverPort = 8080;

    // build out all the services and their routes
    for (Map serviceMap: routeConfig) {
      // build the new service
      String serviceName = (String) serviceMap.get("name");
      String servicePath = (String) serviceMap.get("path");
      Service newService = new Service(serviceName, servicePath, null);

      // build the routes
      ArrayList<Map> routeList = (ArrayList<Map>) serviceMap.get("routes");
      for (Map routeMap: routeList) {
        String  routeAddress = (String)  routeMap.get("address");
        Integer routePort    = (Integer) routeMap.get("port");
        Route   newRoute     = new Route(routeAddress, routePort);
        newService.addRoute(newRoute);
      }

      // add the service and its route to the services
      services.add(newService);
    }
  }

  public void routerSetup(HttpServer server) {
    for (Service service: services) {
      String           path   = service.path;
      ArrayList<Route> routes = service.routes;
      server.createContext(path, new MyHandler(routes));
    }
  }

  static class MyHandler implements HttpHandler {
    private ArrayList<Route> routes;

    public MyHandler(ArrayList<Route> newRoutes) {
      routes = newRoutes;
    }

    @Override
    public void handle(HttpExchange t) throws IOException {
      // TODO - get rid of simple default and add routing layer here
      String response = "{\"success\":\"true\"}";
      t.sendResponseHeaders(200, response.length());
      OutputStream os = t.getResponseBody();
      os.write(response.getBytes());
      os.close();
    }<commit_msg>Some cleanup and testing additions<commit_after>package com.shaneutt.joxy;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;

public class Config {
  // server config
  public String  serverAddress;
  public Integer serverPort;
  public ArrayList<Service> services = new ArrayList<Service>();

  // constructor
  public Config(String fileName) throws FileNotFoundException, IOException {
    Yaml yaml = new Yaml();

    // deal with the configuration file
    InputStream configFile = new FileInputStream(fileName);
    Map config = (Map) yaml.load(configFile);
    configFile.close();

    // get the server and route configurations
    Map serverConfig = (Map) config.get("server");
    ArrayList<Map> routeConfig = (ArrayList<Map>) config.get("routes");

    // store the server configuration and set defaults where needed
    serverAddress = (String) serverConfig.get("address");
    if (serverAddress == null)
      serverAddress = "127.0.0.1";

    serverPort = (Integer) serverConfig.get("port");
    if (serverPort == null)
      serverPort = 8080;

    // build out all the services and their routes
    for (Map serviceMap: routeConfig) {
      // build the new service
      String serviceName = (String) serviceMap.get("name");
      String servicePath = (String) serviceMap.get("path");
      Service newService = new Service(serviceName, servicePath, null);

      // build the routes
      ArrayList<Map> routeList = (ArrayList<Map>) serviceMap.get("routes");
      for (Map routeMap: routeList) {
        String  routeAddress = (String)  routeMap.get("address");
        Integer routePort    = (Integer) routeMap.get("port");
        Route   newRoute     = new Route(routeAddress, routePort);
        newService.addRoute(newRoute);
      }

      // add the service and its route to the services
      services.add(newService);
    }
  }

  public void routerSetup(HttpServer server) {
    for (Service service: services) {
      String           path   = service.path;
      ArrayList<Route> routes = service.routes;
      server.createContext(path, new MyHandler(routes));
    }
  }

  static class MyHandler implements HttpHandler {
    private ArrayList<Route> routes;

    public MyHandler(ArrayList<Route> newRoutes) {
      routes = newRoutes;
    }

    @Override
    public void handle(HttpExchange t) throws IOException {
      // TODO - get rid of simple default and add routing layer here
      String response = routes.toString();
      t.sendResponseHeaders(200, response.length());
      OutputStream os = t.getResponseBody();
      os.write(response.getBytes());
      os.close();
    }
{'fraction_non_alphanumeric': 0.10681894524689285, 'fraction_numerical': 0.004366812227074236, 'mean_word_length': 3.346715328467153, 'pattern_counts': {'":': 1, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '923146', 'n_tokens_mistral': 1652, 'n_tokens_neox': 1611, 'n_words': 466}