From c501d1409445c5d5fffb0851af4d2722eb006cba Mon Sep 17 00:00:00 2001 From: efim Date: Sun, 23 Apr 2023 11:55:53 +0400 Subject: [PATCH] add sub routes to backend app in the shape of websocket and post \ get with receiving of some request model --- .../sunshine/planningpoker/App.scala | 32 ----------- .../sunshine/planningpoker/BackendApp.scala | 54 +++++++++++++++++++ build.sbt | 14 +++-- .../sunshine/planningpoker/Requests.scala | 5 ++ 4 files changed, 70 insertions(+), 35 deletions(-) delete mode 100644 backend/src/main/scala/industries/sunshine/planningpoker/App.scala create mode 100644 backend/src/main/scala/industries/sunshine/planningpoker/BackendApp.scala create mode 100644 common/src/main/scala/industries/sunshine/planningpoker/Requests.scala diff --git a/backend/src/main/scala/industries/sunshine/planningpoker/App.scala b/backend/src/main/scala/industries/sunshine/planningpoker/App.scala deleted file mode 100644 index c7772f4..0000000 --- a/backend/src/main/scala/industries/sunshine/planningpoker/App.scala +++ /dev/null @@ -1,32 +0,0 @@ -package industries.sunshine.planningpoker - -import cats.effect._ -import cats.syntax.all._ -import org.http4s._, org.http4s.dsl.io._, org.http4s.implicits._ -import com.comcast.ip4s._ -import org.http4s.HttpRoutes -import org.http4s.dsl.io._ -import org.http4s.implicits._ -import org.http4s.ember.server._ - -object App extends IOApp.Simple { - - val service = HttpRoutes - .of[IO] { case _ => - Ok("hello") - } - .orNotFound - - override def run: IO[Unit] = { - val a = 1 - IO.println(s"Hello, World! $a") >> - EmberServerBuilder - .default[IO] - .withHost(ipv4"0.0.0.0") - .withPort(port"8080") - .withHttpApp(service) - .build - .use(_ => IO.never) - .as(ExitCode.Success) - } -} diff --git a/backend/src/main/scala/industries/sunshine/planningpoker/BackendApp.scala b/backend/src/main/scala/industries/sunshine/planningpoker/BackendApp.scala new file mode 100644 index 0000000..e6e6d73 --- /dev/null +++ b/backend/src/main/scala/industries/sunshine/planningpoker/BackendApp.scala @@ -0,0 +1,54 @@ +package industries.sunshine.planningpoker + +import cats.effect._ +import cats.syntax.all._ +import org.http4s._, org.http4s.dsl.io._, org.http4s.implicits._ +import com.comcast.ip4s._ +import org.http4s.HttpRoutes +import org.http4s.dsl.io._ +import org.http4s.implicits._ +import org.http4s.ember.server._ +import org.http4s.websocket.WebSocketFrame + +import io.circe.generic.auto._ +import org.http4s.circe.CirceEntityDecoder._ +import scala.concurrent.duration._ +import org.http4s.server.websocket.WebSocketBuilder +import fs2._ + +object BackendApp extends IOApp.Simple { + + def service(wsb: WebSocketBuilder[IO]) = HttpRoutes + .of[IO] { + case req @ POST -> Root / login => { + for { + data <- req.as[Requests.LogIn] + resp <- Ok(s"got some: ${data}") + } yield resp + } + case GET -> Root / subscribe => { + val send: Stream[IO, WebSocketFrame] = + Stream.awakeEvery[IO](1.seconds).map(_ => WebSocketFrame.Text("text")) + val receive: Pipe[IO, WebSocketFrame, Unit] = _.evalMap { + case WebSocketFrame.Text(text, _) => Sync[IO].delay(println(text)) + case other => Sync[IO].delay(println(other)) + } + wsb.build(send, receive) + } + case _ => Ok("hello") + } + .orNotFound + + override def run: IO[Unit] = { + val a = 1 + IO.println(s"Hello, World! $a") >> + EmberServerBuilder + .default[IO] + .withHost(ipv4"0.0.0.0") + .withPort(port"8080") + .withHttpWebSocketApp(service) + .build + .use(_ => IO.never) + .as(ExitCode.Success) + } +} diff --git a/build.sbt b/build.sbt index ece00d9..c2fd62b 100644 --- a/build.sbt +++ b/build.sbt @@ -35,6 +35,9 @@ lazy val frontend = project ) .dependsOn(common) +val circeVersion = "0.14.1" +val http4sVersion = "1.0.0-M39" + lazy val backend = project .in(file("backend")) .settings( @@ -42,16 +45,21 @@ lazy val backend = project libraryDependencies ++= Seq( "org.http4s" %% "http4s-ember-client" % http4sVersion, "org.http4s" %% "http4s-ember-server" % http4sVersion, - "org.http4s" %% "http4s-dsl" % http4sVersion + "org.http4s" %% "http4s-dsl" % http4sVersion, + "org.http4s" %% "http4s-circe" % http4sVersion ), // available for 2.12, 2.13, 3.2 libraryDependencies += "co.fs2" %% "fs2-core" % "3.6.1", libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0", - libraryDependencies += "org.typelevel" %% "cats-effect" % "3.4.9" + libraryDependencies += "org.typelevel" %% "cats-effect" % "3.4.9", + libraryDependencies ++= Seq( + "io.circe" %% "circe-core", + "io.circe" %% "circe-generic", + "io.circe" %% "circe-parser" + ).map(_ % circeVersion) ) .dependsOn(common) -val http4sVersion = "1.0.0-M39" lazy val common = project .in(file("common")) .settings( diff --git a/common/src/main/scala/industries/sunshine/planningpoker/Requests.scala b/common/src/main/scala/industries/sunshine/planningpoker/Requests.scala new file mode 100644 index 0000000..25e3e39 --- /dev/null +++ b/common/src/main/scala/industries/sunshine/planningpoker/Requests.scala @@ -0,0 +1,5 @@ +package industries.sunshine.planningpoker + +object Requests { + final case class LogIn(roomName: String, nickname: String, password: String) +}