Skip to content

Commit 19e149d

Browse files
committed
Started to move from akka http to play as web server. Currently just moved a play project
1 parent 8694d76 commit 19e149d

File tree

9 files changed

+156
-0
lines changed

9 files changed

+156
-0
lines changed

play/build.sbt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name := "SequencePlanner_play"
2+
version := "2.0_M1"
3+
4+
libraryDependencies ++= Seq(
5+
"com.vmunier" %% "scalajs-scripts" % "1.0.0"
6+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import "lib/bootstrap/less/bootstrap.less";
2+
@import "lib/font-awesome/less/font-awesome.less";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Config file in HOCON format. See following for more information:
2+
# https://www.playframework.com/documentation/latest/Configuration
3+
4+
application.cdn = ""
5+
application.cdn=${?APPLICATION_CDN}
6+
7+
spatutorial {
8+
}

play/src/main/resources/routes

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Routes
2+
# This file defines all application routes (Higher priority routes first)
3+
# ~~~~
4+
5+
# Home page
6+
GET / controllers.Application.index
7+
8+
# Map static resources from the /public folder to the /assets URL path
9+
GET /assets/fonts/*file controllers.Assets.at(path="/public/lib/font-awesome/fonts", file)
10+
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
11+
12+
# Autowire calls
13+
POST /api/*path controllers.Application.autowireApi(path: String)
14+
15+
# Logging
16+
POST /logging controllers.Application.logging
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package controllers
2+
3+
import java.nio.ByteBuffer
4+
5+
import boopickle.Default._
6+
import com.google.inject.Inject
7+
import play.api.{Configuration, Environment}
8+
import play.api.mvc._
9+
import services.ApiService
10+
import spatutorial.shared.Api
11+
12+
import scala.concurrent.ExecutionContext.Implicits.global
13+
14+
object Router extends autowire.Server[ByteBuffer, Pickler, Pickler] {
15+
override def read[R: Pickler](p: ByteBuffer) = Unpickle[R].fromBytes(p)
16+
override def write[R: Pickler](r: R) = Pickle.intoBytes(r)
17+
}
18+
19+
class Application @Inject() (implicit val config: Configuration, env: Environment) extends Controller {
20+
val apiService = new ApiService()
21+
22+
def index = Action {
23+
Ok(views.html.index("SPA tutorial"))
24+
}
25+
26+
def autowireApi(path: String) = Action.async(parse.raw) {
27+
implicit request =>
28+
println(s"Request path: $path")
29+
30+
// get the request body as ByteString
31+
val b = request.body.asBytes(parse.UNLIMITED).get
32+
33+
// call Autowire route
34+
Router.route[Api](apiService)(
35+
autowire.Core.Request(path.split("/"), Unpickle[Map[String, ByteBuffer]].fromBytes(b.asByteBuffer))
36+
).map(buffer => {
37+
val data = Array.ofDim[Byte](buffer.remaining())
38+
buffer.get(data)
39+
Ok(data)
40+
})
41+
}
42+
43+
def logging = Action(parse.anyContent) {
44+
implicit request =>
45+
request.body.asJson.foreach { msg =>
46+
println(s"CLIENT - $msg")
47+
}
48+
Ok("")
49+
}
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package services
2+
3+
import java.util.{UUID, Date}
4+
5+
import spatutorial.shared._
6+
7+
class ApiService extends Api {
8+
var todos = Seq(
9+
TodoItem("41424344-4546-4748-494a-4b4c4d4e4f50", 0x61626364, "Wear shirt that says “Life”. Hand out lemons on street corner.", TodoLow, completed = false),
10+
TodoItem("2", 0x61626364, "Make vanilla pudding. Put in mayo jar. Eat in public.", TodoNormal, completed = false),
11+
TodoItem("3", 0x61626364, "Walk away slowly from an explosion without looking back.", TodoHigh, completed = false),
12+
TodoItem("4", 0x61626364, "Sneeze in front of the pope. Get blessed.", TodoNormal, completed = true)
13+
)
14+
15+
override def welcomeMsg(name: String): String =
16+
s"Welcome to SPA, $name! Time is now ${new Date}"
17+
18+
override def getAllTodos(): Seq[TodoItem] = {
19+
// provide some fake Todos
20+
Thread.sleep(300)
21+
println(s"Sending ${todos.size} Todo items")
22+
todos
23+
}
24+
25+
// update a Todo
26+
override def updateTodo(item: TodoItem): Seq[TodoItem] = {
27+
// TODO, update database etc :)
28+
if(todos.exists(_.id == item.id)) {
29+
todos = todos.collect {
30+
case i if i.id == item.id => item
31+
case i => i
32+
}
33+
println(s"Todo item was updated: $item")
34+
} else {
35+
// add a new item
36+
val newItem = item.copy(id = UUID.randomUUID().toString)
37+
todos :+= newItem
38+
println(s"Todo item was added: $newItem")
39+
}
40+
Thread.sleep(300)
41+
todos
42+
}
43+
44+
// delete a Todo
45+
override def deleteTodo(itemId: String): Seq[TodoItem] = {
46+
println(s"Deleting item with id = $itemId")
47+
Thread.sleep(300)
48+
todos = todos.filterNot(_.id == itemId)
49+
todos
50+
}
51+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@(title: String)(implicit config: play.api.Configuration, env: play.api.Environment)
2+
@import views.html.tags._
3+
4+
<!DOCTYPE html>
5+
6+
<html lang="en">
7+
<head>
8+
<meta charset="UTF-8">
9+
<title>@title</title>
10+
<meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
11+
<link rel="stylesheet" media="screen" href=@_asset("stylesheets/main.min.css") >
12+
<link rel="shortcut icon" type="image/png" href=@_asset("images/favicon.png") >
13+
</head>
14+
<body>
15+
<div id="root">
16+
</div>
17+
@scalajs.html.scripts(projectName = "client", name => _asset(name).toString, name => getClass.getResource(s"/public/$name") != null)
18+
</body>
19+
</html>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@(path: String)(implicit config: play.api.Configuration)@{config.getString("application.cdn").getOrElse("") + routes.Assets.versioned(path)}

project/plugins.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.8.2")
66

77
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "0.6.14")
88

9+
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.9")
10+
11+
addSbtPlugin("com.vmunier" % "sbt-web-scalajs" % "1.0.3")

0 commit comments

Comments
 (0)