2

I currently have a Scala echo server, how to do I implement Akka into the code so it would serve HTML files that I've implemented?

Don't even know how to begin to work this out, would I have to import something like an Akka thing? Or to change my dependencies? So instead of using imports Java and Scala would I change it to Akka instead?

My echo server

import java.net._
import java.io._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.control.Breaks

object EchoServer {
  def read_and_write(in: BufferedReader, out: BufferedWriter): Unit = {
    out.write(in.readLine())
    out.flush()
    in.close()
    out.close()
  }

  def serve(server: ServerSocket): Unit = {
    val s = server.accept()
    val in = new BufferedReader(new InputStreamReader(s.getInputStream))
    val out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream))

    read_and_write(in, out)

    s.close()
  }

  def getListOfFiles(dir: String): List[File] = {
    val file = new File(dir)
    if (file.exists && file.isDirectory) {
      file.listFiles.filter(_.isFile).toList
    } else {
      List[File]()
    }
  }

  def main(args: Array[String]) {
    val server = new ServerSocket(9999)
    var readString = "C:\\Users\\Desktop\\EchoServer\\src\\main\\htmlFiles"
    println("What file are you looking for with extension?")
    implicit val ec = ExecutionContext.global
      while (true) {
        Future {
        implicit val ec = ExecutionContext.global
        val userInput = scala.io.StdIn.readLine()
        val result = getListOfFiles(readString)
        val loop = new Breaks
        try {
          result.foreach { file =>
            if (file.getName.endsWith(".html") == (userInput.endsWith(".html"))) {
              if (file.getName.equals(userInput)) {
                println(file.getName + " file found")
              }
              else {
                println("file not found")
              }
            }
          }
        }
        catch {
          case e: FileNotFoundException => println("Couldn't find that file.")
        }
        serve(server)
      }
    }
  }
}

I had implemented futures, not sure if it serves HTML files correctly even. How would I change this to akka concurrency?

KKNg
  • 87
  • 1
  • 5
  • 2
    This question is too general for stackoverflow, the best place to start is with [the documentation](https://doc.akka.io/docs/akka-http/current/introduction.html) for Akka Http, which has a simple working server. – Tim May 12 '19 at 08:21
  • https://stackoverflow.com/a/56088437/14955 – Thilo May 12 '19 at 08:26
  • 1
    Possible duplicate of [How to serve static HTML file in Scala?](https://stackoverflow.com/questions/56074253/how-to-serve-static-html-file-in-scala) – Thilo May 12 '19 at 08:26

0 Answers0