Skip to content

Commit

Permalink
Merge pull request #7 from bb30/SA08_MicroService
Browse files Browse the repository at this point in the history
Enahnce akka implementation
  • Loading branch information
SailReal authored May 2, 2018
2 parents 1da41ef + f4ae44b commit d2435e7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 4 deletions.
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ libraryDependencies += "com.typesafe.play" %% "play-json" % "2.6.6"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3"

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.7.2"

libraryDependencies += "com.typesafe.akka" %% "akka-http" % "10.0.0"
11 changes: 7 additions & 4 deletions src/main/scala/de/htwg/se/sudoku/Sudoku.scala
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package de.htwg.se.sudoku

import com.google.inject.Guice
import de.htwg.se.sudoku.aview.Tui
import com.google.inject.{Guice, Injector}
import de.htwg.se.sudoku.aview.gui.SwingGui
import de.htwg.se.sudoku.aview.{HttpServer, Tui}
import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface

import scala.io.StdIn.readLine

object Sudoku {
val injector = Guice.createInjector(new SudokuModule)
val controller = injector.getInstance(classOf[ControllerInterface])
val injector: Injector = Guice.createInjector(new SudokuModule)
val controller: ControllerInterface = injector.getInstance(classOf[ControllerInterface])
val tui = new Tui(controller)
val gui = new SwingGui(controller)
val webserver = new HttpServer(controller)

controller.createNewGrid

def main(args: Array[String]): Unit = {
Expand All @@ -21,5 +23,6 @@ object Sudoku {
input = readLine()
tui.processInputLine(input)
} while (input != "q")
webserver.unbind()
}
}
73 changes: 73 additions & 0 deletions src/main/scala/de/htwg/se/sudoku/aview/HttpServer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package de.htwg.se.sudoku.aview

import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.{ContentTypes, HttpEntity, StatusCodes}
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.{Route, StandardRoute}
import akka.stream.ActorMaterializer
import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface

import scala.concurrent.{ExecutionContextExecutor, Future}

class HttpServer(controller: ControllerInterface) {

implicit val system: ActorSystem = ActorSystem("system")
implicit val materializer: ActorMaterializer = ActorMaterializer()
// needed for the future flatMap/onComplete in the end
implicit val executionContext: ExecutionContextExecutor = system.dispatcher

val route: Route = get {
pathSingleSlash {
complete(HttpEntity(ContentTypes.`text/plain(UTF-8)`, "<h1>HTWG Sudoku</h1>"))
}
pathPrefix("sudoku" / "rest" / "v1") {
pathEnd {
gridtoHtml
} ~
path("new") {
controller.createEmptyGrid
gridtoHtml
} ~
path("solve") {
controller.solve
gridtoHtml
} ~
path("undo") {
controller.undo
gridtoHtml
} ~
path("redo") {
controller.redo
gridtoHtml
}
}
} ~
post {
pathPrefix("sudoku" / "rest" / "v1") {
path(Segment) { command => {
if (processInputLine(command)) complete(StatusCodes.NoContent) else complete(StatusCodes.BadRequest)
}
}
}
}

def gridtoHtml: StandardRoute = {
complete(HttpEntity(ContentTypes.`text/html(UTF-8)`, "<h1>HTWG Sudoku</h1>" + controller.gridToString))
}

val bindingFuture: Future[Http.ServerBinding] = Http().bindAndHandle(route, "localhost", 8080)

def unbind(): Unit = {
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}

def processInputLine(input: String): Boolean = {
input.toList.filter(c => c != ' ').map(c => c.toString.toInt) match {
case row :: column :: value :: Nil => controller.set(row, column, value); true
case _ => false
}
}
}

0 comments on commit d2435e7

Please sign in to comment.