Router

trait 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
class Object
trait Matchable
class Any

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

def incoming(path: String, methods: RequestMethod*)(handler: RequestHandler): 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.

def mountPath: String

Gets mount path.

Gets mount path.

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

def recover(handler: ErrorHandler): Router

Adds error handler.

Adds error handler.

The handler is appended to existing error handler chain.

Value Params
handler

error handler

Returns

this router

Concrete methods

def delete(path: String)(handler: RequestHandler): Router

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

def files(path: String, source: File, defaults: String*): 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 PathSource DirectoryRouter PathMaps 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.

def get(path: String)(handler: RequestHandler): Router

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

def post(path: String)(handler: RequestHandler): 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

def put(path: String)(handler: RequestHandler): 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

def route(path: String)(app: RoutingApplication): 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

def toAbsolutePath(path: String): String

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 *.

def websocket(path: String)(app: WebSocketApplication[_]): Router

Mounts WebSocket application at given path.

Mounts WebSocket application at given path.

Value Params
app

WebSocket application

path

router path at which application is mounted

Returns

this router