【问题标题】:Splitting Routes into multiple files将路由拆分为多个文件
【发布时间】:2018-03-05 12:38:45
【问题描述】:

我是 KotlinKtor 的新手,作为启动,我在下面的工作正常,现在我需要添加更多 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("my_alias@gmail.com", "my_gmil_passoword"))
                setSSLOnConnect(true)
                setFrom("my_alias@gmail.com")
                setSubject(post["subject"])        
                setMsg(post["message"])            
                addTo("my_alias@gmail.com")
            }.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()
}

【问题讨论】:

    标签: kotlin ktor


    【解决方案1】:

    你可以创建2个文件,例如:

    Principal.kt

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

    还有Contact.kt

    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.moduleContact() {
        routing{
            post("/contact-us") {
                val post = call.receive<ValuesMap>() 
                SimpleEmail().apply {
                    setHostName("smtp.gmail.com")
                    setSmtpPort(465)
                    setAuthenticator(DefaultAuthenticator("my_alias@gmail.com", "my_gmil_passoword"))
                    setSSLOnConnect(true)
                    setFrom("my_alias@gmail.com")
                    setSubject(post["subject"])        
                    setMsg(post["message"])            
                    addTo("my_alias@gmail.com")
                }.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) 
            }
        }
    }
    

    并修改application.conf

    ktor {
        deployment {
            port = 8080
            port = ${?PORT}
        }
        application {
            modules = [ com.ejemplo.blog.PrincipalKt.module,
                        com.ejemplo.blog.ContactKt.moduleContact
            ]
        }
    }
    

    【讨论】:

      【解决方案2】:

      终于想通了:

      为你需要的函数名安装路由,比如:

      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("my_alias@gmail.com", "my_gmil_passoword"))
                      setSSLOnConnect(true)
                      setFrom("my_alias@gmail.com")
                      setSubject(post["subject"])        
                      setMsg(post["message"])            
                      addTo("my_alias@gmail.com")
                  }.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) 
              }
      } 
      

      【讨论】:

      • 谢谢,奇怪的是在 ktor 的文档中没有一个这样的例子
      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 2017-08-15
      • 1970-01-01
      • 2018-06-24
      • 2020-11-09
      • 2020-08-04
      • 2013-08-24
      • 1970-01-01
      相关资源
      最近更新 更多