Coverage Summary for Class: StaticFilesPlugin (kweb.plugins.staticFiles)
Class |
Method, %
|
Branch, %
|
Line, %
|
Instruction, %
|
StaticFilesPlugin |
75%
(3/4)
|
|
66.7%
(4/6)
|
77.6%
(45/58)
|
StaticFilesPlugin$1 |
0%
(0/1)
|
|
0%
(0/1)
|
0%
(0/4)
|
StaticFilesPlugin$2 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(5/5)
|
StaticFilesPlugin$appServerConfigurator$1 |
100%
(1/1)
|
50%
(1/2)
|
100%
(3/3)
|
85.2%
(23/27)
|
StaticFilesPlugin$appServerConfigurator$1$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(8/8)
|
StaticFilesPlugin$appServerConfigurator$1$1$1 |
100%
(1/1)
|
|
100%
(1/1)
|
100%
(19/19)
|
Total |
77.8%
(7/9)
|
50%
(1/2)
|
76.9%
(10/13)
|
82.6%
(100/121)
|
package kweb.plugins.staticFiles
import io.ktor.server.application.install
import io.ktor.server.plugins.cachingheaders.*
import io.ktor.http.CacheControl
import io.ktor.http.content.*
import io.ktor.server.http.content.*
import io.ktor.server.routing.Route
import io.ktor.server.routing.Routing
import kweb.plugins.KwebPlugin
import java.io.File
/**
* @author rpanic
*
* This Plugin serves static files to be used in the frontend
*
* @property rootFolder The root folder, where the static assets are saved
* @property resourceFolder For serving resources, the path to the folder which will be served
* @property servedRoute The route where these assets are being served
*/
class StaticFilesPlugin private constructor(private val servedRoute: String, private val maxCacheAgeSeconds: Int = 60 * 60) : KwebPlugin() {
private lateinit var datasource: (Route) -> Unit
constructor(rootFolder: File, servedRoute: String) : this(servedRoute) {
datasource = {
it.staticRootFolder = rootFolder
}
}
constructor(resourceFolder: ResourceFolder, servedRoute: String) : this(servedRoute) {
datasource = {
it.resources(resourceFolder.resourceFolder)
}
}
override fun appServerConfigurator(routeHandler: Routing) {
routeHandler.static(servedRoute) {
install(CachingHeaders) {
/*
TODO: Ideally the asset path would contain a hash of the file content
TODO: so that we can set a very long cache time (> 1 year). For now it defaults
TODO: to one hour.
*/
options { call, content ->
CachingOptions(CacheControl.MaxAge(maxAgeSeconds = maxCacheAgeSeconds))
}
}
datasource(this)
files(".")
}
}
}
data class ResourceFolder(val resourceFolder: String)