<commit_before>package mts.experiments

import mts.tasks._

import java.io.InputStream
import java.security.{ SecureRandom, KeyStore }
import javax.net.ssl.{ SSLContext, TrustManagerFactory, KeyManagerFactory }

import akka.actor.ActorSystem
import akka.http.scaladsl.server.{ RouteResult, Route, Directives }
import akka.http.scaladsl.{ ConnectionContext, HttpsConnectionContext, Http }
import akka.stream.Materializer
import akka.stream.ActorMaterializer
import com.typesafe.sslconfig.akka.AkkaSSLConfig

import scala.util.{Try, Success, Failure}

class Server(tasks: List[TaskSpecification])(implicit config: TaskConfig) {
  import config._
  implicit val system: ActorSystem = actorSystem
  implicit val materializer: Materializer = ActorMaterializer()
  import system.dispatcher

  val service = new Webservice(tasks)

  val httpBinding = Http().bindAndHandle(service.route, interface, httpPort)
  httpBinding.onComplete {
    case Success(binding) ⇒
      val localAddress = binding.localAddress
      println(s"Server is listening on http://${localAddress.getHostName}:${localAddress.getPort}")
    case Failure(e) ⇒
      println(s"Binding failed with ${e.getMessage}")
      system.terminate()
  }

  val httpsServer = Try {
    // Manual HTTPS configuration

    val password: Array[Char] = new java.util.Scanner(
      getClass.getClassLoader.getResourceAsStream(s"$serverDomain-keystore-password")
    ).next.toCharArray

    val ks: KeyStore = KeyStore.getInstance("PKCS12")
    val keystore: InputStream = getClass.getClassLoader.getResourceAsStream(s"$serverDomain.p12")

    require(keystore != null, "Keystore required!")
    ks.load(keystore, password)

    val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance("SunX509")
    keyManagerFactory.init(ks, password)

    val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
    tmf.init(ks)

    val sslContext = SSLContext.getInstance("TLSv1.2")
    sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)

    val https = ConnectionContext.https(sslContext)

    val httpsBinding = Http().bindAndHandle(service.route, interface, httpsPort, connectionContext = https)
    httpsBinding.onComplete {
      case Success(binding) ⇒
        val localAddress = binding.localAddress
        println(s"Server is listening on https://${localAddress.getHostName}:${localAddress.getPort}")
      case Failure(e) ⇒
        println(s"Binding failed with ${e.getMessage}")
        system.terminate()
    }
  }

  httpsServer match {
    case Success(_) => ()
    case Failure(e) => System.err.println(s"HTTPS binding failed: $e")
  }
}
<commit_msg>Clean up server code to allow HTTP failure<commit_after>package mts.experiments

import mts.tasks._

import java.io.InputStream
import java.security.{ SecureRandom, KeyStore }
import javax.net.ssl.{ SSLContext, TrustManagerFactory, KeyManagerFactory }

import akka.actor.ActorSystem
import akka.http.scaladsl.server.{ RouteResult, Route, Directives }
import akka.http.scaladsl.{ ConnectionContext, HttpsConnectionContext, Http }
import akka.stream.Materializer
import akka.stream.ActorMaterializer
import com.typesafe.sslconfig.akka.AkkaSSLConfig

import scala.util.{Try, Success, Failure}

class Server(tasks: List[TaskSpecification])(implicit config: TaskConfig) {
  import config._
  implicit val system: ActorSystem = actorSystem
  implicit val materializer: Materializer = ActorMaterializer()
  import system.dispatcher

  val service = new Webservice(tasks)

  val httpBinding = Http().bindAndHandle(service.route, interface, httpPort)
  httpBinding.onComplete {
    case Success(binding) ⇒
      val localAddress = binding.localAddress
      println(s"Server is listening on http://${localAddress.getHostName}:${localAddress.getPort}")
    case Failure(e) ⇒
      println(s"HTTP binding failed:")
      e.printStackTrace
  }

  val httpsServer = Try {
    // Manual HTTPS configuration

    val password: Array[Char] = new java.util.Scanner(
      getClass.getClassLoader.getResourceAsStream(s"$serverDomain-keystore-password")
    ).next.toCharArray

    val ks: KeyStore = KeyStore.getInstance("PKCS12")
    val keystore: InputStream = getClass.getClassLoader.getResourceAsStream(s"$serverDomain.p12")

    require(keystore != null, "Keystore required!")
    ks.load(keystore, password)

    val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.getInstance("SunX509")
    keyManagerFactory.init(ks, password)

    val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")
    tmf.init(ks)

    val sslContext = SSLContext.getInstance("TLSv1.2")
    sslContext.init(keyManagerFactory.getKeyManagers, tmf.getTrustManagers, new SecureRandom)

    val https = ConnectionContext.https(sslContext)

    val httpsBinding = Http().bindAndHandle(service.route, interface, httpsPort, connectionContext = https)
    httpsBinding.onComplete {
      case Success(binding) ⇒
        val localAddress = binding.localAddress
        println(s"Server is listening on https://${localAddress.getHostName}:${localAddress.getPort}")
      case Failure(e) ⇒
        println(s"HTTPS binding failed:")
        e.printStackTrace
    }
  }

  httpsServer match {
    case Success(_) => ()
    case Failure(e) =>
      System.err.println(s"HTTPS configuration failed: $e")
      e.printStackTrace
  }
}

{'fraction_non_alphanumeric': 0.10234026745913818, 'fraction_numerical': 0.004457652303120356, 'mean_word_length': 4.4837067209775965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, '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': '1080686', 'n_tokens_mistral': 1618, 'n_tokens_neox': 1559, 'n_words': 346}