我是 akka 的新手。我有一个 html、css、jv 模板,我需要将其上传到我们的服务器。
package com.example
import akka.http.scaladsl.marshallers.xml.ScalaXmlSupport.defaultNodeSeqMarshaller
import akka.http.scaladsl.server.{ HttpApp, Route }
/**
* Server will be started calling Server_HttpApp .startServer("localhost", 8080)`
* and it will be shutdown after pressing return.
*/
object Server_HttpApp extends HttpApp with App {
def routes: Route =
pathEndOrSingleSlash { // Listens to the top `/`
complete("Helloo") // Completes with some text
} ~
path("hello") { // Listens to paths that are exactly `/hello`
get { // Listens only to GET requests
//complete(<html><body><h1>Say hello to akka-http</h1></body></html>) // Completes with some text
getFromResource("src/abc/html/index.html")
} ~
getFromResourceDirectory("src")
}
startServer( "xyz" , 70)
}
如何定义 src/abc/css
下的 CSS javascript 和图像文件的路径; src/abc/jv
; 源代码/abc/图像
。
我见过一些使用前缀的代码,但还不能正确使用它。 另外,有多个图像,我应该全部申报吗?谢谢!
最佳答案
将 abc
目录移动到 src/main/resources
并像这样进行操作。这是一个完整的工作示例:
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Directives._
import akka.http.scaladsl.server.Route
import akka.stream.ActorMaterializer
object WebServerHttpApp {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
def routes: Route = getFromResourceDirectory("abc") ~ pathPrefix("hello") {
get {
redirect("index.html", StatusCodes.PermanentRedirect)
}
}
Http().bindAndHandle(routes, "localhost", 8000)
}
}
访问 localhost:8000/hello
将重定向到 index.html
页面,abc
目录中的 Assets 可以包含在该页面中。
关于javascript - 在 akka 中定义额外的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561044/