@cask.staticResources("/static")
def static() = "todo"
/public/
I couldn't get anything to work. Also I don't understand the return value from def static()
.
object Router extends LazyLogging:
private val utf8 = Codec.UTF8.name
private val contentType = "Content-Type"
private val basePath = "/public/"
private val indexHtml = loadResource("index.html")
private val indexHtmlHeader = contentType -> "text/html; charset=UTF-8"
def loadResource(resource: String): String =
val path = s"$basePath$resource"
logger.debug(s"*** load resource: $path")
Using( Source.fromInputStream(getClass.getResourceAsStream(path), utf8) ) {
source => source.mkString
}.getOrElse("")
def toHeader(resource: String): (String, String) =
logger.debug(s"*** to header: ${resource.split('.').last}")
resource.split('.').last match
case "css" => contentType -> "text/css"
case "ico" => contentType -> "image/x-icon"
case "png" => contentType -> "image/png"
case "js" => contentType -> "text/javascript"
case "map" => contentType -> "application/json"
case "html" => indexHtmlHeader
case _ => contentType -> "text/plain"
class Router(dispatcher: Dispatcher) extends Routes with LazyLogging:
import Router._
@cask.get("/")
def index() = Response(indexHtml, 200, Seq(indexHtmlHeader))
@cask.get(Router.basePath, subpath = true)
def resources(request: Request) =
val resource = request.remainingPathSegments.head
val content = loadResource(resource)
val headers = Seq(toHeader(resource))
logger.debug(s"*** headers: $headers")
Response(content, 200, headers)
@cask.post("/command")
def command(request: Request) =
val command = read[Command](request.text())
logger.debug(s"*** Command: $command")
val event = dispatcher.dispatch(command)
logger.debug(s"*** Event: $event")
write[Event](event)
initialize()
import cask.main.Routes
import cask.model.{Request, Response}
import com.typesafe.scalalogging.LazyLogging
import java.awt.image.BufferedImage
import java.io.ByteArrayOutputStream
import javax.imageio.ImageIO
import scala.io.{Codec, Source}
import scala.util.{Try, Using}
trait Resources(val basePath: String) extends LazyLogging:
val utf8 = Codec.UTF8.name
val contentType = "Content-Type"
val indexHtml = loadResource("index.html")
val indexHtmlHeader = contentType -> "text/html; charset=UTF-8"
def toContentType(resource: String): String = resource.split('.').last
def toPath(resource: String): String = s"$basePath$resource"
def toHeader(resource: String): (String, String) =
logger.debug(s"*** to header: ${toContentType(resource)}")
toContentType(resource) match
case "css" => contentType -> "text/css"
case "ico" => contentType -> "image/x-icon"
case "png" => contentType -> "image/png"
case "js" => contentType -> "text/javascript"
case "map" => contentType -> "application/json"
case "html" => indexHtmlHeader
case _ => contentType -> "text/plain"
def isImage(resource: String): Boolean =
toContentType(resource) match
case "ico" | "png" => true
case _ => false
def loadResource(resource: String): Array[Byte] =
val path = toPath(resource)
logger.debug(s"*** load resource: $path")
Using( Source.fromInputStream(getClass.getResourceAsStream(path), utf8) ) {
source => source.mkString.getBytes
}.getOrElse(Array.empty[Byte])
def loadImage(resource: String): Array[Byte] =
val path = toPath(resource)
logger.debug(s"*** load image: $path")
val url = getClass.getResource(path)
val image = ImageIO.read(url)
val baos = new ByteArrayOutputStream()
val contentType = toContentType(resource)
ImageIO.write(image, contentType, baos)
baos.toByteArray
class Router(dispatcher: Dispatcher) extends Routes with LazyLogging with Resources("/public/"):
@cask.get("/")
def index() = Response(indexHtml, 200, Seq(indexHtmlHeader))
@cask.get(basePath, subpath = true)
def resources(request: Request) =
val resource = request.remainingPathSegments.head
val headers = Seq(toHeader(resource))
if isImage(resource) then Response(loadImage(resource), 200, headers)
else Response(loadResource(resource), 200, headers)
initialize()
https://github.com/objektwerks/cask/blob/master/src/main/scala/objektwerks/handler/CorsHandler.scala
https://github.com/objektwerks/cask/blob/master/src/main/scala/objektwerks/Server.scala
Hello guys, do you know how can I run a cask project with sbt.
object RadarApp extends cask.Main:
val allRoutes: Seq[ApiRoutes] = Seq(ApiRoutes())
override def port: Int = 8888
override def main(args: Array[String]): Unit = super.main(args)
When running it as
sbt "run com.example.RadarApp"
I get the following message:
[info] running com.example.RadarApp com.example.RadarApp
Jun 10, 2022 8:36:28 PM org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 3.1.0.Final
[success] Total time: 1 s, completed Jun 10, 2022, 8:36:28 PM
Sorry to post the same question again (didn't know this channel exists).
I am learning to use cask (https://com-lihaoyi.github.io/cask/#getting-started). But I don't use mill as build tool. Instead I use sbt. In that case, after typing sbt run
in the command line, following messages are returned. However accessing to http://localhost:8080 shows Unable to connect
, unless run sbt
process first, and then type run
or runMain ...
. How can I launch the server without executing sbt process? Thanks
info] running metricsapp.App
Jun 24, 2022 3:42:34 PM org.jboss.threads.Version <clinit>
INFO: JBoss Threads version 3.1.0.Final
[success] Total time: 1 s, completed Jun 24, 2022, 3:42:34 PM
I was trying to upgrade scala from 2.13.3
to 2.13.8
and various dependencies, and I am getting a strange error because of a change in behaviour for static resources, which I am using for css and javascript (the latter built with mill and copied).
http://localhost:8080/public/css/bootstrap.min.css
the browser insists on downloading the file, not showing it.The code with the static resource is:
@cask.staticResources("/public")
def staticResourceRoutes() = "."
The strangest thing is I got this error before I updated cask (but updated other stuff) and also after. The changes are in scalaVersion
, scalaJSVersion
, os-lib
, upickle
and scalatags
.
It seems to me that I should set some header, but I cannot figure out what. Any help is appreciated.