2017-09-24 112 views
0

我是012,和Ktor的新手,下面的代碼與我一起正常工作,現在我需要添加更多Routes如何在多個文件中拆分路線?將路線拆分爲多個文件

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("my_alias[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("[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) 
     } 
}