Skip to content

Instantly share code, notes, and snippets.

@praveenKajla
Last active November 16, 2019 20:59
Show Gist options
  • Select an option

  • Save praveenKajla/636cce1bfabe16dc6f4ae0928f06b77c to your computer and use it in GitHub Desktop.

Select an option

Save praveenKajla/636cce1bfabe16dc6f4ae0928f06b77c to your computer and use it in GitHub Desktop.
package readinglist
class Request(val method:String,val query:String,val contentType:String)
class Response(val contents:String,var status:Status){
fun status(status:Status.() -> Unit){
//do process
}
}
class Status(var code:Int,var description:String)
class RouteHandler(val request:Request,val response: Response){
var executeNext = false
fun next(){
executeNext=true
}
}
fun response(response:Response.() -> Unit){}
fun routeHandler(path:String,f:RouteHandler.() -> Unit):RouteHandler.() -> Unit = f
fun main(args: Array<String>) {
routeHandler("/index.html"){
if(request.query!=""){
//do process
}
response {
status {
code=404
description="Not Found"
}
}
}
}
package readinglist
//using invoke to invoke some functionality just by using instance
class Request(val method:String,val query:String,val contentType:String)
class Response(val contents:String,var status:Status){
operator fun invoke(status: Status.() -> Unit) {}
}
class Status(var code:Int,var description:String)
class RouteHandler(val request:Request,val response: Response){
var executeNext = false
fun next(){
executeNext=true
}
}
fun routeHandler(path:String,f:RouteHandler.() -> Unit):RouteHandler.() -> Unit = f
fun main(args: Array<String>) {
routeHandler("/index.html"){
if(request.query!=""){
//do process
}
response {
code=404
description="Not Found"
}
}
}
}
@bschandramohan

Copy link
Copy Markdown

Praveen, some great examples. Running main here doesn't return any data. How do you print the response here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment