mirror of
https://github.com/phfroidmont/scalive.git
synced 2025-12-25 05:26:59 +01:00
Serve static html rendering
This commit is contained in:
parent
62c1a8a9f4
commit
f53a1cab66
3 changed files with 110 additions and 2 deletions
|
|
@ -1,11 +1,16 @@
|
||||||
package build
|
package build
|
||||||
import mill.*, scalalib.*
|
import mill.*, scalalib.*
|
||||||
|
|
||||||
object core extends ScalaModule:
|
trait Common extends ScalaModule:
|
||||||
def scalaVersion = "3.7.2"
|
def scalaVersion = "3.7.2"
|
||||||
def scalacOptions = Seq("-Wunused:all")
|
def scalacOptions = Seq("-Wunused:all")
|
||||||
|
|
||||||
|
object core extends Common:
|
||||||
def mvnDeps = Seq(mvn"dev.zio::zio-json:0.7.44")
|
def mvnDeps = Seq(mvn"dev.zio::zio-json:0.7.44")
|
||||||
|
|
||||||
object test extends ScalaTests with TestModule.Utest:
|
object test extends ScalaTests with TestModule.Utest:
|
||||||
def utestVersion = "0.9.0"
|
def utestVersion = "0.9.0"
|
||||||
|
|
||||||
|
object zio extends Common:
|
||||||
|
def mvnDeps = Seq(mvn"dev.zio::zio-http:3.4.0")
|
||||||
|
override def moduleDeps = Seq(core)
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,6 @@ def main =
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
println(lv.fullDiff.toJsonPretty)
|
println(lv.fullDiff.toJsonPretty)
|
||||||
|
|
||||||
println(HtmlBuilder.build(lv))
|
println(HtmlBuilder.build(lv))
|
||||||
|
|
||||||
println("Edit first and last")
|
println("Edit first and last")
|
||||||
|
|
@ -30,6 +29,7 @@ def main =
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
println(lv.diff.toJsonPretty)
|
println(lv.diff.toJsonPretty)
|
||||||
|
println(HtmlBuilder.build(lv))
|
||||||
|
|
||||||
println("Add one")
|
println("Add one")
|
||||||
lv.update(
|
lv.update(
|
||||||
|
|
@ -43,6 +43,7 @@ def main =
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
println(lv.diff.toJsonPretty)
|
println(lv.diff.toJsonPretty)
|
||||||
|
println(HtmlBuilder.build(lv))
|
||||||
|
|
||||||
println("Remove first")
|
println("Remove first")
|
||||||
lv.update(
|
lv.update(
|
||||||
|
|
@ -55,12 +56,14 @@ def main =
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
println(lv.diff.toJsonPretty)
|
println(lv.diff.toJsonPretty)
|
||||||
|
println(HtmlBuilder.build(lv))
|
||||||
|
|
||||||
println("Remove all")
|
println("Remove all")
|
||||||
lv.update(
|
lv.update(
|
||||||
MyModel(List.empty, "text-lg")
|
MyModel(List.empty, "text-lg")
|
||||||
)
|
)
|
||||||
println(lv.diff.toJsonPretty)
|
println(lv.diff.toJsonPretty)
|
||||||
|
println(HtmlBuilder.build(lv))
|
||||||
|
|
||||||
final case class MyModel(elems: List[NestedModel], cls: String = "text-xs")
|
final case class MyModel(elems: List[NestedModel], cls: String = "text-xs")
|
||||||
final case class NestedModel(name: String, age: Int)
|
final case class NestedModel(name: String, age: Int)
|
||||||
|
|
|
||||||
100
zio/src/scalive/Example.scala
Normal file
100
zio/src/scalive/Example.scala
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
package scalive
|
||||||
|
|
||||||
|
import zio._
|
||||||
|
|
||||||
|
import zio.http.ChannelEvent.{
|
||||||
|
ExceptionCaught,
|
||||||
|
Read,
|
||||||
|
UserEvent,
|
||||||
|
UserEventTriggered
|
||||||
|
}
|
||||||
|
import zio.http._
|
||||||
|
import zio.http.codec.PathCodec.string
|
||||||
|
import zio.http.template.Html
|
||||||
|
|
||||||
|
object Example extends ZIOAppDefault {
|
||||||
|
|
||||||
|
val lv =
|
||||||
|
LiveView(
|
||||||
|
TestView,
|
||||||
|
MyModel(
|
||||||
|
List(
|
||||||
|
NestedModel("a", 10),
|
||||||
|
NestedModel("b", 15),
|
||||||
|
NestedModel("c", 20)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
val socketApp: WebSocketApp[Any] =
|
||||||
|
Handler.webSocket { channel =>
|
||||||
|
channel.receiveAll {
|
||||||
|
case Read(WebSocketFrame.Text("end")) =>
|
||||||
|
channel.shutdown
|
||||||
|
|
||||||
|
// Send a "bar" if the client sends a "foo"
|
||||||
|
case Read(WebSocketFrame.Text("foo")) =>
|
||||||
|
channel.send(Read(WebSocketFrame.text("bar")))
|
||||||
|
|
||||||
|
// Send a "foo" if the client sends a "bar"
|
||||||
|
case Read(WebSocketFrame.Text("bar")) =>
|
||||||
|
channel.send(Read(WebSocketFrame.text("foo")))
|
||||||
|
|
||||||
|
// Echo the same message 10 times if it's not "foo" or "bar"
|
||||||
|
case Read(WebSocketFrame.Text(text)) =>
|
||||||
|
channel
|
||||||
|
.send(Read(WebSocketFrame.text(s"echo $text")))
|
||||||
|
.repeatN(10)
|
||||||
|
.catchSomeCause { case cause =>
|
||||||
|
ZIO.logErrorCause(s"failed sending", cause)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send a "greeting" message to the client once the connection is established
|
||||||
|
case UserEventTriggered(UserEvent.HandshakeComplete) =>
|
||||||
|
channel.send(Read(WebSocketFrame.text("Greetings!")))
|
||||||
|
|
||||||
|
// Log when the channel is getting closed
|
||||||
|
case Read(WebSocketFrame.Close(status, reason)) =>
|
||||||
|
Console.printLine(
|
||||||
|
"Closing channel with status: " + status + " and reason: " + reason
|
||||||
|
)
|
||||||
|
|
||||||
|
// Print the exception if it's not a normal close
|
||||||
|
case ExceptionCaught(cause) =>
|
||||||
|
Console.printLine(s"Channel error!: ${cause.getMessage}")
|
||||||
|
|
||||||
|
case _ =>
|
||||||
|
ZIO.unit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val routes: Routes[Any, Response] =
|
||||||
|
Routes(
|
||||||
|
Method.GET / "" -> handler { (_: Request) =>
|
||||||
|
Response.html(Html.raw(HtmlBuilder.build(lv)))
|
||||||
|
},
|
||||||
|
Method.GET / "live" / "ws" -> handler(socketApp.toResponse)
|
||||||
|
)
|
||||||
|
|
||||||
|
override val run = Server.serve(routes).provide(Server.default)
|
||||||
|
}
|
||||||
|
|
||||||
|
final case class MyModel(elems: List[NestedModel], cls: String = "text-xs")
|
||||||
|
final case class NestedModel(name: String, age: Int)
|
||||||
|
|
||||||
|
object TestView extends View[MyModel]:
|
||||||
|
val root: HtmlElement[MyModel] =
|
||||||
|
div(
|
||||||
|
idAttr := "42",
|
||||||
|
cls := model(_.cls),
|
||||||
|
ul(
|
||||||
|
model.splitByIndex(_.elems)(elem =>
|
||||||
|
li(
|
||||||
|
"Nom: ",
|
||||||
|
elem(_.name),
|
||||||
|
" Age: ",
|
||||||
|
elem(_.age.toString)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue