在akka中定义额外路径

时间:2017-08-08 06:41:34

标签: javascript html css akka

我对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)
}

如何定义CSS {j}和图像文件的路径,这些路径位于src/abc/css下; src/abc/jv; src/abc/images

我见过一些使用前缀的代码,但还没有正确使用它。 此外,有多个图像,我应该声明它们吗?谢谢!

1 个答案:

答案 0 :(得分:1)

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目录中的资源可以包含在该页面中。