-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from bb30/SA08_MicroService
Enahnce akka implementation
- Loading branch information
Showing
3 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |