2017-09-24 11 views
0

私はKotlinKtorに新しくなっています。以下は起動時にうまく動作しています。これ以上追加する必要があります。複数のファイルでルートを分割するにはどうすればよいですか? 、あなたが必要とする名前ルートを複数のファイルに分割する

は、機能のルーティングをインストールするように:

package blog 

import org.jetbrains.ktor.netty.* 
import org.jetbrains.ktor.routing.* 
import org.jetbrains.ktor.application.* 
import org.jetbrains.ktor.features.* 
import org.jetbrains.ktor.host.* 
import org.jetbrains.ktor.http.* 
import org.jetbrains.ktor.response.* 
import org.jetbrains.ktor.request.*  // for recieve 
import org.jetbrains.ktor.util.*  // for ValuesMap 

import org.apache.commons.mail.* 

fun Application.module() { 
    install(DefaultHeaders) 
    install(CallLogging) 
    install(Routing) { 
     get("/") { 
      call.respondText(""" 
      My Example Blog2 
       <form action="/contact-us" method="post"> 
        <input name="subject" placeholder="Subject"> 
        <br> 
        <textarea name="message" placeholder="Your message ..."></textarea> 
        <br> 
        <button>Submit</button> 
       </form> 
      """, ContentType.Text.Html) 
     } 
     post("/contact-us") { 
      val post = call.receive<ValuesMap>() 
      SimpleEmail().apply { 
       setHostName("smtp.gmail.com") 
       setSmtpPort(465) 
       setAuthenticator(DefaultAuthenticator("[email protected]", "my_gmil_passoword")) 
       setSSLOnConnect(true) 
       setFrom("[email protected]") 
       setSubject(post["subject"])   
       setMsg(post["message"])    
       addTo("[email protected]") 
      }.send() // will throw email-exception if something is wrong 
      call.respondRedirect("/contact-us/success") 
     } 
     get("/contact-us/success") { 
      call.respondText("Your message was sent", ContentType.Text.Html) 
     } 
    } 
} 

fun main(args: Array<String>) { 
    embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start() 
} 

答えて

1

は最後に、私はそれを考え出した私の例のためのよう

install(Routing) { 
     contact() 
} 

、要件を処理するためにfun Route.contact(){ ..}のような関数を作成します。私は以下を作成しました:

fun Route.contact(){ 
     get("/") { 
      call.respondText(""" 
      My Example Blog 12 
       <form action="/contact-us" method="post"> 
        <input name="subject" placeholder="Subject"> 
        <br> 
        <textarea name="message" placeholder="Your message ..."></textarea> 
        <br> 
        <button>Submit</button> 
       </form> 
      """, ContentType.Text.Html) 
     } 
     post("/contact-us") { 
      val post = call.receive<ValuesMap>() // val userId = registration["userId"] 
      SimpleEmail().apply { 
       setHostName("smtp.gmail.com") 
       setSmtpPort(465) 
       setAuthenticator(DefaultAuthenticator("[email protected]", "my_gmil_passoword")) 
       setSSLOnConnect(true) 
       setFrom("[email protected]") 
       setSubject(post["subject"])   
       setMsg(post["message"])    
       addTo("my_alia[email protected]") 
      }.send() // will throw email-exception if something is wrong 
      call.respondRedirect("/contact-us/success") 
     } 
     get"/contact-us/success") { 
      call.respondText("Your message was sent", ContentType.Text.Html) 
     } 
} 
関連する問題