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.BodyParser import scamper.Implicits.stringToEntity import scamper.ResponseStatus.Registry.{ NotFound, Ok } import scamper.server.HttpServer import scamper.server.Implicits._ // Get server application val app = HttpServer.app() // Add request handler to log all requests app.incoming { 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 = BodyParser.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")) // Gzip response body if not empty app.outgoing { res => res.body.isKnownEmpty match { case true => res case false => res.setGzipContentEncoding() } } // Create server val server = app.create(8080) printf("Host: %s%n", server.host) printf("Port: %d%n", server.port) // Run server for 60 seconds 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
PathParameters extends AnyRef
Provides access to path parameters.
-
trait
RequestHandler extends AnyRef
Provides utility for handling 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 request. -
trait
ResponseFilter extends AnyRef
Provides utility for filtering outgoing response.
-
trait
Router extends AnyRef
Used for routing request handlers.
Used for routing request handlers.
import scamper.Implicits.stringToEntity import scamper.ResponseStatus.Registry.{ NotFound, Ok } import scamper.server.HttpServer import scamper.server.Implicits.ServerHttpRequestType val app = HttpServer.app() // 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()) } }
- See also
-
class
ServerApplication extends Router
Configures and creates
HttpServer
.Configures and creates
HttpServer
.ServerApplication
is a mutable structure. With each applied change, the application is modified and returned. After the desired settings are applied, a server is created using one of several factory methods.
Value Members
-
object
HttpServer
Provides factory 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
. -
object
WebSocketUpgrade
Upgrades request to WebSocket connection.
Upgrades request to WebSocket connection.
import scamper.HttpRequest import scamper.ResponseStatus.Registry.Unauthorized import scamper.server.{ ServerApplication, WebSocketUpgrade } val app = ServerApplication() app.get("/chat/:roomId") { req => def authorize(req: HttpRequest): Boolean = ... authorize(req) match { case true => WebSocketUpgrade(req) { session => // Set up session ... session.open() } case false => Unauthorized() } }
Provided herein is API documentation for Scamper, the HTTP library for Scala.