将路线分成多个文件

哈桑·优素福

我是新来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("[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()
}
哈桑·优素福

终于我弄清楚了:

为所需的功能名称安装路由,例如:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章