Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/perwendel/spark-kotlin
A Spark DSL in idiomatic kotlin // dependency: com.sparkjava:spark-kotlin:1.0.0-alpha
https://github.com/perwendel/spark-kotlin
Last synced: 9 days ago
JSON representation
A Spark DSL in idiomatic kotlin // dependency: com.sparkjava:spark-kotlin:1.0.0-alpha
- Host: GitHub
- URL: https://github.com/perwendel/spark-kotlin
- Owner: perwendel
- License: apache-2.0
- Created: 2017-05-24T08:39:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-03-14T17:07:14.000Z (over 4 years ago)
- Last Synced: 2024-10-14T13:46:10.843Z (25 days ago)
- Language: Kotlin
- Homepage:
- Size: 115 KB
- Stars: 987
- Watchers: 35
- Forks: 43
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-kotlin - spark-kotlin - A Spark DSL in idiomatic kotlin // dependency: com.sparkjava:spark-kotlin:1.0.0-alpha (Libraries)
- awesome-ccamel - perwendel/spark-kotlin - A Spark DSL in idiomatic kotlin // dependency: com.sparkjava:spark-kotlin:1.0.0-alpha (Kotlin)
README
# spark-kotlin
A Spark DSL in idiomatic kotlin.
Authors:
--------
- Per Wendel, @perwendel
- Love Löfdahl, @lallemuppDependency:
-----------
Maven:
```xmlcom.sparkjava
spark-kotlin
1.0.0-alpha```
Gradle:
```groovy
compile "com.sparkjava:spark-kotlin:1.0.0-alpha"
```Documentation
-------------Routes
------```kotlin
// Static API
get("/hello") {
"Hello Spark Kotlin"
}get("/doc") {
// available (same in instance API)
params()
params("")
splat()
status()
status(404)
queryParams("")
queryMap()
queryMap("")
attribute("")
attribute("", "")
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
------------------
```kotlin
// 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
------------
```kotlin
// 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)
------------------------------
```kotlin
// 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
```