<commit_before>package com.theomn.cartography.web.controllers


import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.model.{HttpResponse, ContentType, MediaType}
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

import slick.driver.H2Driver.api._
import spray.json.DefaultJsonProtocol

import com.theomn.cartography.DB
import com.theomn.cartography.models.{DBTile, tiles}

object JsonProtocol extends DefaultJsonProtocol {
  implicit val tileFormat = jsonFormat4(DBTile.apply)
}

import com.theomn.cartography.Executors.threadpoolContext


object Application extends App {

  import JsonProtocol._
  implicit val system = ActorSystem("cartogarphy")
  implicit val materializer = ActorMaterializer()
  val logger = Logging(system, getClass)

  val route = path("tiles") {
      get {
        val db = DB.getConn
        complete {
          ToResponseMarshallable {
            db.run(tiles.all.result)
          }
        }
      }
    } ~
    path("tiles" / Segment / IntNumber / IntNumber / IntNumber) {
      (worldName, col, row, zoomLevel) =>
        get {
          val q = tiles.
            filter(_.col === col)
            .filter(_.row === row)
            .filter(_.zoomLevel === zoomLevel)


          DB.getConn.run(q.result.headOption).map {

            case Some(data) =>
              val img = ???
              val resp = HttpResponse(OK, entity=img)
              complete(OK, resp.entity.withContentType(ContentType(MediaType.`image/png`)))
            case _ =>
              complete(NotFound, "tile not found")
          }

        }

    }

    val bindingFuture = Http().bindAndHandle(route, "localhost", 9000)

  def stop() = {
    system.shutdown()
    DB.shutdown()
  }

  def start() = {
    main(args=Array[String]())
    logger.info("started akka-http")
  }

}<commit_msg>tile interaction endpoint is working now<commit_after>package com.theomn.cartography.web.controllers

import javax.xml.bind.DatatypeConverter

import akka.actor.ActorSystem
import akka.event.Logging
import akka.http.scaladsl.model.{MediaTypes, HttpResponse, ContentType}
import akka.stream.ActorMaterializer
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes._
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.marshalling.ToResponseMarshallable
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport._
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._

import slick.driver.H2Driver.api._
import spray.json.DefaultJsonProtocol

import com.theomn.cartography.DB
import com.theomn.cartography.models.{DBTile, tiles}

object JsonProtocol extends DefaultJsonProtocol {
  implicit val tileFormat = jsonFormat4(DBTile.apply)
}

import com.theomn.cartography.Executors


object Application extends App {
  import JsonProtocol._
  implicit val ctx = Executors.threadpoolContext
  implicit val system = ActorSystem("cartogarphy-web")
  implicit val materializer = ActorMaterializer()
  val logger = Logging(system, getClass)
  val db = DB.getConn

  val route = pathSingleSlash {
    get{
      complete {
        <html>
          <body>Hello world!</body>
        </html>
      }
    }
  } ~
  (pathPrefix("tiles") & get) {
    pathEnd {
      complete {
        ToResponseMarshallable {
          db.run(tiles.all.result)
        }
      }
    } ~
    (path("grid") & get){
      // TODO: display grid of tile pics
      complete {
        ToResponseMarshallable {
          db.run(tiles.all.result)
        }
      }
    } ~
    (path(Segment / Segment / Segment / IntNumber) & get) {
      (worldName, col, row, zoomLevel) =>
        complete {
          val q = tiles
            .filter(_.col === col.toInt)
            .filter(_.row === row.toInt)
            .filter(_.zoomLevel === zoomLevel)
          for {result <- DB.getConn.run(q.result.headOption)} yield result match {
            case Some(tile: DBTile) =>
              val img = DatatypeConverter.parseBase64Binary(tile.data)
              val resp = HttpResponse(OK, entity = img)
              val respEntity =
                resp.entity.withContentType(ContentType(MediaTypes.`image/png`))
              HttpResponse(OK, entity = respEntity)
            case _ =>
              HttpResponse(NotFound, entity = "tile not found")
          }

        }

    }

  }

    val bindingFuture = Http().bindAndHandle(route, "localhost", 9000)

  def stop() = {
    system.shutdown()
    DB.shutdown()
  }

  def start() = {
    main(args=Array[String]())
    logger.info("started akka-http")
  }

}
{'fraction_non_alphanumeric': 0.10849155017734195, 'fraction_numerical': 0.0029209263509284373, 'mean_word_length': 2.6735632183908047, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7322559', 'n_tokens_mistral': 1499, 'n_tokens_neox': 1465, 'n_words': 281}