score:9
Most current browsers are automatically looking for favicon.ico
inside your site's root. So, you have to process their GET /favicon.ico
http-requests. The easiest way to do that is to use spray-routing
's getFromResource
:
import spray.routing.SimpleRoutingApp
import spray.http._
import MediaTypes._
object Main extends App with SimpleRoutingApp {
implicit val system = ActorSystem("my-system")
startServer(interface = "localhost", port = 8080) {
path("favicon.ico") {
getFromResource("favicon.ico", `image/x-icon`) // will look for the file inside your `resources` folder
}
}
}
If you already have some processing actor (and don't use spray-routing
), you will need to process GET /favicon.ico
directly inside your actor, returning something like:
def receive = {
case HttpRequest(GET, Uri.Path("/favicon.ico"), _, _, _) =>
sender ! HttpResponse(entity = HttpEntity(`image/x-icon`,
HttpData(new File("favicon.ico")))) //will take it from a file in application folder, you may also pass any array of byte instead of File
...
}
See another answer for more info about second option.
P.S. If you don't want to bother with favicon.ico
file - you may return just StatusCodes.NotFound
in both cases:
complete(NotFound)
for routingsender ! HttpResponse(NotFound)
for your own actor
As it's done inside SiteServiceActor
.
Also, using W3C's preferred method (putting the link inside your web pages) won't guarantee cross-browser compatibility as it depends on the browser's search order and there is no W3C standard about that (see siteData-36). Most browsers seems to look inside the site's root first, even if you don't have any html pages.
Source: stackoverflow.com
Related Query
- Where to put favicon for a scala Spray application (i.e. what is the root of the site?)?
- Needed help fixing the pom for a Scala application with Spray and Akka
- What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?
- What is the rule for parenthesis in Scala method invocation?
- What special rules does the scala compiler have for the unit type within the type system
- What is the Scala syntax for summing a List of objects?
- what are the options for hadoop on scala
- What is the accepted/recommended syntax for Scala code with lots of method-chaining?
- What are the use cases for Scala 2.9's try...catch generalization?
- What language is Scala written in, and where can the source be found?
- What is the Scala type-programming analogy for the `this` keyword?
- What is the scala percent operator (%) and at method for strings do?
- What are the best practices to deploy and host artifacts for a Docker Multicontainer environment in Elasticbeanstalk for Scala Apps?
- What is the Scala type mapping for all Spark SQL DataType
- What is the syntax for creating a Map in Scala that uses an enum as a key?
- What are the guarantees for scala access qualifiers?
- What are the obstacles for Scala having "const classes" a la Fantom?
- What is a "build manager" for scala in the scala ide extensions for Eclipse?
- What is the Scala syntax for calling a function with variadic arguments but with named arguments?
- What is the difference between curly bracket and parenthesis in a Scala for loop?
- What is the keyboard shortcut for ⇒ in your Scala editor of choice?
- What is this syntax called? And where is it explained in the Scala documentation?
- Spray is rejecting my request for the wrong reason (405 but should be 400) when a query parameter on a PUT is the wrong type
- Scala 2.12: What is the equivalent of Java 8 method reference for universally quantified SAM trait?
- Where are the test for Scala collections?
- What are the available parser(s) for scala programming language?
- What is the syntax for placeholder in Scala replace regex?
- Where does scaladoc look for the rootdoc.txt to create the root doc
- What is the equivalent Scala for the following Python statement?
- What is the proper behavior for empty string XML attributes in Scala
More Query from same tag
- What is the prefered way to factor out behavior
- Remove all items in PriorityQueue with value less than x
- covariant type T occurs in invariant position
- Comparing lists - checking if one list is a segment of second one
- Scala : Unable to send message to Kafka (hosted on remote server)
- In a SPARK dataframe, I want to groupBy, then orderBY and them concatenate rows of another column
- How to store data in Akka Actor Object fields?
- Why does using traits with type member give "error: type mismatch" in subtrait?
- Implementing a generic Vector in Scala
- Creating custom shell using scala
- Scala Spark sort RDD by index of substring
- Akka Streams' ActorPublisher as a Source for web response - how back-pressure works
- Scala Option: isDefined vs map based approach
- Import statment in Scala Play Framework doesn't work
- scala error: type arguments do not conform to class type parameter bounds [X >: A,Y >: A]
- sun.misc.Contended is not a member of package sun.misc Scala
- How to covert List to Tree without stack overflow
- BoxedUnit vs. Unit in Scala
- Is it instance name/id that scala REPL prints?
- Problems with Scala Play Framework Slick Session
- How to update value of a key in a immutable tree map in Scala
- What is the best design for set of elements with different type parameters in Scala
- Count the number of conditions satisfied by a set of variable values
- Scala Queue and NoSuchElementException
- Lost message when stoping and restarting Embedded ActiveMQ
- In SBT 1.3.8 how can I figure out who is bringing in a dependency?
- java.lang.IndexOutOfBoundsException: No group 1 | Pattern matching
- In Scala, Refer to Subclass in Abstract Super Class
- Parse a JSON gives JsResultException
- Scala class with a private constructor and implicit parameter