Introduction
Wasabi is a sinatra inspired webframework for kotlin.
Or according to the author.
An HTTP Framework built with Kotlin for the JVM.
Wasabi combines the conciseness and expressiveness of Kotlin, the power of Netty and the simplicity of Express.js (and other Sinatra-inspired web frameworks) to provide an easy to use HTTP framework
Wasabi has no viewengine. You should use a clientside MVC-framework like Angularjs or ember.js.
So on to the code.
Installation
There is a gradle file that should import everything for you. But I’m stupid and couldn’t get it to work. So I just cloned the repo and then added that as a module to my intellij project.
Hello world
This is the absolute minimum code I have found to get wasabi to show me anything. No matter what the read.me says.
package be.baes
import org.wasabi.app.AppServer
fun main(args: Array<String>) {
val server = AppServer()
server.get("/", { response.send ("Hello world.") })
server.start()
}
You should now see this message in the run window of your intellij project.
[main] INFO org.wasabi.app.AppServer – Server starting on port 3000
We can now assume that the url will be http:\localhost:3000
And yes, yes it does.
You should also see these mentions in your run window in intellij.
[nioEventLoopGroup-2-1] INFO org.wasabi.interceptors.LoggingInterceptor – [GET] – /
[nioEventLoopGroup-2-2] INFO org.wasabi.interceptors.LoggingInterceptor – [GET] – /favicon.ico
So how do get it to output to another port?
Easy, we give the AppServer object a configuration object.
val config = AppConfiguration(4000, "This is me on port 4000", true)
val server = AppServer(config)
```
Clearly the first parameter is the port, the second a message when connecting and the third is the option to turn off logging.
Now let’s try and return an object.
```java
package be.baes
import org.wasabi.app.AppServer
import org.wasabi.app.AppConfiguration
import org.wasabi.interceptors.parseContentNegotiationHeaders
import org.wasabi.interceptors.negotiateContent
fun main(args: Array<String>) {
val config = AppConfiguration(4000, "This is me on port 4000", true)
val server = AppServer(config)
var person = Person()
person.firstName = "test"
person.lastName = "test2"
server.get("/", { response.send (person) })
server.start()
}
class Person()
{
public var lastName : String = ""
public var firstName : String = ""
}
And when we try the above with postman we get….
NOTHING.
So I must be missing something.
package be.baes
import org.wasabi.app.AppServer
import org.wasabi.app.AppConfiguration
import org.wasabi.interceptors.parseContentNegotiationHeaders
import org.wasabi.interceptors.negotiateContent
fun main(args: Array<String>) {
val config = AppConfiguration(4000, "This is me on port 4000", true)
val server = AppServer(config)
server.negotiateContent()
var person = Person()
person.firstName = "test"
person.lastName = "test2"
server.get("/", { response.send (person) })
server.start()
}
class Person()
{
public var lastName : String = ""
public var firstName : String = ""
}
I need to tell it to negotiateContent. I kinda think that should be by default.
And now we get.
Wasabi only supports json for now. No XML, no HTML.
Update
Considering the comment by Hadi, the more concise form would then look like.
package be.baes
import org.wasabi.app.AppServer
import org.wasabi.app.AppConfiguration
import org.wasabi.interceptors.parseContentNegotiationHeaders
import org.wasabi.interceptors.negotiateContent
fun main(args: Array<String>) {
val config = AppConfiguration(4000, "This is me on port 4000", true)
val server = AppServer(config)
server.negotiateContent()
var person = Person("test","test2")
server.get("/", { response.send (person) })
server.start()
}
class Person(var firstName: String, var lastName: String)
Conclusion
It needs some more work but it works and isn’t to hard to get to work. Go ahead and play with it.