Ktor: Static files: Serve "/semantic-name" instead of "/semantic-name.html"

Created on 24 Oct 2017  路  6Comments  路  Source: ktorio/ktor

I want Ktor to drive my website. I have a bunch of HTML files but I want to strip the HTML suffix from those files when they are being served as the HTML suffix is just a technical detail that I want to strip.

feature

Most helpful comment

There is no built-in functionality like this yet, but it's quite easy to achieve by copying part of static source code (as a workaround):

fun Route.htmlFiles(folder: File) {
    val dir = staticRootFolder.combine(folder)
    get("{$pathParameterName...}") {
        val relativePath = call.parameters.getAll(pathParameterName)?.joinToString(File.separator) ?: return@get
        val file = dir.combineSafe(relativePath) + ".html" // added this
        if (file.isFile) {
            call.respond(LocalFileContent(file))
        }
    }
}

In general that is interesting request and we might need to introduce some kind of "static files filters" that can locate files with some "map" function (e.g. path -> path + ".html" like here), or even transform a file content while sending (e.g. markdown -> html)

All 6 comments

There is no built-in functionality like this yet, but it's quite easy to achieve by copying part of static source code (as a workaround):

fun Route.htmlFiles(folder: File) {
    val dir = staticRootFolder.combine(folder)
    get("{$pathParameterName...}") {
        val relativePath = call.parameters.getAll(pathParameterName)?.joinToString(File.separator) ?: return@get
        val file = dir.combineSafe(relativePath) + ".html" // added this
        if (file.isFile) {
            call.respond(LocalFileContent(file))
        }
    }
}

In general that is interesting request and we might need to introduce some kind of "static files filters" that can locate files with some "map" function (e.g. path -> path + ".html" like here), or even transform a file content while sending (e.g. markdown -> html)

I am using ktor 0.4.0 and got this
compile-error

...the compile errors are all due to "private in file". This is the file https://github.com/loxal/muctool/blob/master/service/src/main/kotlin/net/loxal/muctool/App.kt where I tried to incorporate your snippet.

I see. You might need to copy some more code :) We will think about this feature as an out-of-the-box later.

This also could be achieved if we implement something similar to mod_rewrite. Unfortunately it's not easy to implement.

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Just wanted to revisit this case after a few years. I there a solution available? I tried to achieve it using Spring Boot filters w/o Ktor but it requires a dee response rewrite. Unfortunately there is not setLocationHeader solution available.

Using Nginx' mod_rewrite could be also a valid approach but then I'd need to split up static files and endpoint logic into different sub-projects which would introduce additional management overhead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

guenhter picture guenhter  路  4Comments

baruchn picture baruchn  路  3Comments

scorsi picture scorsi  路  4Comments

diaodou picture diaodou  路  3Comments

r4zzz4k picture r4zzz4k  路  4Comments