• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

perwendel/spark-kotlin: A Spark DSL in idiomatic kotlin // dependency: com.spark ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

perwendel/spark-kotlin

开源软件地址(OpenSource Url):

https://github.com/perwendel/spark-kotlin

开源编程语言(OpenSource Language):

Kotlin 97.7%

开源软件介绍(OpenSource Introduction):

spark-kotlin

A Spark DSL in idiomatic kotlin.

Authors:

  • Per Wendel, @perwendel
  • Love Löfdahl, @lallemupp

Dependency:

Maven:

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-kotlin</artifactId>
    <version>1.0.0-alpha</version>
</dependency>

Gradle:

compile "com.sparkjava:spark-kotlin:1.0.0-alpha"

Documentation

Routes

// Static API
get("/hello") {
    "Hello Spark Kotlin"
}

get("/doc") {
    // available (same in instance API)
    params()
    params("<path-param-name>")
    splat()
    status()
    status(404)
    queryParams("<key>")
    queryMap()
    queryMap("<key>")
    attribute("<key>")
    attribute("<key>", "<value>")
    attributes()
    session()
    session(create = true)
    contentType()
    uri()
    protocol()
    scheme()
    host()
    port()
    pathInfo()
    servletPath()
    contextPath()
    userAgent()
    requestMethod()
    type()
    type(contentType = "application/json")
    redirect(location = "/to")
    redirect(location = "/to", statusCode = 302)
    
    // has all above and some more
    request 
    response
}

get("/nothing") {
    status(404)
    "Oops, we couldn't find what you're looking for"
}

get("/saymy/:name") {
    params(":name")
}

get("/redirect") {
    redirect("/hello")
}

// Instance API
val http = ignite()

http.get("/hello") {
    "Hello Spark Kotlin"
}

http.get("/nothing") {
    status(404)
    "Oops, we couldn't find what you're looking for"
}

http.get("/saymy/:name") {
    params(":name")
}

http.get("/redirect") {
    redirect("/hello")
}

Initialization DSL

// Static API
config {
    port = 5500
    ipAddress = "0.0.0.0"
    threadPool {
        maxThreads = 10
        minThreads = 5
        idleTimeoutMillis = 1000
    }
    secure {
        keystore {
            file = "/etc/secure/keystore"
            password = "hardtocrack"
        }
        truststore {
            file = "/etc/secure/truststore"
            password = "otherdifficultpassword"
        }
        needsClientCert = false
    }
    staticFiles {
        location = "/public"
        expiryTime = 36000.seconds
        headers(
                "description" to "static content",
                "licence" to "free to use"
        )
        mimeTypes(
                "cxt" to "text/html"
        )
    }
}

// Instance API
val http = ignite {
    port = 5500
    ipAddress = "0.0.0.0"
    threadPool {
        maxThreads = 10
        minThreads = 5
        idleTimeoutMillis = 1000
    }
    secure {
        keystore {
            file = "/etc/secure/keystore"
            password = "hardtocrack"
        }
        truststore {
            file = "/etc/secure/truststore"
            password = "otherdifficultpassword"
        }
        needsClientCert = false
    }
    staticFiles {
        location = "/public"
        expiryTime = 36000.seconds
        headers(
                "description" to "static content",
                "licence" to "free to use"
        )
        mimeTypes(
                "cxt" to "text/html"
        )
    }
}

Redirect DSL

// Static API
redirect {
    any(
            "/from" to "/hello",
            "/hi" to "/hello"
    )
    get(
            "/source" to "/target"
    )
    post(
            "/gone" to "/new"
    )
}

// Instance API
http.redirect {
    any(
            "/from" to "/hello",
            "/hi" to "/hello"
    )
    get(
            "/source" to "/target"
    )
    post(
            "/gone" to "/new"
    )
}


WebSocket DSL (design ongoing)

// Static API
webSocket("/echo") {
    opened {
        println("[Opened] remote address = " + session.remoteAddress)
    }
    received {
        println("[Received] message = $message")
    }
    closed {
        println("[Closed] code = $code, reason = $reason, session = $session")
    }
    error {
        println("[Error] cause = " + cause)
    }
}

// Instance API
http.webSocket("/echo") {
    opened {
        println("[Opened] remote address = " + session.remoteAddress)
    }
    received {
        println("[Received] message = $message")
    }
    closed {
        println("[Closed] code = $code, reason = $reason, session = $session")
    }
    error {
        println("[Error] cause = " + cause)
    }
}

class WebSocketSession
    val remote
    var idleTimeout
    val isOpen
    val localAddress
    val protocolVersion
    val upgradeResponse
    val upgradeRequest
    val policy
    val isSecure
    val remoteAddress
    
    fun disconnect()
    fun suspend(): Suspension 
    fun close()
    fun close(closeStatus: CloseStatus?)
    fun close(statusCode: Int, reason: String?)
    fun raw(): Session



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap