feat(11): enabled cask server with simple template

This commit is contained in:
efim 2023-06-30 14:06:15 +00:00
parent 770b4f6e87
commit ec5cac0680
4 changed files with 87 additions and 8 deletions

View File

@ -0,0 +1,2 @@
version = "3.7.3"
runner.dialect = scala3

View File

@ -1,4 +1,5 @@
ThisBuild / scalaVersion := "3.2.2"
fork := true
ThisBuild / version := "0.0.1"
ThisBuild / organization := "industries.sunshine"

View File

@ -0,0 +1,30 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Initial flie</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<!-- Place favicon.ico in the root directory -->
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
your experience.
</p>
<![endif]-->
<main>
<h1>Hello!</h1>
<p th:text="${myVar}" >
with static text
</p>
</main>
</body>
</html>

View File

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