Last active
November 16, 2019 20:59
-
-
Save praveenKajla/636cce1bfabe16dc6f4ae0928f06b77c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Praveen, some great examples. Running main here doesn't return any data. How do you print the response here?