scamper.http.server
Type members
Classlikes
Defines utility for handling error during request processing.
Defines utility for handling error during request processing.
- Companion
- object
Defines handle to server instance.
Defines handle to server instance.
- See also
- Companion
- object
Defines managed service.
Defines managed service.
A managed service is started when the server is created; it is stopped when the server is shut down.
Defines noncritical service.
Defines noncritical service.
If a noncritical service fails during startup, it does not halt server creation.
Defines handler for incoming request.
Defines handler for incoming request.
- Companion
- object
Provides RequestHandler
utilities.
Provides RequestHandler
utilities.
- Companion
- class
Indicates response was aborted.
Indicates response was aborted.
A RequestHandler
throws ResponseAborted
if no response should be sent
for the request.
Defines filter for outgoing response.
Defines filter for outgoing response.
- Companion
- object
Provides ResponseFilter
utilities.
Provides ResponseFilter
utilities.
- Companion
- class
Defines router for request handling.
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
Defines server application for creating HttpServer
.
Defines server application for creating HttpServer
.
Default Configuration
The initial application is constructed with the following default configuration:
Key | Value |
---|---|
logger | scamper.logging.ConsoleLogger |
backlogSize | 50 |
poolSize | Runtime.getRuntime().availableProcessors() |
queueSize | Runtime.getRuntime().availableProcessors() * 4 |
bufferSize | 8192 |
readTimeout | 5000 |
headerLimit | 100 |
keepAlive | (Not configured) |
secure | (Not configured) |
manage | (Not configured) |
incoming | (Not configured) |
outgoing | (Not configured) |
recover | (Sends 500 Internal Server Error ) |
Building HTTP Server
ServerApplication
is a mutable structure. With each applied change, the
application is modified and returned. After the desired configuration is
applied, a server is created using a factory method.
import java.io.File
import scala.language.implicitConversions
import scamper.http.{ BodyParser, stringToEntity }
import scamper.http.ResponseStatus.Registry.{ NotFound, NoContent, Ok }
import scamper.http.server.{ *, given }
// Get server application
val app = ServerApplication()
// Add request handler to log all requests
app.incoming { req =>
println(req.startLine)
req
}
// Add request handler for GET requests at specified path
app.get("/about") { req =>
Ok("This server is powered by Scamper.")
}
// Add request handler for PUT requests at specified path
app.put("/data/:id") { req =>
def update(id: Int, data: String): Boolean = ???
given BodyParser[String] = BodyParser.string()
// Get path parameter
val id = req.params.getInt("id")
update(id, req.as[String]) match
case true => NoContent()
case false => NotFound()
}
// Serve static files from file directory
app.files("/main", 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)
try
printf("Host: %s%n", server.host)
printf("Port: %d%n", server.port)
// Run server for 60 seconds
Thread.sleep(60 * 1000)
finally
// Close server when done
server.close()
Indicates exception in managed service.
Indicates exception in managed service.
- Value Params
- cause
underlying cause
- message
detail message
- Constructor
Constructs exception with supplied message and cause.
Provides factory for upgrading request to WebSocket connection.
Provides factory for upgrading request to WebSocket connection.
import scamper.http.HttpRequest
import scamper.http.ResponseStatus.Registry.Unauthorized
import scamper.http.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()
}