Alexander Suraphel
Alexander Suraphel

Reputation: 10633

What is the difference between static files and embedded application resources in Ktor?

I'm reading the ktor documentation on serving static content and it's not clear to me what the difference between files("css") and resources("css") is.

Upvotes: 2

Views: 637

Answers (1)

Aleksei Tirman
Aleksei Tirman

Reputation: 7189

The static method is equivalent to the route method so it just creates a path route in the routing tree.

The files method allows serving all static files from the provided path (directory) from a local filesystem. Relative paths will be resolved using the current working directory.

The resources method does the same as the files method except that it allows serving static files from the classpath.

Here is an example:

// Assume that current working directory is /home/user/project
embeddedServer(Netty, port = 8080) {
    routing {
        // This route will be resolved if a request path starts with /assets/
        static("assets") {
            // For the request path /assets/style.css the file /home/user/project/css/style.css will be served
            files("./css")
            // It's the same as above
            files("css")
            // For the request path /assets/data.txt the file /absolute/path/to/data.txt will be served
            files("/absolute/path/to")

            // For the request path /assets/style.css the file <resources>/css/style.css will be served
            // where <resources> is the embedded resource directory
            resources("css")
        }
    }
}.start(wait = true)

Upvotes: 4

Related Questions