2017-08-08 1 views
2

私はかなりakkaに新しいです。私は私たちのサーバーに置く必要があるhtml、CSS、jvテンプレートを持っています。私はsrc/abc/css下にあるCSS、JavaScriptと画像ファイルへのパスを定義するにはどうすればよい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) 
} 

src/abc/jv; src/abc/images

私は接頭辞を使用していくつかのコードを見ましたが、それを適切に使用することはまだできませんでした。 また、複数の画像がありますが、それらをすべて宣言する必要がありますか?ありがとう!

答えて

1

abcディレクトリをsrc/main/resourcesに移動し、このようにします。これは完全な作業例です:

localhost:8000/helloを訪問
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) 
    } 
} 

index.htmlページにリダイレクトされますとabcディレクトリ内の資産は、そのページに含めることができます。

+0

実際には動作しません。使用するインポートと完全なスクリプトを書き留めてもよろしいですか?あなたは – uniXVanXcel

+0

@uniXVanXcel更新されました。 –

+0

ありがとうございました:) – uniXVanXcel

関連する問題