HTTP::Server
class HTTP::Server
Overview
An HTTP server.
A server is given a handler that receives an HTTP::Server::Context
that holds the HTTP::Request
to process and must in turn configure and write to an HTTP::Server::Response
.
The HTTP::Server::Response
object has status
and headers
properties that can be configured before writing the response body. Once response output is written, changing the status
and headers
properties has no effect.
The HTTP::Server::Response
is also a write-only IO
, so all IO
methods are available in it.
The handler given to a server can simply be a block that receives an HTTP::Server::Context
, or it can be an HTTP::Handler
. An HTTP::Handler
has an optional next
handler, so handlers can be chained. For example, an initial handler may handle exceptions in a subsequent handler and return a 500 status code (see HTTP::ErrorHandler
), the next handler might log the incoming request (see HTTP::LogHandler
), and the final handler deals with routing and application logic.
Simple Setup
A handler is given with a block.
require "http/server" server = HTTP::Server.new(8080) do |context| context.response.content_type = "text/plain" context.response.print "Hello world!" end puts "Listening on http://127.0.0.1:8080" server.listen
With non-localhost bind address
require "http/server" server = HTTP::Server.new("0.0.0.0", 8080) do |context| context.response.content_type = "text/plain" context.response.print "Hello world!" end puts "Listening on http://0.0.0.0:8080" server.listen
Add handlers
A series of handlers are chained.
require "http/server" HTTP::Server.new("127.0.0.1", 8080, [ HTTP::ErrorHandler.new, HTTP::LogHandler.new, HTTP::CompressHandler.new, HTTP::StaticFileHandler.new("."), ]).listen
Add handlers and block
A series of handlers is chained, the last one being the given block.
require "http/server" server = HTTP::Server.new("0.0.0.0", 8080, [ HTTP::ErrorHandler.new, HTTP::LogHandler.new, ]) do |context| context.response.content_type = "text/plain" context.response.print "Hello world!" end server.listen
Defined in:
http/server/context.crhttp/server/response.cr
http/server.cr
Class Method Summary
- .build_middleware(handlers, last_handler : Context -> ? = nil)
Builds all handlers as the middleware for
HTTP::Server
. - .new(host : String, port : Int32, handlers : Array(HTTP::Handler), &handler : Context -> )
- .new(port, handlers : Array(HTTP::Handler), &handler : Context -> )
- .new(host : String, port : Int32, &handler : Context -> )
- .new(port, &handler : Context -> )
- .new(host : String, port : Int32, handlers : Array(HTTP::Handler))
- .new(host : String, port : Int32, handler : HTTP::Handler | HTTP::Handler::Proc)
- .new(port, handlers : Array(HTTP::Handler))
- .new(port, handler)
Instance Method Summary
- #bind(reuse_port = false)
Creates the underlying
TCPServer
if the doesn't already exist. - #close
Gracefully terminates the server.
- #listen(reuse_port = false)
Starts the server.
- #port
Returns the TCP port the server is connected to.
- #tls : OpenSSL::SSL::Context::Server?
- #tls=(tls : OpenSSL::SSL::Context::Server?)
Instance methods inherited from class Reference
==(other : self)==(other) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s
Class methods inherited from class Reference
new new Instance methods inherited from class Object
!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
from_json(string_or_io, root : String) : selffrom_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml
Class Method Detail
def self.build_middleware(handlers, last_handler : Context -> ? = nil)Source
Builds all handlers as the middleware for HTTP::Server
.
def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler), &handler : Context -> )Source
def self.new(port, handlers : Array(HTTP::Handler), &handler : Context -> )Source
def self.new(host : String, port : Int32, handlers : Array(HTTP::Handler))Source
def self.new(host : String, port : Int32, handler : HTTP::Handler | HTTP::Handler::Proc)Source
def self.new(port, handlers : Array(HTTP::Handler))Source
def self.new(port, handler)Source
Instance Method Detail
def bind(reuse_port = false)Source
Creates the underlying TCPServer
if the doesn't already exist.
You may set reuse_port to true to enable the SO_REUSEPORT
socket option, which allows multiple processes to bind to the same port.
def closeSource
Gracefully terminates the server. It will process currently accepted requests, but it won't accept new connections.
def listen(reuse_port = false)Source
Starts the server. Blocks until the server is closed.
See #bind
for details on the reuse_port argument.
def portSource
Returns the TCP port the server is connected to.
For example you may let the system choose a port, then report it:
server = HTTP::Server.new(0) { } server.bind server.port # => 12345
def tls : OpenSSL::SSL::Context::Server?
def tls=(tls : OpenSSL::SSL::Context::Server?)
© 2012–2017 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.22.0/HTTP/Server.html