backend - adding room service and wiring
preparing to create a better auth module that uses room service
This commit is contained in:
@@ -6,7 +6,8 @@ import cats.data.OptionT
|
||||
import org.http4s.Request
|
||||
import industries.sunshine.planningpoker.common.Models.PlayerID
|
||||
import java.util.UUID
|
||||
import industries.sunshine.planningpoker.common.Models.RoomID
|
||||
import industries.sunshine.planningpoker.common.Models.{RoomID, Room}
|
||||
import org.http4s.ResponseCookie
|
||||
|
||||
trait Auth {
|
||||
|
||||
@@ -24,10 +25,10 @@ trait Auth {
|
||||
roomName: String,
|
||||
roomPassword: String,
|
||||
nickName: String
|
||||
): IO[Either[Unit, Long]]
|
||||
): IO[Either[Unit, ResponseCookie]]
|
||||
|
||||
def leaveRoom(
|
||||
sessionId: Long
|
||||
sessionId: Long
|
||||
): IO[Unit]
|
||||
|
||||
}
|
||||
@@ -36,38 +37,56 @@ object Auth {
|
||||
def pureBadStub = new Auth {
|
||||
override def authUser =
|
||||
Kleisli((req: Request[IO]) =>
|
||||
OptionT.liftF(IO(println(s"authUser: $req")) >> IO(PlayerID(14) -> RoomID(101)))
|
||||
OptionT.liftF(
|
||||
IO(println(s"authUser: $req")) >> IO(
|
||||
PlayerID(14) -> RoomID("testroom")
|
||||
)
|
||||
)
|
||||
)
|
||||
override def accessRoom(
|
||||
roomName: String,
|
||||
roomPassword: String,
|
||||
nickName: String
|
||||
) =
|
||||
IO(println(s"> access room for $roomName $roomPassword $nickName, to return stub 111")) >> IO.pure(Right(111L))
|
||||
IO(
|
||||
println(
|
||||
s"> access room for $roomName $roomPassword $nickName, to return stub 111"
|
||||
)
|
||||
) >> IO.pure(Right(ResponseCookie("authcookie", "1")))
|
||||
|
||||
override def leaveRoom(sessionId: Long): IO[Unit] =
|
||||
IO(s"got request to leave for $sessionId")
|
||||
}
|
||||
|
||||
type SessionsMap = Map[Long, (RoomID, PlayerID)]
|
||||
val sessionsRef = Ref.of[IO, SessionsMap](Map.empty)
|
||||
val roomPasswordsRef = Ref.of[IO, Map[Long, String]](Map.empty)
|
||||
val sessionsRef =
|
||||
Ref.of[IO, SessionsMap](Map(1L -> (RoomID("testroom"), PlayerID(1L))))
|
||||
val roomsRef =
|
||||
Ref.of[IO, Map[RoomID, Room]](Map(RoomID("testroom") -> Room.testRoom))
|
||||
|
||||
def apply(): IO[Auth] =
|
||||
for {
|
||||
store <- sessionsRef
|
||||
roomPasswords <- roomPasswordsRef
|
||||
rooms <- roomsRef
|
||||
} yield new Auth {
|
||||
|
||||
val authcookie = "authcookie"
|
||||
|
||||
override def authUser = Kleisli { (req: Request[IO]) =>
|
||||
{
|
||||
???
|
||||
??? // oh, this one for when cookie present
|
||||
}
|
||||
}
|
||||
import cats.syntax.either._
|
||||
import cats.syntax.option._
|
||||
override def accessRoom(
|
||||
roomName: String,
|
||||
roomPassword: String,
|
||||
nickName: String
|
||||
): IO[Either[Unit, Long]] = ???
|
||||
): IO[Either[Unit, ResponseCookie]] = {
|
||||
|
||||
???
|
||||
}
|
||||
|
||||
override def leaveRoom(sessionId: Long): IO[Unit] = ???
|
||||
}
|
||||
|
||||
@@ -12,17 +12,21 @@ object BackendApp extends IOApp {
|
||||
val host = host"0.0.0.0"
|
||||
val port = port"8080"
|
||||
|
||||
val server = for {
|
||||
srv <- EmberServerBuilder
|
||||
val wiring = for {
|
||||
roomService <- Resource.eval(RoomService.make[IO])
|
||||
httpService = MyHttpService.create(Auth.pureBadStub)
|
||||
server <- EmberServerBuilder
|
||||
.default[IO]
|
||||
.withHost(host)
|
||||
.withPort(port)
|
||||
.withHttpWebSocketApp(MyHttpService.create(Auth.pureBadStub)(_))
|
||||
.withHttpWebSocketApp(httpService(_))
|
||||
.build
|
||||
} yield srv
|
||||
} yield server
|
||||
|
||||
server.use(server =>
|
||||
IO.delay(println(s"Server Has Started at ${server.address}")) >> IO.never.as(ExitCode.Success))
|
||||
wiring.use(server =>
|
||||
IO.delay(println(s"Server Has Started at ${server.address}")) >> IO.never
|
||||
.as(ExitCode.Success)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,12 +62,9 @@ object MyHttpService {
|
||||
resp <- authResult match {
|
||||
case Left(error) =>
|
||||
Forbidden(error)
|
||||
case Right(sessionId) => {
|
||||
Ok("Logged in!").map(
|
||||
_.addCookie(
|
||||
ResponseCookie("authcookie", sessionId.toString())
|
||||
)
|
||||
)
|
||||
case Right(authCookie) => {
|
||||
IO(println(s"> logging in ${data.nickname} to ${data.roomName}")) >>
|
||||
Ok().map(_.addCookie(authCookie))
|
||||
}
|
||||
}
|
||||
} yield resp
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package industries.sunshine.planningpoker
|
||||
|
||||
import industries.sunshine.planningpoker.common.Models.*
|
||||
import cats.effect.{Ref, Sync}
|
||||
import cats.syntax.all._
|
||||
|
||||
enum RoomError {
|
||||
case RoomAlreadyExists(name: String)
|
||||
}
|
||||
|
||||
trait RoomService[F[_]] {
|
||||
def createRoom(newRoom: Room): F[Either[RoomError, Room]]
|
||||
def updateRoom(room: Room): F[Unit]
|
||||
def deleteRoom(roomID: RoomID): F[Unit]
|
||||
def getRoom(roomID: RoomID): F[Option[Room]]
|
||||
}
|
||||
|
||||
class InMemoryRoomService[F[_]: Sync](stateRef: Ref[F, Map[RoomID, Room]])
|
||||
extends RoomService[F] {
|
||||
override def createRoom(newRoom: Room): F[Either[RoomError, Room]] = {
|
||||
stateRef.modify { rooms =>
|
||||
rooms.get(newRoom.id) match {
|
||||
case Some(_) =>
|
||||
rooms -> RoomError.RoomAlreadyExists(newRoom.id.name).asLeft[Room]
|
||||
case None =>
|
||||
(rooms.updated(newRoom.id, newRoom)) -> newRoom.asRight[RoomError]
|
||||
}
|
||||
}
|
||||
}
|
||||
def updateRoom(room: Room): F[Unit] = stateRef.update { state =>
|
||||
state.get(room.id).fold(state)(oldRoom => state.updated(room.id, room))
|
||||
}
|
||||
|
||||
def deleteRoom(roomID: RoomID): F[Unit] = stateRef.update(_.removed(roomID))
|
||||
|
||||
def getRoom(roomID: RoomID): F[Option[Room]] = stateRef.get.map(_.get(roomID))
|
||||
|
||||
}
|
||||
object RoomService {
|
||||
def make[F[_]: Sync]: F[RoomService[F]] = {
|
||||
Ref.of[F, Map[RoomID, Room]](Map.empty).map(new InMemoryRoomService[F](_))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user