2017-08-08 29 views
2

我對akka很新穎。我有一個html,css,jv模板,我需要把它放到我們的服務器上。在akka中定義額外路徑

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 javascript和圖像文件的路徑,它們在src/abc/css下; src/abc/jv; src/abc/images

我已經看到一些代碼使用前綴,但還沒有能夠正確使用它。 此外,有多個圖像,我應該宣佈他們全部?謝謝!

回答

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目錄內的資產可以被包含在該網頁。

+0

實際上這不起作用,你介意寫出你使用的進口和完整的腳本嗎? thnk you – uniXVanXcel

+0

@uniXVanXcel更新。 –

+0

好的,謝謝:) – uniXVanXcel