Я использую Vert.X (через org.jetbrains.kotlinx: vertx3-lang-kotlin library) с Kotlin, и я пытаюсь создать одностраничное приложение, которое само по себе - хранится в банке.Маршрутизация статического содержимого в VertX
С Maven стороны, это моя зависимость:
<properties>
...
<vertx.version>3.3.2</vertx.version>
<kotlin.version>1.0.3</kotlin.version>
<main.class>Bob</main.class>
</properties>
<!-- Vertx Dependencies -->
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>${vertx.version}</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>${vertx.version}</version>
</dependency>
<!-- Kotlin Dependencies -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
<!-- Kotlin Vertx Binding Dependency -->
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>vertx3-lang-kotlin</artifactId>
<version>[0.0.4,0.1.0)</version>
</dependency>
В моем главном классе, я получил следующее:
object Bob {
val log = LoggerFactory.getLogger(javaClass)
val port = 9999
@JvmStatic
fun main(args: Array<String>) {
DefaultVertx {
httpServer(port = Bob.port, block = Route {
GET("/") { request ->
headers().add("Author", "[email protected]")
contentType("text/html")
sendFile("html/index.html")
}
otherwise {
setStatus(404, "Resource not found")
body {
write("The requested resource was not found\n")
}
}
});
}
}
Перейти к localhost:9999
index.html успешно работает. теперь я хочу, чтобы иметь возможность запросить CSS/JS файлы и обслуживать их, а также
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="css/bootstrap.min.css" >
<link rel="stylesheet" href="css/bootstrap-theme.min.css" >
<script src="js/bootstrap.min.js"></script>
Для каждого из ресурсов, браузер возвращает ошибку 404, как ожидалось.
Я сейчас пытаюсь служить CSS, используя
GET("/css/*") {
request ->
println("Serving CSS")
contentType("text/css")
sendFile("css/${request.path()}")
}
Но я не вижу, что когда-либо входя в этот блок, и он продолжает служить 404-х ошибок.
Каков правильный способ обслуживания статических файлов в Vert.X с использованием org.jetbrains.kotlinx: vertx3-lang-kotlin библиотека?
Где я могу называть 'router.route', внутри' Route {...} '? Я постараюсь первым делом завтра утром. –
Я добавил полный пример в Котлин в своем первоначальном ответе. Наслаждайтесь! –