Router
Defines router for request handling.
import scala.language.implicitConversions
import scamper.http.ResponseStatus.Registry.{ BadRequest, NotFound, Ok }
import scamper.http.server.{ ParameterNotConvertible, ServerApplication, ServerHttpRequest }
import scamper.http.stringToEntity
val app = ServerApplication()
// Mount router to /api
app.route("/api") { router =>
val messages = Map(1 -> "Hello, world!", 2 -> "Goodbye, cruel world!")
// Map handler to /api/messages
router.get("/messages") { req =>
Ok(messages.mkString("\r\n"))
}
// Map handler to /api/messages/:id
router.get("/messages/:id") { req =>
val id = req.params.getInt("id")
messages.get(id)
.map(Ok(_))
.getOrElse(NotFound())
}
router.recover { req =>
{ case _: ParameterNotConvertible => BadRequest(req.target.toString) }
}
}
- See also
Value members
Abstract methods
Adds supplied request handler.
Adds supplied request handler.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- Returns
this router
Adds supplied handler for requests with given router path and any of specified request methods.
Adds supplied handler for requests with given router path and any of specified request methods.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- methods
request methods
- path
router path
- Returns
this router
- Note
If no request methods are specified, then matches are limited to path only.
Adds supplied response filter.
Adds supplied response filter.
The filter is appended to existing response filter chain.
- Value Params
- filter
response filter
- Returns
this router
Concrete methods
Adds supplied handler for DELETE requests to given router path.
Adds supplied handler for DELETE requests to given router path.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- path
router path
- Returns
this router
Mounts file server at given path.
Mounts file server at given path.
At request time, the mount path is stripped from the router path, and the remaining path is used to locate a file in the source directory.
File Mapping Examples
Mount Path | Source Directory | Router Path | Maps to |
---|---|---|---|
/images | /tmp | /images/logo.png | /tmp/logo.png |
/images | /tmp | /images/icons/warning.png | /tmp/icons/warning.png |
- Value Params
- defaults
default file names used when request matches directory
- path
router path at which directory is mounted
- source
directory from which files are served
- Returns
this router
- Note
If a request matches a directory, and if a file with one of the default file names exists in that directory, the server sends 303 (See Other) with a Location header value set to path of default file.
Adds supplied handler for GET requests to given router path.
Adds supplied handler for GET requests to given router path.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- path
router path
- Returns
this router
Adds supplied handler for POST requests to given router path.
Adds supplied handler for POST requests to given router path.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- path
router path
- Returns
this router
Adds supplied handler for PUT requests to given router path.
Adds supplied handler for PUT requests to given router path.
The handler is appended to existing request handler chain.
- Value Params
- handler
request handler
- path
router path
- Returns
this router
Mounts routing application at given path.
Mounts routing application at given path.
- Value Params
- app
routing application
- path
router path at which application is mounted
- Returns
this router
Expands supplied router path to its absolute path.
Expands supplied router path to its absolute path.
- Value Params
- path
router path
- Throws
- java.lang.IllegalArgumentException
if router path is not
*
and does not begin with/
or if it escapes mount path
- Note
If
*
is supplied as router path, its absolute path is also*
.