feat(11): enabled cask server with simple template
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user