Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (80 sloc) 2.99 KB
import com.google.gson.Gson
import io.vertx.core.AsyncResult
import io.vertx.core.Future
import io.vertx.core.Vertx
import io.vertx.core.json.Json
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
import io.vertx.ext.web.handler.BodyHandler
import java.util.*
import kotlin.reflect.KClass
/**
* Step02 - In memory REST User repository
*/
object Vertx3KotlinRestJdbcTutorial {
val gson = Gson()
@JvmStatic fun main(args: Array<String>) {
val port = 9000
val vertx = Vertx.vertx()
val server = vertx.createHttpServer()
val router = Router.router(vertx)
router.route().handler(BodyHandler.create()) // Required for RoutingContext.bodyAsString
val userService = MemoryUserService()
router.get("/:userId").handler { ctx ->
val userId = ctx.request().getParam("userId")
jsonResponse(ctx, userService.getUser(userId))
}
router.post("/").handler { ctx ->
val user = jsonRequest<User>(ctx, User::class)
jsonResponse(ctx, userService.addUser(user))
}
router.delete("/:userId").handler { ctx ->
val userId = ctx.request().getParam("userId")
jsonResponse(ctx, userService.remUser(userId))
}
server.requestHandler { router.accept(it) }.listen(port) {
if (it.succeeded()) println("Server listening at $port")
else println(it.cause())
}
}
fun jsonRequest<T>(ctx: RoutingContext, clazz: KClass<out Any>): T =
gson.fromJson(ctx.bodyAsString, clazz.java) as T
fun jsonResponse<T>(ctx: RoutingContext, future: Future<T>) {
future.setHandler {
if (it.succeeded()) {
val res = if (it.result() == null) "" else gson.toJson(it.result())
ctx.response().end(res)
} else {
ctx.response().setStatusCode(500).end(it.cause().toString())
}
}
}
}
//-----------------------------------------------------------------------------
// API
data class User(val id:String, val fname: String, val lname: String)
interface UserService {
fun getUser(id: String): Future<User>
fun addUser(user: User): Future<Unit>
fun remUser(id: String): Future<Unit>
}
//-----------------------------------------------------------------------------
// IMPLEMENTATION
class MemoryUserService(): UserService {
val _users = HashMap<String, User>()
init {
addUser(User("1", "user1_fname", "user1_lname"))
}
override fun getUser(id: String): Future<User> {
return if (_users.containsKey(id)) Future.succeededFuture(_users.getOrImplicitDefault(id))
else Future.failedFuture(IllegalArgumentException("Unknown user $id"))
}
override fun addUser(user: User): Future<Unit> {
_users.put(user.id, user)
return Future.succeededFuture()
}
override fun remUser(id: String): Future<Unit> {
_users.remove(id)
return Future.succeededFuture()
}
}