代码之家  ›  专栏  ›  技术社区  ›  Hasan A Yousef Michael Benjamin

将路由拆分为多个文件

  •  5
  • Hasan A Yousef Michael Benjamin  · 技术社区  · 7 年前

    我是新手 Kotlin 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("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()
    }
    
    2 回复  |  直到 7 年前
        1
  •  11
  •   Hasan A Yousef Michael Benjamin    7 年前

    最后我想起来了:

    为所需的函数名安装路由,例如:

    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) 
            }
    } 
    
        2
  •  0
  •   Cesar Chavez    4 年前

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

    最重要的千吨

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

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

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