Skip to content

Instantly share code, notes, and snippets.

@JakubOboza
Created June 7, 2014 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakubOboza/7399fd0c8c1002255674 to your computer and use it in GitHub Desktop.
Save JakubOboza/7399fd0c8c1002255674 to your computer and use it in GitHub Desktop.

Sinatra like Web Framework for Scala

I tried few solutions like Scalatra, Play and for my POV Scalatra was a bit too bloated and big, didn't feel like sinatra at all. Play is more like Rails so you get everything. But spark is simple and easy to udnerstand. I didn't see any Scala example so i made one.

Sadly there are few things i don't like :(

If we would build DSL in a different way eg. we could have it like this:

  get(Route("/hello"){
    response.body("Hello World")
  })

or

  get(Route("/hello"){
    return "Hello World"
  })

That would make it super nice to use.

import spark.Spark._
import spark.Request
import spark.Response
import spark.Route
// this is on 0.9 not on 2.0 with new support, maybe some can be simplified even more :)
// more on spark options: https://code.google.com/p/spark-java/
object HelloWorld {
def main(args: Array[String]) {
get(new Route("/hello") {
def handle(request: Request , response: Response) : Object = {
response.status(401)
return "Hello World! Srsl? " + request.body()
}
})
get(new Route("/unauth"){
def handle(request: Request, response: Response) : Object = {
response.status(401)
return "Sorry mate"
}
})
get(new Route("/user/:name"){
def handle(request: Request, response: Response) : Object = {
return "Selected user: " + request.params(":name");
}
})
get(new Route("/redirect"){
def handle(request: Request, response: Response) : Object = {
response.redirect("/user/kuba")
return ""
}
})
get(new Route("/params"){
def handle(request: Request, response: Response) : Object = {
val name = request.queryParams("name")
val params = request.queryParams()
return name + params
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment