From ec5cac06806ad052d8b695abb21f209b77fee125 Mon Sep 17 00:00:00 2001 From: efim Date: Fri, 30 Jun 2023 14:06:15 +0000 Subject: [PATCH] feat(11): enabled cask server with simple template --- 14-rock-paper-scissors/.scalafmt.conf | 2 + 14-rock-paper-scissors/build.sbt | 1 + .../src/main/resources/templates/index.html | 30 +++++++++ .../main/scala/rockpaperscissors/Main.scala | 62 ++++++++++++++++--- 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 14-rock-paper-scissors/.scalafmt.conf create mode 100644 14-rock-paper-scissors/src/main/resources/templates/index.html diff --git a/14-rock-paper-scissors/.scalafmt.conf b/14-rock-paper-scissors/.scalafmt.conf new file mode 100644 index 0000000..db004c6 --- /dev/null +++ b/14-rock-paper-scissors/.scalafmt.conf @@ -0,0 +1,2 @@ +version = "3.7.3" +runner.dialect = scala3 \ No newline at end of file diff --git a/14-rock-paper-scissors/build.sbt b/14-rock-paper-scissors/build.sbt index 08f2d59..ace61d9 100644 --- a/14-rock-paper-scissors/build.sbt +++ b/14-rock-paper-scissors/build.sbt @@ -1,4 +1,5 @@ ThisBuild / scalaVersion := "3.2.2" +fork := true ThisBuild / version := "0.0.1" ThisBuild / organization := "industries.sunshine" diff --git a/14-rock-paper-scissors/src/main/resources/templates/index.html b/14-rock-paper-scissors/src/main/resources/templates/index.html new file mode 100644 index 0000000..7f00571 --- /dev/null +++ b/14-rock-paper-scissors/src/main/resources/templates/index.html @@ -0,0 +1,30 @@ + + + + + + Initial flie + + + + + + + + + +
+

Hello!

+

+ with static text +

+
+ + + diff --git a/14-rock-paper-scissors/src/main/scala/rockpaperscissors/Main.scala b/14-rock-paper-scissors/src/main/scala/rockpaperscissors/Main.scala index f468e0a..6538b3a 100644 --- a/14-rock-paper-scissors/src/main/scala/rockpaperscissors/Main.scala +++ b/14-rock-paper-scissors/src/main/scala/rockpaperscissors/Main.scala @@ -1,21 +1,67 @@ package rockpaperscissors import mainargs.{main, arg, ParserForMethods} +import cask.main.Routes +import org.thymeleaf.context.Context +import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver +import org.thymeleaf.TemplateEngine object Main { @main def run( - @arg( - name = "port", - short = 'p', - doc = "Port on which server will start serving" - ) + @arg( + name = "port", + short = 'p', + doc = "Port on which server will start serving" + ) portArg: Int = 8080, - @arg(name = "host", doc = "Host on which server will start serving.") + @arg(name = "host", doc = "Host on which server will start serving.") hostArg: String = "localhost" ): Unit = { - val a = 1 println(s"Will start server on ${hostArg}:${portArg}") + val server = new cask.Main { + override def allRoutes: Seq[Routes] = Seq(AppRoutes()) + override def port: Int = portArg + override def host: String = hostArg + } + server.main(Array.empty) + } + + def main(args: Array[String]): Unit = + { + println(s"got args : $args") + ParserForMethods(this).runOrExit(args) + } + + case class AppRoutes()(implicit cc: castor.Context, log: cask.Logger) + extends cask.Routes { + val templateResolver = new ClassLoaderTemplateResolver() + templateResolver.setPrefix("templates/"); + templateResolver.setSuffix(".html") + templateResolver.setTemplateMode("HTML5") + + val templateEngine = new TemplateEngine() + templateEngine.setTemplateResolver(templateResolver) + + @cask.get("/") + def index() = { + val context = new Context() + context.setVariable( + "myVar", + "Hello, from Scala world" + ) + val result = templateEngine.process("index", context) + cask.Response( + result, + headers = Seq("Content-Type" -> "text/html;charset=UTF-8") + ) + } + + @cask.staticFiles("/dist") + def distFiles() = "dist" + @cask.staticFiles("/public") + def publicFiles() = "public" + + initialize() } - def main(args: Array[String]) = ParserForMethods(this).runOrExit(args) }