add sub routes to backend app

in the shape of websocket and post \ get
with receiving of some request model
This commit is contained in:
efim 2023-04-23 11:55:53 +04:00
parent d8af92787d
commit c501d14094
4 changed files with 70 additions and 35 deletions

View File

@ -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)
}
}

View File

@ -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)
}
}

View File

@ -35,6 +35,9 @@ lazy val frontend = project
) )
.dependsOn(common) .dependsOn(common)
val circeVersion = "0.14.1"
val http4sVersion = "1.0.0-M39"
lazy val backend = project lazy val backend = project
.in(file("backend")) .in(file("backend"))
.settings( .settings(
@ -42,16 +45,21 @@ lazy val backend = project
libraryDependencies ++= Seq( libraryDependencies ++= Seq(
"org.http4s" %% "http4s-ember-client" % http4sVersion, "org.http4s" %% "http4s-ember-client" % http4sVersion,
"org.http4s" %% "http4s-ember-server" % 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 // available for 2.12, 2.13, 3.2
libraryDependencies += "co.fs2" %% "fs2-core" % "3.6.1", libraryDependencies += "co.fs2" %% "fs2-core" % "3.6.1",
libraryDependencies += "org.typelevel" %% "cats-core" % "2.9.0", 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) .dependsOn(common)
val http4sVersion = "1.0.0-M39"
lazy val common = project lazy val common = project
.in(file("common")) .in(file("common"))
.settings( .settings(

View File

@ -0,0 +1,5 @@
package industries.sunshine.planningpoker
object Requests {
final case class LogIn(roomName: String, nickname: String, password: String)
}