Saturday, 25 June 2016

Kotlin + Maven + Spark

I have created a demo app, with Kotlin and Spark using maven as the build tool. Check it out at
https://github.com/rizvn/Kotlin-Web-App.

Mixing Kotlin with Spark really allows for some concise yet type safe code. Below is an example of routing in the app.

import app.Conf.view
import spark.Route
import spark.Spark.*
import java.util.*


fun main(args: Array<String>){
    port(8080)
    staticFileLocation("/static")

    //Routes
    get("/",        index)
    get("/about",   about)
    get("/contact", contact)
}


//controllers
val index   = Route { req, res -> view("index")  }
val about   = Route { req, res -> view("about") }


val contact = Route { req, res ->
   val map = HashMap<String, Any>()
   map["telNo"] = "0123456";
   view("contact", model = map)
}

No comments:

Post a Comment