Serving static content
Whether we're creating a website or an HTTP endpoint, many applications need to serve files (such as stylesheets, scripts, images, etc.).
While it is certainly possible with Ktor to load the contents of a file and send it in response to a request, given this is such a common functionality, Ktor simplifies the entire process for us with the static plugin (previously known as feature).
The first step in defining a static route is to define the path under which the content should be served. For instance, if you want everything under the route assets to be treated as static content, you need to add the following to your application setup:
The next step is to define where we want the content to be served from, which can be either
Folders
In order to serve the contents from a folder, we need to specify the folder name using the files function. The path is always relative to the application path:
files("css") would then allow for any file located in the folder css to be served as static content under the given URL pattern, which in this case is assets. This means that a request to
/assets/stylesheet.css would serve the file /css/stylesheet.css
We can have as many folders as we like under a single path. For instance the following would also be valid:
Serving individual files
In addition to serving files from folders, we can also specify individuals files we would like to make available by using the file function. Optionally this takes a second argument which allows us to map a physical filename to a virtual one:
Serving all files including in subfolders
The files function can also take the string ".", which will serve up any file as long as the request path and physical filename match.
Defining a default file
For a specific path, we can also define the default file to be loaded:
which would cause a request to /assets to serve index.html.
Serving pre-compressed files
Ktor provides the ability to serve pre-compressed files and avoid using dynamic compression. For example, to serve pre-compressed files from the assets/html folder, call files inside the preCompressed function in the following way:
You can also raise the priority of one compression type over another. In the example below, Ktor tries to serve *.br files over *.gz:
For example, for a request made to /static/index.html Ktor tries to serve assets/html/index.html.br first.
Changing the default root folder
Ktor also provides us the ability to specify a different root folder from where contents is served. This is useful for instance if we want to dynamically define where contents should be served from, or even use absolute paths.
We can do this by setting the value of the staticRootFolder property:
which would then map any request to /docs to the physical folder /system/folder/docs/public.
Embedded Application Resources
We can embed content as resources in our applications and serve these using the resource and resources functions:
resources("css") would then allow for any file located under the resource css to be served as static content under the given URL pattern, which in this case is assets. This means that a request to
/assets/stylesheet.cs would serve the file /css/stylesheet.cs
We can have as many resources as we like under a single path. For instance the following would also be valid:
Serving individual resources
In addition to serving files from resources, we can also specify individuals files we would like to make available by using the resource function. Optionally this takes a second argument which allows us to map a physical filename to a virtual one:
Defining a default resource
For a specific path, we can also define the default file to be loaded:
Changing the default resource package
Ktor also provides us the ability to specify a different base resource package from where contents is served.
We can do this by setting the value of the staticBasePackage property:
Sub-routes
If we want to have sub-routes, we can nest static functions:
allowing for /assets/themes to load files from the /data
Handling errors
If the request content is not found, Ktor will automatically respond with a 404 Not Found HTTP status code. For more information about personalising error handling, please see status pages.
Customising Content Type header
Ktor automatically looks up the content type of a file based on its extension and sets the appropriate Content-Type header. The list of supported MIME types is defined in the mimelist.csv resource file located in ktor-server-core artifact.
Example
An example application that serves static files using both folders and resources can be found below: