package server
Provides HTTP server implementation.
Building HTTP Server
To build a server, you begin with ServerApplication
. This is a mutable
structure to which you apply changes to configure the server. Once the desired
settings are applied, you invoke one of several methods to create the server.
import java.io.File import scamper.BodyParsers import scamper.Implicits.{ stringToEntity, inputStreamToEntity } import scamper.ResponseStatuses.{ NotFound, Ok } import scamper.headers.TransferEncoding import scamper.server.HttpServer import scamper.server.Implicits.ServerHttpRequestType import scamper.types.Implicits.stringToTransferCoding // Get server application val app = HttpServer.app() // Add request handler to log all requests app.request { req => println(req.startLine) req } // Add request handler to specific request method and path app.get("/about") { req => Ok("This server is powered by Scamper.") } // Add request handler using path parameter app.put("/data/:id") { req => def update(id: Int, data: String): Boolean = ??? implicit val parser = BodyParsers.text() // Get path parameter val id = req.params.getInt("id") update(id, req.as[String]) match { case true => Ok() case false => NotFound() } } // Serve static files app.files("/main", new File("/path/to/public")) // Tell server to compress response app.response { res => res.withTransferEncoding("gzip", "chunked") } // Create server val server = app.create(8080) printf("Host: %s%n", server.host) printf("Port: %d%n", server.port) Thread.sleep(60 * 1000) // Close server when done server.close()
- Alphabetic
- By Inheritance
- server
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- trait ErrorHandler extends AnyRef
Provides utility for handling error when servicing request.
- trait HttpServer extends AnyRef
Provides handle to server instance.
Provides handle to server instance.
- See also
- case class ParameterNotConvertible(name: String, value: String) extends HttpException with Product with Serializable
Indicates parameter cannot be converted.
- case class ParameterNotFound(name: String) extends HttpException with Product with Serializable
Indicates parameter is not found.
- trait RequestFilter extends RequestHandler
Provides utility for filtering incoming request.
- trait RequestHandler extends AnyRef
Provides utility for handling incoming request.
- trait RequestParameters extends AnyRef
Provides access to server-side request parameters.
- trait RequestProcessor extends RequestHandler
Provides utility for processing incoming request.
- case class ResponseAborted(message: String) extends HttpException with Product with Serializable
Indicates response was aborted.
Indicates response was aborted.
A
RequestHandler
throwsResponseAborted
if no response should be sent for the handled request. - trait ResponseFilter extends AnyRef
Provides utility for filtering outgoing response.
- trait Router extends AnyRef
Configures routing for
ServerApplication
.Configures routing for
ServerApplication
.Router
works in much the same way as ServerApplication, except it is configured for request handling only, and all router paths are relative to a mount point defined in the owner application.import scamper.Implicits.stringToEntity import scamper.ResponseStatuses.{ NotFound, Ok } import scamper.server.HttpServer import scamper.server.Implicits.ServerHttpRequestType val app = HttpServer.app() // Mount point of router is /api app.use("/api") { router => val messages = Map(1 -> "Hello, world!", 2 -> "Goodbye, cruel world!") // Will be mapped to /api/messages/:id router.get("/messages/:id") { req => val id = req.params.getInt("id") messages.get(id) .map(Ok(_)) .getOrElse(NotFound()) } }
- See also
- class ServerApplication extends AnyRef
Configures and creates
HttpServer
.Configures and creates
HttpServer
.ServerApplication
is a mutable structure. With each applied change, the application is modified and returned. Changes applied after creating a server are not effected in the server.
Value Members
- object HttpServer
Provides factory methods for creating
HttpServer
. - object Implicits
Includes server-side type classes.
- object RequestHandler
Provides
RequestHandler
utilities. - object ResponseFilter
Provides
ResponseFilter
utilities. - object ServerApplication
Provides factory for
ServerApplication
.