feat(12): adding thymeleaf lib and template resp
This commit is contained in:
parent
d0f8cd771e
commit
509c45b357
|
@ -15,6 +15,7 @@ lazy val root = (project in file("."))
|
||||||
libraryDependencies += (toolkitTest % Test),
|
libraryDependencies += (toolkitTest % Test),
|
||||||
libraryDependencies ++= Seq(
|
libraryDependencies ++= Seq(
|
||||||
"com.lihaoyi" %% "cask" % "0.9.1",
|
"com.lihaoyi" %% "cask" % "0.9.1",
|
||||||
"com.lihaoyi" %% "mainargs" % "0.5.0"
|
"com.lihaoyi" %% "mainargs" % "0.5.0",
|
||||||
|
"org.thymeleaf" % "thymeleaf" % "3.1.1.RELEASE"
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
<p>Hello, <span th:text="${name}">Name</span>!</p>
|
|
@ -1,3 +1,6 @@
|
||||||
|
//> using dep com.lihaoyi::cask:0.9.1
|
||||||
|
//> using dep com.lihaoyi::mainargs:0.5.0
|
||||||
|
//> using dep org.thymeleaf:thymeleaf:3.1.1.RELEASE
|
||||||
package example
|
package example
|
||||||
|
|
||||||
import mainargs.{main, arg, ParserForMethods}
|
import mainargs.{main, arg, ParserForMethods}
|
||||||
|
@ -12,9 +15,53 @@ object Main {
|
||||||
portArg: Int = 8080,
|
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"
|
hostArg: String = "localhost"
|
||||||
): Unit =
|
): Unit = {
|
||||||
println(s"Will start server on ${hostArg}:${portArg}")
|
println(s"Will start server on ${hostArg}:${portArg}")
|
||||||
|
val server = new cask.Main {
|
||||||
|
override val allRoutes = Seq(AppRoutes())
|
||||||
|
override def port = portArg
|
||||||
|
override def host = hostArg
|
||||||
|
}
|
||||||
|
server.main(Array.empty)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
|
def main(args: Array[String]): Unit = ParserForMethods(this).runOrExit(args)
|
||||||
|
|
||||||
|
case class AppRoutes()(implicit cc: castor.Context, log: cask.Logger)
|
||||||
|
extends cask.Routes {
|
||||||
|
@cask.get("/")
|
||||||
|
def index() = {
|
||||||
|
import org.thymeleaf.context.Context
|
||||||
|
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
|
||||||
|
import org.thymeleaf.TemplateEngine
|
||||||
|
|
||||||
|
val templateResolver = new ClassLoaderTemplateResolver()
|
||||||
|
templateResolver.setPrefix("templates/");
|
||||||
|
templateResolver.setSuffix(".html")
|
||||||
|
templateResolver.setTemplateMode("HTML5")
|
||||||
|
|
||||||
|
val templateEngine = new TemplateEngine()
|
||||||
|
templateEngine.setTemplateResolver(templateResolver)
|
||||||
|
|
||||||
|
val a = 11234
|
||||||
|
val context = new Context()
|
||||||
|
context.setVariable("name", s"Johny $a")
|
||||||
|
|
||||||
|
val result = templateEngine.process("basic-template", context)
|
||||||
|
|
||||||
|
cask.Response(
|
||||||
|
result,
|
||||||
|
headers = Seq("Content-Type" -> "text/html;charset=UTF-8")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@cask.staticFiles("/dist") // this is what path gets matched
|
||||||
|
def distFiles() = "dist"
|
||||||
|
|
||||||
|
@cask.staticFiles("/public") // this is what path gets matched
|
||||||
|
def publicFiles() = "public"
|
||||||
|
|
||||||
|
initialize()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue