Current section

Files

Jump to
dream src dream@servers@mist@handler.erl
Raw

src/dream@servers@mist@handler.erl

-module(dream@servers@mist@handler).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/dream/servers/mist/handler.gleam").
-export([create/5]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Request handler for Mist server\n"
"\n"
" This module provides handler creation functionality that converts\n"
" Mist requests to Dream requests, routes them, and converts the\n"
" response back to Mist format.\n"
"\n"
" This is an internal module used by the Dream server implementation.\n"
" Most applications won't need to use this directly.\n"
).
-file("src/dream/servers/mist/handler.gleam", 59).
?DOC(
" Create a request handler that converts Mist requests to Dream requests\n"
"\n"
" This function creates the main request handler used by the Mist server.\n"
" It handles the complete request/response cycle:\n"
"\n"
" 1. Read the request body (with size limit)\n"
" 2. Convert Mist request to Dream request format\n"
" 3. Generate a request ID and update context\n"
" 4. Route the request through the router\n"
" 5. Convert Dream response back to Mist format\n"
"\n"
" ## Parameters\n"
"\n"
" - `router`: The application's router with all routes configured\n"
" - `max_body_size`: Maximum allowed request body size in bytes\n"
" - `template_context`: Template context to clone for each request\n"
" - `services_instance`: Application services (database, cache, etc.)\n"
" - `update_context`: Function to update context with request-specific data\n"
"\n"
" ## Returns\n"
"\n"
" A function that takes a Mist HTTP request and returns a Mist HTTP response.\n"
" This function is what you pass to `mist.new()`.\n"
"\n"
" ## Example\n"
"\n"
" ```gleam\n"
" // Internal use - normally called by dream.listen()\n"
" let handler = handler.create(\n"
" my_router,\n"
" 10_000_000, // 10MB max body\n"
" my_context,\n"
" my_services,\n"
" fn(ctx, request_id) { MyContext(..ctx, request_id: request_id) }\n"
" )\n"
"\n"
" mist.new(handler)\n"
" |> mist.port(3000)\n"
" |> mist.start()\n"
" ```\n"
).
-spec create(
dream@router:router(ADTH, ADTI),
integer(),
ADTH,
ADTI,
fun((ADTH, binary()) -> ADTH)
) -> fun((gleam@http@request:request(mist@internal@http:connection())) -> gleam@http@response:response(mist:response_data())).
create(
Router,
Max_body_size,
Template_context,
Services_instance,
Update_context
) ->
fun(Mist_req) ->
Body_result = mist:read_body(Mist_req, Max_body_size),
case Body_result of
{ok, Req_with_body} ->
{Dream_req, Request_id} = dream@servers@mist@request:convert(
Mist_req,
Req_with_body
),
Request_context = Update_context(Template_context, Request_id),
Dream_resp = dream@dream:route_request(
Router,
Dream_req,
Request_context,
Services_instance
),
dream@servers@mist@response:convert(Dream_resp);
{error, _} ->
_pipe = gleam@http@response:new(400),
gleam@http@response:set_body(
_pipe,
{bytes, gleam@bytes_tree:new()}
)
end
end.