Spaceboy.Conn (Spaceboy v0.1.1) View Source
Struct representing Spaceboy connection (request) roughly equivalent to Plug.Conn
This is main struct that will go through the whole request lifetime.
Request fields
These values are set by the framework and you are supposed to treat them as read-only
:scheme- is always:gemini(no other schemes are supported):host- will be set to host from request:port- listening port (default 1965):remote_ip- IP address of the client (or closest proxy):path_info- path segments:request_path- default path as received from client:query_string- query string as received from client:peer_cert- client certificate
Fetchabel fields
These fields are not populated until they are fetched manually.
:path_params- fetched bySpaceboy.Router:query_params- fetched byfetch_query_params/1:params- combined field of:path_paramsand:query_params
Those fields requires manual fetching because you don't always want to format e.g. query. If you
are using query for simple user input (e.g. username) and the query looks like &my_username you
actually don't want to fetch it and create params from it.
Response fields
You are supposed to set those values during request lifetime
:header- header struct for the response:body- response body or file path
Furthermore, the before_send field stores callbacks that are invoked before the connection is
sent.
Connection fields
:assigns- user data (currently unused):owner- process which owns the connection:halted- the boolean status on whether the pipeline was halted:state- the connection state
The connection state is used to track the connection lifecycle. It starts as :unset but is
changed to :set or :set_file when response is set. Its final result is :sent.
Link to this section Summary
Functions
Set client certificate required response
Fetch query params - decode query_string to map()
Add file as response.
Add text/gemini string as response
Set input response
Add map as JSON response
Set not found response
Set redirect response
Add function to be run righ before the response is actually send.
Add response header and potentially body to the function.
Link to this section Types
Specs
state() :: :unset | :set | :set_file | :sent
Specs
t() :: %Spaceboy.Conn{
assigns: map(),
before_send: [(t() -> t())],
body: String.t() | nil,
halted: boolean(),
header: Spaceboy.Header.t() | nil,
host: String.t(),
owner: pid() | nil,
params: map() | Spaceboy.Conn.Unfetched.t(),
path_info: [String.t()],
path_params: map() | Spaceboy.Conn.Unfetched.t(),
peer_cert: binary() | :no_peercert,
port: :inet.port_number(),
query_params: map() | Spaceboy.Conn.Unfetched.t(),
query_string: String.t() | nil,
remote_ip: :inet.ip_address() | nil,
request_path: String.t(),
scheme: :gemini,
state: state()
}
Link to this section Functions
Specs
Set client certificate required response
Specs
Fetch query params - decode query_string to map()
Specs
Add file as response.
Third argument is MIME type of the file. If it is not set the function will use MIME.from_path/1
function to guess its type.
Specs
Add text/gemini string as response
Specs
Set input response
Specs
Add map as JSON response
Specs
Set not found response
Specs
Set redirect response
Specs
Add function to be run righ before the response is actually send.
Multiple functions will get executed in FIFO order.
Examples
iex> conn = Spaceboy.Conn.register_before_send(%Spaceboy.Conn{}, fn conn -> conn end)
iex> length(conn.before_send)
1
iex> is_function(hd(conn.before_send), 1)
true
Specs
resp(conn :: t(), header :: Spaceboy.Header.t(), body :: String.t() | nil) :: t()
Add response header and potentially body to the function.