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:
parent
d8af92787d
commit
c501d14094
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
}
|
14
build.sbt
14
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(
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package industries.sunshine.planningpoker
|
||||
|
||||
object Requests {
|
||||
final case class LogIn(roomName: String, nickname: String, password: String)
|
||||
}
|
Loading…
Reference in New Issue