A simple, easy to use and lightweight kotlin web server for small quick projects
repositories {
maven {
name = "devOS"
url = uri("https://mvn.devos.one/releases")
}
}
dependencies {
implementation("cz.lukynka:lkws:1.2")
}
repositories {
mavenCentral()
maven {
name "devOS"
url "https://mvn.devos.one/releases"
}
}
dependencies {
implementation 'cz.lukynka:lkws:1.2'
}
You want to create new instance of LightweightWebServer
with port supplied as parameter (defaults to 7270)
val server = LightweightWebServer(port = 7270)
You can then listen to get calls on specific path by calling .get(path)
on the server
and providing lambda expression to execute when a request is received at the specified endpoint.
server.get("/uwu") { res ->
res.respond("owo :3")
}
you can use Response.URLParameters[param]
to get url parameter
server.put("/users/{USER}/settings") {
val user = it.URLParameters["USER"]
val updatedSettingsJson = it.requestBody
//handle stuff
it.respond("settings changed", 201)
}
You can use Response.requestCookies[cookie]
to get value of cookie. Will return null
if cookie was not found. To return error to user you can simply throw exception. Error page is customizable (Error Handling section below)
server.post("/projects/create") {
if(it.requestCookies["token"] != superSecretTokenTrustMe) throw Exception("nuh uh, you are not logged in")
val projectName = it.queryParameters["name"]
val redirectAfterCreated = it.queryParameters["redirect"].toBoolean()
//whatever here
if(redirectAfterCreated) {
it.respondRedirect("/projects/$projectName")
} else {
it.respond("project created", 201)
}
}
You can add Headers by adding Pair<string, string>
to the headers
variable of Response
server.get("/status") { res ->
res.headers.Add(Pair("Content-Type", "application/json"))
res.respondJson("{ 'status': 'operational' }")
}
You can also add middleware to all requests like this:
val token = "imdownbadforvonlycaon"
fun isAuth(response: Response): Boolean {
return response.requestHeaders["Token"] == token
}
server.get("/status", ::isAuth) { res ->
it.respondFile(File("src/test/imgs/lycaon.jpg"), 200, "image")
}
The server will automatically throw an exception if the auth function returns false
Additionally, this supports all request types (GET, POST, PUT, HEAD, PATCH etc.)
Listening to errors / invalid requests is pretty much the same as listening to for example GET
requests but with the difference of the thrown exception being included in the response object
server.error { res ->
res.respond("oopsie happened: ${res.exception}", 500)
}